var pics = new Array();

$(document).ready(function()
{

	$('#countries > ul').hide();
	
	$('#countries a:lt(2)').mouseover(function(){
		$('#countries > ul').fadeIn(300);
	});
	
	$('#countries > ul').mouseleave(function(){
		$('#countries > ul').fadeOut(300);
	});
	
	$('#pictures > div > a').showPic();
});


jQuery.fn.showPic = function()
{	
	return this.each(function()
	{		
		$(this).click(function()
		{
			$('#picWindow').remove();
		
			$('#videoplayer').hide();
			
			var picWindow = $('<div id="picWindow">');
			//picWindow.hide();
			
			
			closePicWindow = function()
			{
				$(picWindow).fadeOut('fast');
				$(picWindow).queue(function(){
					$('#videoplayer').show();
					$(picWindow).remove();				
				});
			};
			
			
			var closeButton = $('<a>');
			closeButton.attr('href','#');
			closeButton.addClass('close');
			closeButton.text('close');
			closeButton.click(function()
			{
				closePicWindow();
				return false;
			});
			picWindow.append(closeButton);

			
			var pic = $('<img>');
			pic.attr('src', $(this).attr('href'));
			//pic.click(closePicWindow);
			
			
			var nav = $('<div id="picWindowNavigation">');
			nav.html('<a href="#" class="nextPic">next</a> <a href="#" class="prevPic">previous</a>');
			
			var currentImage = this;
			$('a.prevPic', nav).click(function(){
				if($(currentImage).is(":first-child"))
					currentImage = $('a:last-child', $(currentImage).parent());
				else
					currentImage = $(currentImage).prev();
					
				pic.attr('src', currentImage.attr('href'));
				return false;
			});
			$('a.nextPic', nav).click(function(){
				if($(currentImage).is(":last-child"))
				{
					currentImage = $('a:first-child', $(currentImage).parent());
				}
				else
				{
					currentImage = $(currentImage).next();
				}
				pic.attr('src', currentImage.attr('href'));
				return false;
			});
			picWindow.append(nav);


			

			
			
			picLoaded = function()
			{
				picWindow.css({width: $(pic).attr('width')+'px'});
				picWindow.center($(pic).attr('width'), $(pic).attr('height')+77);
				
				closeButton.after(pic);
				picWindow.show();	
				
				$(picWindow).draggable();
			};
			
			
			
			if(pic.get(0).complete)
			{
				picLoaded();				
			}
			else
				pic.load(picLoaded);
				
		
			
			$('body').append(picWindow);
			
			
			
			
			return false;
		});
	});
	
	return false;
};


function preloadImage(file)
{
	
	var img = new Image();
	img.src = file;
	pics.push(img);

}


jQuery.fn.center = function (width, height) {
	return this.each(function () {
		var t = jQuery(this);

		t.css({
			position:	'absolute', 
			left:		'50%', 
			top:		'50%', 
			zIndex:		'99999'
		}).css({
			marginLeft:	'-' + (width / 2) + 'px', 
			marginTop:	'-' + (height / 2) + 'px'
		});
		t.css({
			marginTop:	parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
			marginLeft:	parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
		});
	});
};



var cart = {

	isEmpty : true,
	
	
	
	add : function(productcode)
	{
		if($('#cart').html()=='')
		{
			cart.isEmpty = true;
			$('#cart').hide();	
		}
		else
			cart.isEmpty = false;
			
		$('#cart').load(document.location.toString(), {a:'cart_add',productcode:productcode}, cart.addCallback);

		return false;
	},
	
	addCallback : function(data)
	{
		if(cart.isEmpty)
		{
			$('#cart').slideDown();
		}
	},
	
	
	
	
	remove : function(productcode)
	{
		$('#cart').load(document.location.toString(), {a:'cart_remove',productcode:productcode}, cart.removeCallback);
		
		return false;
	},
	
	removeCallback : function(data)
	{
		if($('#cart').html()=='')
		{
			$('#cart').slideUp();
		}
	},
	
	
	
	
	changeAmount : function(select, productcode)
	{
		$('#cart').load(document.location.toString(), {a:'cart_changeamount',productcode:productcode,amount:$(select).val()});
		
		return false;
	}

}


function downloadFile(select)
{
	if($(select).val()=='')
		return;
		
	var win = window.open();
	win.location = '/PDF/' + $(select).val();
}