$(document).ready(function () {
	//sidebar categories
	$('.categories h2 a:nth-child(2)').bind("click" , function(){
    	$('h2 .category-close').parent().next('.category').not($(this).parent().next(".category")).slideToggle('slow');
    	$('.category').prev("h2").children('a:nth-child(2)').not($(this)).removeClass('category-close').addClass('category-open');
    	$(this).parent().next().stop(true, true).slideToggle('slow');
    	$(this).toggleClass('category-open').toggleClass('category-close');
	});

	//product ratings controls
	$('.rating-control').click(function(){
		$(this).toggleClass('category-open').toggleClass('category-close').parent().next().slideToggle('fast');
	});

	//notifications close
	$(".close").click(function () {
		$(this).fadeTo(400, 0, function () { // Links with the class "close" will close parent
			$(this).slideUp(400);
		});
		return false;
	});	

	//remove ord pris if not on sale
	if ($('.featured_sale_yes').length ){
	    
	}else{
		$('.optionOrdPrice').parent().remove();
		$('.featured_sale_no').remove();
	}


	//remove empty ord pris
//	columns = $(".productOptions tr th").size();
//	place = columns - 3;

//	if (place > 0)
//	{
//		remove = true;
//		$(".productOptions tr td:nth-child("+place+")").each(function(){
//			val = $(this).children("span").text();
//			if ((val != " ") && (val != "")) remove = false;
			
			//alert($(this).text())
//		})
	
//		if(remove)
//		{
//			$(".productOptions tr th:nth-child("+place+")").remove();
//		    $(".productOptions tr td:nth-child("+place+")").remove();		
//		}		
//	}


	//facebook friends from sidebar
	$('<iframe>').attr({
		'src': '//www.facebook.com/plugins/likebox.php?href=http://www.facebook.com/pages/Piscarise/176195454755&width=196&colorscheme=light&connections=6&stream=false&header=false&height=265',
		'scrolling': 'no',
		'frameborder': '0',
		'style': 'border:none; overflow:hidden; width:196px; height:265px; margin: 1px 0 0 1px;',
		'allowTransparency': 'true'
	}).appendTo('#piscariFriends');



	//gallery - video buttons toggle
	$('.galleryVideoLink').click(function(){
		$('.galleryVideoButtons').toggle();
	});

	//newsletter default values validating method
	jQuery.validator.addMethod("requiredNewsletter", function(value, element) {
		return ((value != "Förnamn*") && (value != "Efternamn*") && (value != "Email*"));
	}, " ");

	//filename validation method
	jQuery.validator.addMethod("myFile", function(value, element) {
		return this.optional(element) || /(?:^|\/|\\)((?:[a-z0-9 _-])*\.(?:jpg|JPG))$/i.test(value);
	}, "<br />Fel Filtyp! Du kan endast ladda upp jpg bilder.");


	//form validation
	jQuery.validator.messages.required = "";
	$(".validateMe").each(function(){
		$(this).validate({
			rules: {
				name_file: {
					required: true,
					myFile: true
				}
			},
			errorClass: 'error',
			errorElement: 'span',
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					var message = errors == 1
						? 'You missed 1 field. It has been highlighted below'
						: 'You missed ' + errors + ' fields.  They have been highlighted below';
					$("div.error span").html(message);
					$("div.error").show();
				} else {
					$("div.error").hide();
				}
			},
			onkeyup: false,
			messages: {
				TG_VR_EMAIL: {
					required: " ",
					email: "Var god ange rätt email adress."
				},
				TG_EMAIL: {
					required: " ",
					email: "<br />Var god ange rätt email adress."
				},
				TG_RATING_FEEDBACK_RESPONSE1: {
					required: "Välj ett betyg värde."
				}				
			},
			debug:false
		});		
	})

	//contest add filename to form based on user input
	$('#submitContest').click(function(event){
		event.preventDefault();
		var imageLink = $('#name_file').val().split('\\');
		var imageName = imageLink[imageLink.length-1];
		$('#TG_FM_FILE_1').val(imageName);
		$('#monthlyContestForm').submit();
	});


});



//adds a product to the shopping cart using ajax
function addToCart(url , logo, sku, price)
{
	$(document).scrollTop(1);
	//$.scrollTo('.shoppingCartItemDetails', 800);
	$.ajax({
		type: "POST",
		url: url,
		error: function() {
			alert("An error ocurred!");
		},
		success: function(data) {
			$(".shoppingCartStatus").html(data);
			$('.shoppingCartItemDetailsImage').html('<img src="'+logo+'" />')
			$('.shoppingCartItemDetailsName').html($('.section').children('h1').text());
			$('.shoppingCartItemDetailsSKU').html(sku);
			$('.shoppingCartItemDetailsPrice').html(price);
			$(".shoppingCartItemDetails").slideDown("slow");
			setTimeout("$('.shoppingCartItemDetails').slideUp('slow');", 4000);
		}
	});	
}



