
var currentPaymentMethod = null;
///////////////////////////////////////
// Init pngFix
///////////////////////////////////////

$(document).ready(function(){ 
	$(document).pngFix(); 

	$("#q").click(function(){
		this.value = '';
	}).blur(function() {
		if ('' == this.value) {
			this.value = 'Sökord...';
			//$(this).removeClass('errorClass');
		}
	});
	
});


///////////////////////////////////////
// Init imageScroller
///////////////////////////////////////

$(function(){
	$("#viewer").imageScroller({
		next:"next",
		prev:"prev",
		frame:"viewerFrame",
		width:568,
		child:"a",
		auto:true
	});
});

///////////////////////////////////////
// Init checkout scripts
///////////////////////////////////////
$(document).ready(function(e) {

	$("input:radio[@name=paymentmethodx]").click(function() { 
			var checkedvalue = $(this).val();
			$('#txtCheckoutPaymentSum').html(((parseInt(checkedvalue)+parseInt($('#totalxsum').val()))/100)+' kr');
	});

		$(".picture").click(function (e) {
			e.preventDefault();
			//$varUrl = 
			$('#largeimage').css('background-image', 'url('+$(this).closest('a').attr('href')+')');
			$("#largeimagelink").attr('href', $(this).closest('a').attr('href').replace('367','original'));  		
		});
		

	$('.deletecoupon').live('click', function(e) {
		e.preventDefault();
		CouponHandler('deletecode', this.id.split('coupon')[1]);
	});
	

	$('.auto-submit-star').rating( { 
		callback:function(value, link) {
		
		$.ajax({
			type: "POST",
			url: "/_scripts/saveproductvote.php",
			data: "call=ajax&productid=" + document.getElementById("productid").value + "&vote=" + value,
			dataType: "text/plain",

			success: function(votes) {
			$("#test p").html(votes);
			},
			error: function(xhr, ajaxOptions, thornError) {
            alert('Error: '+xhr.status);
			//alert(thrownError);
			}


			
		});

		}
	 
	});



	$('#txtCheckoutCart .delete, #txtCheckoutCart .decrease, #txtCheckoutCart .increase').live('click', function(e){
		e.preventDefault();
		var action = $(this).attr('class');
		var productid = $(this).closest('tr').attr('id').split('=');
		var productstockid = productid[2];
		var quantity = productid[3];
		productid = productid[1];
		if ('decrease' === action) {
			quantity--;
		} else if ('increase' === action) {
			quantity++;
		} else if ('delete' === action) {
			quantity = 0;
		}

		$.ajax({
			cache:false,
			type:'POST',
			url:'/_scripts/updatecheckout.php',
			data:'action='+action+'&productid='+productid+'&stockid='+productstockid+'&quantity='+quantity+'&paymentmethod='+$('#frmOrder input[name=paymentmethod]:checked').val(),
			success: function(res) {
				if ('notenoughinstock' == res) {
					hs.htmlExpand(null,{
						src: '/_includes/information.php?subject=Meddelande&message=Vi%20har%20tyvärr%20inte%20så%20många%20exemplar%20i%20lager.',
						objectType: 'iframe',
						width: 330,
						height: 82,
						wrapperClassName: 'no-footer'
					});
				} else {
					$('#txtCheckoutCart').html(res);
				}
			}
		});
	});

	$('.paymentmethod').click(function(){
	

	
		if ('payment2' == this.id) { // 2 = invoice
			if (false === $('#sameaddr').attr('checked')) {
				$('#deliveryaddr input').addClass('gray').removeClass('white');
				//$('#from_ssnumber').addClass('white').removeClass('gray');
				//$('#from_ssnumber').attr('disabled',false);
				$('#deliveryaddr input.copy').val('');
				$('#sameaddr').attr('checked', true);
			}
			$('#deliveryaddr input').attr('disabled', true);
				$('#from_ssnumber').addClass('white').removeClass('gray');
				$('#from_ssnumber').attr('disabled',false);
		} else {
			if ('payment2' == currentPaymentMethod) {
				$('#sameaddr').attr('disabled', false);
			}
				$('#from_ssnumber').addClass('gray').removeClass('white');
				$('#from_ssnumber').attr('disabled',true);
			if (false === $('#sameaddr').attr('checked')) {

				$('#deliveryaddr input').attr('disabled', false).removeClass('gray').addClass('white');
			}
		}

		currentPaymentMethod = this.id;

		$.ajax({
			cache:false,
			type:'POST',
			url:'/_scripts/updatecheckout.php',
			data:'action=changepaymentmethod&paymentmethod='+this.id.split('payment')[1],
			success: function(res) {
				$('#txtCheckoutCart').html(res);
				//$('#payment5').attr('disabled', 'disabled');
			}

		});

	});

	$('#sameaddr').click(function() {
		if (true === $(this).attr('checked')) {
			$('#deliveryaddr input.copy').attr('disabled', true).addClass('gray').removeClass('error').removeClass('white').val('');
		} else {
			$('#deliveryaddr input.copy').attr('disabled', false).removeClass('gray').addClass('white');
		}
	});
});

///////////////////////////////////////
// Init Validation
///////////////////////////////////////
	
$(document).ready(function() {

	jQuery.validator.addMethod("notEqualToText", function(value, element, param) {
		return !(param == value);
	});
	
	
	$('.frmProduct .filterbox').change(function() {
		$(this).css('backgroundColor','#fff');
		$(this).closest('div').removeClass('filterboxOutlineError');
	});
	$('.frmProduct').submit(function(e){

		if(this.stockid.value == '') {
			$(".filterboxOutline",this).addClass('filterboxOutlineError');
			this.stockid.style.backgroundColor = '#fee';
			return false;
			
		}else{
			$(".filterboxOutline",this).removeClass('filterboxOutlineError');
			this.stockid.style.backgroundColor = '#fff';
			ajaxSubmit(this, '#txtCart');
			return false;
		}
	
	});
	
	$('.frmProductWatch').validate({
		showErrors: function() { 
			for ( var i = 0; this.errorList[i]; i++ ) { 
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass ); 
			}

			if (this.settings.unhighlight) { 
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { 
					this.settings.unhighlight.call( this, elements[i], 
					this.settings.errorClass);
				} 
			}
		},

		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
		},

		rules: {
			email: { required: true, email: true }
		},

		submitHandler: function(form) {
			ajaxSubmit(".frmProductWatch");
		}
	});
	
	
	$('#frmContactus').validate({
	
		showErrors: function() { 
			for ( var i = 0; this.errorList[i]; i++ ) { 
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass ); 
			}

			if (this.settings.unhighlight) { 
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { 
						this.settings.unhighlight.call( this, elements[i], 
						this.settings.errorClass); 
				}
			}
		},

		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
			if(document.getElementById('subject').value == '') {
			document.getElementById('outlinesubject').style.borderColor = "#c00";
			document.getElementById('subject').style.backgroundColor = '#fee';
			}
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
			if(!document.getElementById('subject').value == '') {
			document.getElementById('outlinesubject').style.borderColor = "#b4b4b4 #dddddd #dddddd #b4b4b4";
			document.getElementById('subject').style.backgroundColor = '#fff';
			}
			
		},

		submitHandler: function(form) {
			ajaxSubmit("#frmContactus");
		},

		rules: {
			firstname: { required: true, minlength: 2, maxlength: 25 },
			lastname: { required: true, minlength: 2, maxlength: 25 },
			email: { required: true, email: true },
			subject: { required: true },
			message: { required: true }
		}
	});

	$('#frmSearch').validate({
		showErrors: function() {
			for ( var i = 0; this.errorList[i]; i++ ) {
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass );
			}

			if (this.settings.unhighlight) {
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
						this.settings.unhighlight.call( this, elements[i],
						this.settings.errorClass);
				}
			}
		},

		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
		},

		rules: {
			q: { required: true, notEqualToText:'Sökord...' }
		}
	});

	$('#frmCoupon').validate({
		showErrors: function() {
			for ( var i = 0; this.errorList[i]; i++ ) {
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass );
			}

			if (this.settings.unhighlight) {
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
						this.settings.unhighlight.call( this, elements[i],
						this.settings.errorClass);
				}
			}
		},

		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
		},

		rules: {
			code: { required: true}
		},

		submitHandler: function() {
			CouponHandler('addcode');
		}
	});

	$('#frmOrder').validate({
		showErrors: function() {
			for ( var i = 0; this.errorList[i]; i++ ) {
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass );
			}
			if (this.settings.unhighlight) { 
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { 
					this.settings.unhighlight.call( this, elements[i], 
					this.settings.errorClass); 
				}
			}
		},

		highlight: function(element, errorClass) {
			//if($(element).attr('name') != 'paymentmethod' && $(element).attr('name') != 'purchasingconditions') {
			$(element).addClass(errorClass);
			//}
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
		},

		rules: {
			paymentmethod: { required:true },
		
			from_ssnumber: { required:true, minlength:10, maxlength:11},
			from_firstname: { required: true, minlength: 2, maxlength: 25 },
			from_lastname: { required: true, minlength: 2, maxlength: 25 },
			from_address: { required: true, minlength: 2, maxlength: 50 },
			from_zipcode: { required: true, minlength: 2, maxlength: 25 },
			from_city: { required: true, minlength: 2, maxlength: 25 },
			from_phonenumber: { required: true, minlength: 2, maxlength: 25, number: true },
			from_mobilenumber: { required: true, minlength: 10, maxlength: 10, number: true },
			from_email: { required: true, email: true },
			
			to_firstname: { required: true, minlength: 2, maxlength: 25 },
			to_lastname: { required: true, minlength: 2, maxlength: 25 },
			to_address: { required: true, minlength: 2, maxlength: 50 },
			to_zipcode: { required: true, minlength: 2, maxlength: 25 },
			to_city: { required: true, minlength: 2, maxlength: 25 },
			
			purchasingconditions: { required: true }
		}
	});

	$('#frmNewsletter').validate({
		showErrors: function() { 
			for ( var i = 0; this.errorList[i]; i++ ) { 
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass ); 
			}

			if (this.settings.unhighlight) { 
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { 
					this.settings.unhighlight.call( this, elements[i], 
					this.settings.errorClass);
				} 
			}
		},

		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
		},

		rules: {
			name: { required: true, minlength: 2, maxlength: 25 },
			email: { required: true, email: true }
		},

		submitHandler: function(form) {
			ajaxSubmit("#frmNewsletter");
		}
	});

	$('#frmShoppinglist').validate({
		showErrors: function() {
			for ( var i = 0; this.errorList[i]; i++ ) {
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass ); 
			}
				
			if (this.settings.unhighlight) { 
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { 
					this.settings.unhighlight.call( this, elements[i], 
					this.settings.errorClass); 
				} 
			}
		},

		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
		},

		submitHandler: function(form) {
			ajaxSubmit("#frmShoppinglist");
		},

		rules: {
			name: { required: true, minlength: 2, maxlength: 25 },
			email: { required: true, email: true }
		}
	});

	$('#frmTellfriend').validate({
		showErrors: function() {
			for ( var i = 0; this.errorList[i]; i++ ) { 
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass ); 
			}

			if (this.settings.unhighlight) { 
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { 
					this.settings.unhighlight.call( this, elements[i], 
					this.settings.errorClass); 
				}
			}
		},

		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
		},

		submitHandler: function(form) {
			ajaxSubmit("#frmTellfriend");
		},

		rules: {
			name: { required: true, minlength: 2, maxlength: 25 },
			toemail: { required: true, email: true },
			fromemail: { required: true, email: true }
		}
	});

	$('#frmProductComment').validate({
		showErrors: function() { 
			for ( var i = 0; this.errorList[i]; i++ ) { 
				this.settings.highlight.call( this, this.errorList[i].element, this.settings.errorClass );
			}

			if (this.settings.unhighlight) { 
				for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) {
					this.settings.unhighlight.call( this, elements[i], 
					this.settings.errorClass); 
				}
			}
		},

		highlight: function(element, errorClass) {
			$(element).addClass(errorClass);
		},

		unhighlight: function(element, errorClass) {
			$(element).removeClass(errorClass);
		},

		submitHandler: function(form) {
			//alert('test');
			ajaxSubmit("#frmProductComment");
		},

		rules: {
			name: { required: true, minlength: 2, maxlength: 50 },
			email: { required: true, email: true },
			rating: { required: true },
			comment: { required: true}
		}
	});
	
	$('.fbsLink').click(function() {
		var url = this.href.replace(/share\.php/, 'sharer.php');
		window.open(url,'sharer','toolbar=0,status=0,width=626,height=436');
		return false; 
	});

});
