$(function() {
	$('.fancy_img').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	400,
		'overlayColor'	:	'#000',
		'overlayOpacity':	0.4,
		'overlayShow'	:	true,
		'centerOnScroll':	true
	});
	$(".inline").fancybox({
		'type'				: 'iframe',
		'width'				: 700,
		'height'			: 450,
		'autoDimensions'	: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	$('.trig_mail').click(function(event) {
		$('.mail_form_trig').slideToggle('slow');
		event.preventDefault();
	});
	$(".StaffTitle").toggle(function(){
 		var id = $(this).attr('rel');
 		$("#StaffCont_"+id).slideDown('slow');
 	},
 	function(){
 		var id = $(this).attr('rel');
 		$("#StaffCont_"+id).slideUp('fast');
 	})
	var overall_width = 0;
	var end_width = 25;
	$('.nav a').each(function(index, elem) {
	    var $elem = $(elem);
	    overall_width += $elem.outerWidth();
	});
	end_width = 850 - 28 - overall_width - ($('.split').size() * 1);
	//alert(end_width);
	$('.t_sub').hover(function() {
		$(this).children('ul').slideDown('fast');
	}, function() {
		$(this).children('ul').slideUp('fast');
	});
	$('.check_term').click(function(event) {
		if ($('#accept:not(:checked)').length) {
			alert('Til að ganga frá kaupum verður þú að samþykkja skilmálana okkar.');
			event.preventDefault();
		}
	});
	$('.nav_end, .nav_end .shader').css('width', end_width+'px');
	$('.select_large').live('click', function() {
		$(this).toggleClass('sel_up', 'select_large');
		$(this).children('ul').slideToggle('fast');
	});
	$('.nav a, .sub_nav a').click(function(event) {
		var this_link = $(this).attr('href');
		$.ajax({
			type: "GET",
			url: "/ajax.php",
			data: "action=clearsession",
			success: function(data) {
				window.location = this_link;
			}
		});
		event.preventDefault();
	});  
	$('.select_large ul li a').click(function(event) {
		$(this).parents('.select_large').toggleClass('sel_up', 'select_large');
		$(this).parents('ul').find('a').removeClass('selected');
		var class_pos = $(this).attr('class');
		var rel_id = $(this).attr('rel');
		$('#f_'+class_pos).val(rel_id);
		$(this).parents('.select_large').find('.sel_title').text($(this).text());
		$(this).parents('.select_large').children('ul').slideToggle('fast');
		$(this).addClass('selected');
		$('#p_filter').submit();
		event.stopPropagation();
	});
	$('.select_size').live('click', function() {
		$(this).toggleClass('sel_up', 'select_size');
		$(this).children('ul').slideToggle('fast');
	});
	$('.select_size ul li a').live('click', function(event) {
		$(this).parents('.select_size').toggleClass('sel_up', 'select_size');
		$(this).parents('ul').find('a').removeClass('selected');
		var width_price_text = $(this).data('pricetext');
		var width_price = $(this).data('price');
		$('.prod_box_price').text(width_price_text);
		var class_pos = $(this).attr('class');
		var rel_id = $(this).attr('rel');
		$('#pr_'+class_pos).val(rel_id);
		if (width_price != undefined) {
			$('#pr_prodprice').val(width_price);
		}	
		$(this).parents('.select_size').find('.sel_title').text($(this).text());
		$(this).parents('.select_size').children('ul').slideToggle('fast');
		$(this).addClass('selected');
		event.stopPropagation();
	});
	$('.select_size_for_2').live('click', function() {
		$(this).toggleClass('sel_up', 'select_size_for_2');
		$(this).children('ul').slideToggle('fast');
	});
	$('.select_size_for_2 ul li a').live('click', function(event) {
		$(this).parents('.select_size_for_2').toggleClass('sel_up', 'select_size_for_2');
		$(this).parents('ul').find('a').removeClass('selected');
		var width_price_text = $(this).data('pricetext');
		var width_price = $(this).data('price');
		$('.prod_box_price').text(width_price_text);
		var class_pos = $(this).attr('class');
		var rel_id = $(this).attr('rel');
		$('#pr_'+class_pos).val(rel_id);
		if (width_price != undefined) {
			$('#pr_prodprice').val(width_price);
		}
		$('#pr_ring_size').val($('#pr_ring_size_1').val()+','+$('#pr_ring_size_2').val());
		$(this).parents('.select_size_for_2').find('.sel_title').text($(this).text());
		$(this).parents('.select_size_for_2').children('ul').slideToggle('fast');
		$(this).addClass('selected');
		event.stopPropagation();
	});
	$('.select_cart').live('click', function() {
		$(this).toggleClass('sel_up', 'select_cart');
		$(this).children('ul').slideToggle('fast');
	});
	$('.select_cart ul li a').live('click', function(event) {
		$(this).parents('.select_cart').toggleClass('sel_up', 'select_cart');
		$(this).parents('ul').find('a').removeClass('selected');
		if ($(this).parents().hasClass('sizes_f_2')) {
			var class_pos = $(this).attr('class');
			var rel_id = $(this).attr('rel');
			$('#pr_'+class_pos).val(rel_id);
			$('#pr_ring_size').val($('#pr_ring_size_1').val()+','+$('#pr_ring_size_2').val());
			var ring_size = $('#pr_ring_size').val();
		}
		else {
			var ring_size = $(this).data('ringsize');
		}
		var width_price = $(this).data('price');
		var size = $(this).data('size');
		var prodid = $(this).data('prodid');
		$.ajax({
			type: "GET",
			url: "/ajax.php",
			data: "action=loadcart&prodnr="+prodid+"&price="+width_price+"&ring_size="+ring_size+"&size="+size,
			success :function(data)
	        {
	            $('#CartContent').html(data);
	        }
		});
		/*$('#pr_'+class_pos).val(rel_id);
		$('#pr_prodprice').val(width_price);
		$(this).parents('.select_cart').find('.sel_title').text($(this).text());
		$(this).parents('.select_cart').children('ul').slideToggle('fast');
		$(this).addClass('selected');*/
		event.stopPropagation();
	});
	if ($('.prods .small_thumb').length) {
		var isiDevice = navigator.userAgent.match(/iP/i) != null;
	
		if(isiDevice){
			$('.prods .small_thumb .p_title').clearQueue().animate({
				'position'	:	'absolute',
				'bottom'	:	'0px'
			}, 500);
		}
	}
	$('.prods .small_thumb').hover(function() {
		$(this).children('.p_title').clearQueue().animate({
			'position'	:	'absolute',
			'bottom'	:	'0px'
		}, 500);
	}, function() {
		$(this).children('.p_title').clearQueue().animate({
			'position'	:	'absolute',
			'bottom'	:	'-30px'
		}, 500);
	});
	$('.cart_remove').live('click', function(event) {
		
		var p_prodid = $(this).attr('rel');
		//alert(p_prodid);
		var p_item = $(this).parents('.cart_item');
		$.ajax({
			type: "GET",
			url: "/ajax.php",
			data: "action=removecartitem&key="+p_prodid,
			success: function(data) {
				p_item.slideUp(600, function() {
					p_item.remove();
					$.ajax({
						type: "GET",
						url: "/ajax.php",
						data: "action=loadcart",
						success :function(data)
				        {
				            $('#CartContent').html(data);
				        }
					});
				});
			}
		})
		event.preventDefault();
	});
	$('#CheckoutContent > .Options .Tab').bind({
		click : function()
		{
			$(".Tab").removeClass('Selected');
			$(".swap").addClass('hide');
			$(this).addClass('Selected');
			var tab = $(this).attr('rel');
			$('#'+tab).removeClass('hide');
		}
	});
	$('.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();
	});
	$('.prod_buy').live('click', function(event) {
		var item = $(this);
		var p_prodid = $('#pr_prodid').val();
		var p_price = $('#pr_prodprice').val();
		var p_size = $('#pr_size').val();
		var p_ring_size = $('#pr_ring_size').val();
		var p_color = $('#pr_color').val();
		var p_quant = $('#pr_quant').val();
		
		if ($('.sel_ring_sizes').length) {
			if ($('#pr_ring_size_1').val() == '' || $('#pr_ring_size_2').val() == '') {
				alert('Það verður að velja stærð fyrir báða hringa');
				return false;
			}
		}
		
		if ($('.sel_ring_size').length) {
			if ($('#pr_ring_size').val() == '') {
				alert('Það verður að velja stærð á hringnum');
				return false;
			}
		}
		
		$.ajax({
			type: "GET",
			url: "/ajax.php",
			data: "action=addtocart&prodid="+p_prodid+"&size="+p_size+"&color="+p_color+"&quant="+p_quant+"&price="+p_price+"&ring_size="+p_ring_size,
			success: function(data) {
				var add_clone = item.clone();
				var this_pos = item.offset();
				var this_cart_pos = $('.shop_cart').offset();
		
				add_clone.attr('id', 'moving_cart');
				
				$('body').append(add_clone);
				$('#moving_cart').css({'position':'absolute', 'top': this_pos.top+'px', 'left' : this_pos.left+'px', 'z-index': '100'});
				$('#moving_cart').animate({
					left: this_cart_pos.left,
					top: this_cart_pos.top,
					opacity: 0.4
				}, 900, function() {
					var num_items = parseFloat($('.nr_prods').text()) + parseFloat(p_quant);
					$('.nr_prods').text(num_items);
					
					if (num_items >= 2) {
						$('.nr_prods_text').text(' vörur');
					}
					else {
						$('.nr_prods_text').text(' vara');
					}
					$('#moving_cart').remove();
				});
				item.css({
					'background':'url(/sysimages/jo_sprite.png) no-repeat -395px 0px'
				});
				
				
				$('.shop_cart').animate({
					'z-index'	:	100
				}, 900).delay(2000).animate({
					'z-index'	:	1
				}, 500);
			}
		})
		event.preventDefault();
	});
	$('.view_prod').click(function(event) {
		var b_width = ($('body').outerWidth() - 470) / 2;
		var b_height = ($('body').outerHeight() - 230 - 80) / 2;

		$('body').append('<div class="prod_over"></div><div class="prod_box darker_shadow" style="position: fixed; top: '+b_height+'px; left: '+b_width+'px;"></div><a href="#" class="prod_box_close" style="position: fixed; z-index: 91; top: '+(b_height - 20)+'px; left: '+(b_width + 430 + 20)+'px;"><img src="/sysimages/prod_box_close.png" border="0" alt="Loka glugga" /></a><a href="#" class="prod_arr_prev darker_shadow" style="position: fixed; z-index: 91; top: '+(b_height + 95)+'px; left: '+(b_width - 72)+'px;" rel="'+$(this).attr('rel')+'"><img src="/sysimages/prod_arr_left.png" border="0" alt="Fyrri vara" /></a><a href="#" class="prod_arr_next darker_shadow" style="position: fixed; z-index: 91; top: '+(b_height + 95)+'px; left: '+(b_width + 430 + 72)+'px;" rel="'+$(this).attr('rel')+'"><img src="/sysimages/prod_arr_right.png" border="0" alt="Næsta vara" /></a>');
		
		$.ajax({
			type: "GET",
			url: "/ajax.php",
			data: "get_product="+$(this).attr('rel')+"&cat="+$('#cat').val(),
			success: function(data) {
				var dataJSON = jQuery.parseJSON(data);
				$('.prod_box').html(dataJSON.html);
			}
		})
		event.preventDefault();
	});
	$('.prod_arr_next').live('click', function(event) {
		$.ajax({
			type: "GET",
			url: "/ajax.php",
			data: "get_product="+$(this).attr('rel')+'&next=true&cat='+$('#cat').val(),
			success: function(data) {
				var dataJSON = jQuery.parseJSON(data);
				$('.prod_box').html(dataJSON.html);
				$('.prod_arr_next, .prod_arr_prev').attr('rel', dataJSON.id);
			}
		})
		event.preventDefault();		
	});
	$('.prod_arr_prev').live('click', function(event) {
		$.ajax({
			type: "GET",
			url: "/ajax.php",
			data: "get_product="+$(this).attr('rel')+'&prev=true&cat='+$('#cat').val(),
			success: function(data) {
				var dataJSON = jQuery.parseJSON(data);
				$('.prod_box').html(dataJSON.html);
				$('.prod_arr_next, .prod_arr_prev').attr('rel', dataJSON.id);
			}
		})
		event.preventDefault();		
	});
	$('.prod_over, .prod_box_close').live('click', function(event) {
		$('.prod_over, .prod_box, .prod_arr_prev, .prod_arr_next, .prod_box_close').remove();
		event.preventDefault();
	});
	$('html').keyup(function(event) {
		if ($('.prod_over').length) {
			//alert(event.keyCode);
			if (event.keyCode == 39) {
				$('.prod_arr_next').trigger('click');
			}
			if (event.keyCode == 37) {
				$('.prod_arr_prev').trigger('click');
			}
			if (event.keyCode == 27) {
				$('.prod_box_close').trigger('click');
			}
		}
	});
});

function ConfirmNewUser()
{
	var name = $('#name');
	var address = $('#address');
	var city = $('#city');
	var zip = $('#zip');
	var phone = $('#phone');
	var email = $('#email');
		
	var error = "";
	
	if(name.value == "")
	{
		error += "&middot; Nafn verður að vera útfyllt<br>\n";
	}
	if(address.value == "")
	{
		error += "&middot; Heimilisfang verður að vera útfyllt<br>\n";
	}
	if(city.value == "")
	{
		error += "&middot; Bæjarfélag verður að vera útfyllt<br>\n";
	}
	if(zip.value == "")
	{
		error += "&middot; Póstnúmer verður að vera útfyllt<br>\n";
	}
	if(phone.value == "")
	{
		error += "&middot; Símanúmer verður að vera útfyllt<br>\n";
	}
	if(email.value == "")
	{
		error += "&middot; Netfang verður að vera útfyllt<br>\n";
	}
	if(email.value.indexOf('@') == -1)
	{
		error += "&middot; Netfang verður að vera gilt<br>\n";
	}
	
	if(error != "")
	{
		var errorMsg = "<b>Ekki tókst að framkvæma skráningu vegna eftirtalinna atriða</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 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,
			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
	{
		
	}
}
function fade_message() {
	$('.mail_form_message').fadeTo('fast', 0, function() {
		$('.mail_form_message').remove();
	});
}

