$(function() {
	$('.fancy_img').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200,
		'overlayColor'	:	'#fff',
		'overlayOpacity':	0.7,
		'overlayShow'	:	true
	});

	$('a.iframe').fancybox();
	
	$('.cart').toggle(function() {
		$('.cart_list').slideToggle('slow');
		$('body').append('<div id="white_backgr" style="position: absolute; display: none; top: 0px; left: 0px; width: 100%; height: 100%; background: #fff;"></div>');
		$('#white_backgr').fadeTo(500, 0.7);
		$('.cart_arrow').animate({
			backgroundPosition: '-964px -13px'
		}, 800);
	}, function() {
		$('.cart_list').slideToggle('slow');
		$('#white_backgr').fadeTo(500, 0, function() {
			$('#white_backgr').remove();
		});
		$('.cart_arrow').animate({
			backgroundPosition: '-964px 0px'
		}, 800);
	});
	
	$('.checkout_link').click(function()
	{
		window.location=$(this).attr('rel');
	});
	$('.prod_item').hover( function() {
		$(this).addClass('hover_effect');
	}, function() {
		$(this).removeClass('hover_effect');
	});
	
	$('.add_to_cart').click(function() {
		// Quick fix
		var p = $(this).parent('li:first');
		window.location = $('a:first', p).attr('href');
		return;
		var add_clone = $(this).siblings('.pi_image').children('img').clone();
		var this_pos = $(this).siblings('.pi_image').children('img').offset();
		var this_cart_pos = $('.cart').offset();

		add_clone.attr('id', 'moving_cart');
		
		//alert(this_cart_pos.left+' '+this_cart_pos.top);
		$('body').append(add_clone);
		$('#moving_cart').css({'position':'absolute', 'top': this_pos.top+'px', 'left' : this_pos.left+'px', 'z-index': '20'});
		$('#moving_cart').animate({
			left: this_cart_pos.left + 60,
			top: this_cart_pos.top,
			width: '10px',
			opacity: 0.2
		}, 900, function() {
			$('#nr_prods').text(parseInt($('#nr_prods').text()) + 1);
			$('#moving_cart').remove();
		});
	});
	$('.fade_me').hover(function() {
		$(this).stop().clearQueue().fadeTo(800, 0.5);
	}, function() {
		$(this).stop().clearQueue().fadeTo(500, 1);
	});
	
	$('#addtocart').submit(function()
	{
		var prodid = $('#prodid').val();
		var size = $('#size').val();
		var color = $('#color').val();
		var quant = $('#quant').val();

		var error = "";
		
		if(size == "null")
		{
			error += "You must select size\n";
		}
		if(color == "null")
		{
			error += "You must select color/style\n";
		}
		if(quant == "null")
		{
			error += "You must select quantity\n";
		}
		
		if(error != "")
		{
			alert(error);
		}else
		{
			$.ajax({
				url : '/ajax.php',
				data : 'action=addtocart&prodid='+prodid+'&size='+size+'&color='+color+"&quant="+quant,
				success : function(data)
				{
					$('#iteminCart').slideDown('fast');
					$('#nr_prods').text(data);
					doCart();
				}
			});
		}
		return false;		
	});
	
	/*$('.altImg').click(function()
	{
		var imagesrc = $(this).attr('alt');
		//var imageUrl = "/images/"+imagesrc;
		var thumbimgUrl = "/thumb.php?file="+imagesrc+"&h=298&h=466";
		$('#largeImg').attr('src',thumbimgUrl);
		//$('#largeImgLink').attr('href',imageUrl);
		return false;
	});*/
	
	$('#requestForm > form > input[type=text]').focus(function()
	{
		if($(this).attr('rel') == $(this).val())
		{
			$(this).val('');
		}
	});
	
	$('#requestForm > form > input[type=text]').blur(function()
	{
		if($(this).val() == '')
		{
			var tval = $(this).attr('rel');
			$(this).val(tval);
		}	
	});
	
	$('#requestForm > form > textarea').focus(function()
	{
		if($(this).attr('rel') == $(this).val())
		{
			$(this).val('');
		}
	});
	
	$('#requestForm > form > textarea').blur(function()
	{
		if($(this).val() == '')
		{
			var tval = $(this).attr('rel');
			$(this).val(tval);
		}	
	});
	
	$('.productInquiry').click(function()
	{
		$("#requestForm").slideToggle();
		$(".form_field").slideToggle();
	});
	
	$('#CheckoutContent > .Options .Tab').bind({
		click : function()
		{
			$(".Tab").removeClass('Selected');
			$(".swap").addClass('hide');
			$(this).addClass('Selected');
			var tab = $(this).attr('rel');
			$('#'+tab).removeClass('hide');
		}
	});
	
	$('.note').live('click',function()
	{
		$(this).slideUp('fast');
		$('.productInquiry').click();
		
	});
	
	$('#request').live('submit',function(event){
		//$('#requestForm').html('<img src="/sysimages/ajax-loader.gif" border="0">');
		$.ajax({
			url: '/ajax.php',
			data: 'action=sendmail&name='+$('#name').val()+'&email='+$('#email').val()+'&msg='+$('#message').val()+'&product='+$('#product').val()+'&loc='+window.location.href,
			type: "GET",
			contentType: "application/json",
			dataType: "json",
			success : function(data)
			{
				if(data.error != '')
				{
					$('#'+data.error).css({border:'1px solid #900'});
					$('#senderror').html(data.msg);
					$('#senderror').slideDown('fast');
				}else
				{
					$('#requestForm').html('<div class="note">Request has been sent - Click to close</div>');
				}
			}
		});
		event.preventDefault();
	});
	
	$('.forgotten').click(
		function()
		{
			$('.loginformholder').slideToggle('fast');
			$('.forgottenpassform').slideToggle('fast');
		}
	);
	
	$('#forgottenpass').live('submit',function(event){
		var email = $('#forgottenemail').val();
		if(email == "")
		{
			alert("Email must be set");
		}else if(email.indexOf('@') == -1)
		{
			alert("Must be a valid email");
		}else
		{
			$.ajax({
				url : '/ajax.php',
				data : 'action=sendpassword&email='+email,
				type: "GET",
				contentType: "application/json",
				dataType: "json",
				success : function(data)
				{
					if(data.response == "ok")
					{
						$('#NewUserError').html(data.msg);
						$('#NewUserError').slideDown('fast');
						$('.forgotten').click();
					}
					if(data.response == "error")
					{
						$('#NewUserError').html(data.msg);
						$('#NewUserError').slideDown('fast');
					}
				}
			});
		}
		event.preventDefault();
	});
	
	$('.rmitem').live('mouseover mouseout click',function(event){
		switch(event.type)
		{
			case "mouseover" :
				$(this).fadeTo('fast',0.6);		
			break;
			case "mouseout" :
				$(this).fadeTo('fast',1);
			break;
			case "click" :
				var item = $(this).attr('key');
				if(confirm($(this).attr('note')) == true)
				{
					$('#row_'+item).fadeOut('slow',function(){
						$.ajax({
							url : '/ajax.php',
							data : 'action=removecartitem&key='+item,
							success : function()
							{
								$.ajax({
									url : '/ajax.php',
									data : 'action=loadcart',
									success : function(data)
									{
										$('#CartContent').html(data);
									}
								});
							}
						});
					})
				}
			break;
		}
	});
	
	
	$('.move_down').click(function(){
		var startloc =  parseInt($('.desc_holder').css('top'));
		var maxheight = parseInt($('.desc_holder').css('height'));
		var holderheight = parseInt($('.description').css('height'));
		var newloc = startloc - 80;

		var calc = maxheight + newloc - holderheight;	
		
		if(calc > 0)
		{
			$('.desc_holder').animate({
				top: newloc+"px"
			},500);
		}
	});
	$('.move_up').click(function(){
		var startloc =  parseInt($('.desc_holder').css('top'));
		var holderheight = parseInt($('.description').css('height'));
		var newloc = startloc + 80;
		
		var maxheight = parseInt($('.desc_holder').css('height'));
		
		if(newloc <= 0)
		{
			$('.desc_holder').animate({
				top: newloc+"px"
			},500);
		}
	});
	
	check_size();
	doCart();
});

function check_size() {
	var left_s = parseInt($('.nav .upper').css('height'));
	var right_s = parseInt($('.c_content').css('height')) - 115;
	
	if (left_s < right_s) {
		$('.nav .upper').css('height', right_s+'px');
	}
}

function moveSlider(direction)
{
	var sliderLoc = parseInt($('#imageSlider').css('top'));
	var itemHeight = 12 + $('#other_images > ul > li:first-child').height();// 166;
	var elnumber = $('#other_images > ul > li').length;
	var maxslider = -((itemHeight * elnumber) - (itemHeight * 4));
	var run = true;
	
	if(direction == "up")
	{
		var moveTo = sliderLoc - itemHeight;
	}
	
	if(direction == "down")
	{
		var moveTo = itemHeight + sliderLoc;
	}
	
	if(moveTo < maxslider)
	{
		run = false;
	}
	
	if(moveTo > 0)
	{
		run = false;
	}
	
	
	if(elnumber < 4)
	{
		run = false;
	}
	
	if(run == true)
	{
		$('#imageSlider').animate({
			top: moveTo+"px"
		},500);
	}
}

//function moveDesc(direction)
//{
//	var sliderLoc = parseInt($('.description').css('top'));
//	var itemHeight = 12 + $('#other_images > ul > li:first-child').height();// 166;
//	var elnumber = $('#other_images > ul > li').length;
//	var maxslider = -((itemHeight * elnumber) - (itemHeight * 4));
//	var run = true;
//	
//	if(direction == "up")
//	{
//		var moveTo = sliderLoc - itemHeight;
//	}
//	
//	if(direction == "down")
//	{
//		var moveTo = itemHeight + sliderLoc;
//	}
//	
//	if(moveTo < maxslider)
//	{
//		run = false;
//	}
//	
//	if(moveTo > 0)
//	{
//		run = false;
//	}
//	
//	
//	if(elnumber < 4)
//	{
//		run = false;
//	}
//	
//	if(run == true)
//	{
//		$('#imageSlider').animate({
//			top: moveTo+"px"
//		},500);
//	}
//}

function doCart()
{
	$.ajax({
		url :'/ajax.php',
		data : 'action=docart',
		success : function(data)
		{
			$('#cartTable').html(data);
		}
	});
}

function ConfirmNewUser()
{
	var name = document.getElementById('name');
	var address = document.getElementById('address');
	var city = document.getElementById('city');
	var country = document.getElementById('country');
	var zip = document.getElementById('zip');
	var phone = document.getElementById('phone');
	var email = document.getElementById('email');
		
	var error = "";
	
	if(name.value == "")
	{
		error += "&middot; Name must be set<br>\n";
	}
	if(address.value == "")
	{
		error += "&middot; Address must be set<br>\n";
	}
	if(city.value == "")
	{
		error += "&middot; City must be set<br>\n";
	}
	if(zip.value == "")
	{
		error += "&middot; Zip code must be set<br>\n";
	}
	if(phone.value == "")
	{
		error += "&middot; Phone must be set<br>\n";
	}
	if(country.value == "")
	{
		error += "&middot; Country must be set<br>\n";
	}
	if(email.value == "")
	{
		error += "&middot; Email must be set<br>\n";
	}
	if(email.value.indexOf('@') == -1)
	{
		error += "&middot; Email must be a valid email<br>\n";
	}
	
	if(error != "")
	{
		var errorMsg = "<b>The registration could not be executed because of the following errors</b><br>\n";
		errorMsg += error;
		
		document.getElementById('NewUserError').innerHTML = errorMsg;
		$("#NewUserError").slideDown('slow');
		return false;
	}else
	{
		return true;
	}
}

function checkUserLogin()
{
	var username = document.getElementById('loginusername');
	var password = document.getElementById('userpass');
	
	var error = "";
	
	if(username.value == "")
	{
		error += "&middot; Username must be set<br>\n";
	}
	if(password.value == "")
	{
		error += "&middot; Password must be set<br>\n";
	}
	
	if(error != "")
	{
		var errorMsg = "Login could not be executed because of the following errors<br>\n";
		errorMsg += error;
		document.getElementById('NewUserError').innerHTML = errorMsg;
		$("#NewUserError").slideDown('slow');
		return false;
	}else
	{
		return true;
	}
}

function preSubmitOrder()
{
	var userid = $('#buyerid').val();	
	var name = $('#buyername').val();	
	var address = $('#buyeraddress').val();	
	var zip = $('#buyerzip').val();	
	var city = $('#buyercity').val();	
	var country = $('#buyercountry').val();	
	var phone = $('#buyerphone').val();	
	var email = $('#buyeremail').val();	
	var dates = $('#buyerdates').val();	
	var items = $('#buyeritems').val();	
	var ref = $('#Tilvisunarnumer').val();	
	var pickup = $('#buyerpickup').val();	
	var other = $('#other').val();
	
	var terms = $('#terms:checked').length;
	
	if(terms == 1)
	{
		$.ajax({
			url :'/ajax.php',
			data : 'action=presubmit&name='+name+'&address='+address+'&city='+city+'&zip='+zip+'&country='+country+'&phone='+phone+'&email='+email+'&items='+items+'&ref='+ref+'&pickup='+pickup+'&reference='+ref+'&userid='+userid+'&other='+other,
			async : false,
			success :function(data)
					{
						return true;
					}		
		});	
	}else
	{
		alert("You must check that you have read the terms and conditions");
		return false;
	}
}

function ajaxSendInquiry()
{
	var name = $('#name').val();
	var email = $('#email').val();
	var message = $('#message').val();
	var product = $('#product').val();
	
	var error = "";
	
	if(name =="")
	{
		error += "Name must be set\n";
	}
	if(email == "")
	{
		error += "Email must be set\n";
	}
	
	if(error != "")
	{
		alert(error)
		return false;
	}else
	{
		
	}
}

