var currentRating = 0;

$(document).ready(function() {

	/* Text replacement selection */
	$('#content h2').addClass('top10font');
	$('h2.lightbox').removeClass('top10font');

	/* sifr exception */
	$('#wide_banner_content h2').removeClass('top10font');
	$('#content #shop_section h2').css('margin','0');
	$('#products table th a').each(function(){
		 var url=$(this).attr('href');
		$(this).attr('href',url+'#products');
	});
	

	//$('#wide_banner_content h2').css('color',merchant_colour);

	
	/* page scrolling */
	
	$('.scroll_link').click(function(){
		$(window).scrollTo($(this).attr('href'), 250);
		return false;
	});
	
	$('.link_top').click(function(){
		$(window).scrollTo('#header', 250);
		return false;
	});

	/* merchant features */
	$('#features dt').show();
	$('#features #feat_wrap').css('height','226px')
	.css('padding','0');
	$('#feature_details ul').css('margin','0');
	 
	if ($("#feature_details").length) {
		$("#feature_details").attr("id","feature_nav");
		$("#feature_nav").parent().append('<div id=\"feature_display\" class=\"text_content\"></div>');
		$("#feature_nav dd").hide();
		$("#feature_nav dt").each(function(index) {
			$(this).html('<a href="#">'+$("#feature_nav dt:eq("+index+")").html()+'</a>');
		});
	$("#ftr0_title a").addClass("selected"); // The first element is selected by default
		$("#feature_nav a").click(function(){
			$("#feature_nav dt a").removeAttr("class");	
			$(this).addClass("selected");
			$(this).parent().attr("class","active");
			$("#feature_display").html($(this).parent().next().html());
			return false;
		});
		$("#feature_nav dt:first").attr("class","active");
		$("#feature_display").html($("#feature_nav dd:first").html());
	}
	
	
	/* user reviews */
	$('#review_link').show();
	if ($("#more_reviews").length) {
		$("#more_reviews").prev().append('<div id="more_reviews_link"><a href="#">Show more reviews</a> ('+$("#more_reviews li").length+' hidden)</div>');
		$("#more_reviews").hide();
		$('#more_reviews_link a').click(function() {
			if ($(this).html() == 'Show more reviews') {
				$(this).html('Hide reviews');
				$(this).next().html('');
			}
			else { 
				$(this).html('Show more reviews');
				$(this).next().html('('+$("#more_reviews li").length+' hidden)');
			}
			$("#more_reviews").slideToggle('slow');
			return false;
		});  
	}
		$("div#review").hide();
		var review_width = 320;
		var review_height = 500;
		if ($.browser.version < 7) {
			var review_width = 340;
			var review_height = 540;
		}

		$("a#review_link").fancybox({
		'width':322,
		'height':524,
			'onComplete': function(){
			addCloseText();
			$('#fancybox-inner #review').show();
				//$('#rating_form').eq(0).removeAttr('id');
				rate(0);
				$("#fancybox-inner form").submit(checkForm);
				$('.btn_clone').click(function(){$(this).parents('form').submit();return false;});
				if (!$.browser.msie || $.browser.version > 7) {
					Cufon.now();
					Cufon.replace('.user_review_form #top h2');
				}
			},
			'onClosed':function(){
			$('#review').hide();
				//$('#fancy_close').empty();
				//$('#review > form').attr('id','rating_form');
			}
		});

	/* star ratings */

	$("#fancybox-inner #rating_form .star_rating li a").live('click',function(){
		rate($(this).attr("class").substring(4));
		return false;
	});
	$("#fancybox-inner #rating_form .star_rating li a").live('mouseover',function(){
		highlight($(this).attr("class").substring(4));
	});
	$("#fancybox-inner #rating_form .star_rating li a").live('mouseout',function(){
		lowlight();
	});

	/* mailing list */
	
	$("#consent div").hide();
	$("#consent label").append(' <a href="#">Privacy policy</a>.');
	$("#consent a").click(function(){
		$("#consent div").slideToggle('fast');
		return false;
	});
});

function rate(rating) {
	
	$("#fancybox-inner .rating").attr('value',rating).prev().find('.error_alert_icn').hide();
	highlight(rating);
}

function highlight(rating) {
	if (rating > 0) {
		$(".star1").css("background-position","0 -18px");
	}
	if (rating > 1) {
		$(".star2").css("background-position","0 -18px");
	}
	if (rating > 2) {
		$(".star3").css("background-position","0 -18px");
	}
	if (rating > 3) {
		$(".star4").css("background-position","0 -18px");
	}
	if (rating > 4) {
		$(".star5").css("background-position","0 -18px");
	}
}

function lowlight() {
	currentRating = $("#fancybox-inner .rating").attr('value');
	if (currentRating < 1) {
		$(".star1").css("background-position","0 0");
	}
	if (currentRating < 2) {
		$(".star2").css("background-position","0 0");
	}
	if (currentRating < 3) {
		$(".star3").css("background-position","0 0");
	}
	if (currentRating < 4) {
		$(".star4").css("background-position","0 0");
	}
	if (currentRating < 5) {
		$(".star5").css("background-position","0 0");
	}
}

function checkForm() {

	var self = this;
	var valid = false;
	
	var formFail = function(field, problem) {
		$('.'+field, self).addClass('invalid').prev().find('.error_alert_icn').show();
		
		if ($('.comment').is('.invalid')) {
			$('.comment').prev().prev().find('.error_alert_icn').show();
		}
		valid=false;
		return false;
	}
	
	function validate(field){
		$(field).removeClass('invalid');
		$(field).prev().find('.error_alert_icn').hide();
		if (!$('.comment').is('.invalid')) {
			$('.comment').prev().prev().find('.error_alert_icn').hide();
			$('.validation_message').hide();
		}
	}
	
		if ($(".reviewer_name", this).val()=='' || $("#reviewer_name", this).val()==' ' || !emailPattern.test($(".email", this).val()) || $(".comment", this).val()=='' || $(".comment",this).val()==' ' || $(".comment", this).val().length < 20) {
			valid = false;
		}
		else {valid=true}

	
	if ($(".reviewer_name", this).val()==''||$("#reviewer_name", this).val()==' ') {
		formFail('reviewer_name','Please enter your name');
	}
	else {
		validate($(".reviewer_name", this));
	}
	
	if (!emailPattern.test($(".email", this).val())) {
		formFail('email', 'Please enter a valid email address');
	}
	else {
		validate($(".email", this));
	}
	
	if ($(".comment", this).val()=='' || $(".comment",this).val()==' ') {
		formFail('comment', 'Please enter a comment');
	}
	else if ($(".comment", this).val().length < 20) {
		formFail('comment', 'Please write a longer comment');
		$('.validation_message').show();
	}
	else {
		validate($(".comment", this));
	}
	
	if ($(".rating", this).val() == 0) {
		formFail('rating', 'Please choose a star rating');
	}
	else {
		validate($(".rating", this));
	}

	
	if (valid) {
		$("#fancybox-inner .review_status").clone().insertAfter('#fancybox-inner #rating_form').removeClass('error_message').html('<img src="/_img/broadband/brands/loading.gif" alt="Loading..." title="Sending review" />').hide();
		$("#fancybox-inner #rating_form fieldset").fadeOut('fast',function(){
			$("#fancybox-inner .review_status").fadeIn('fast');
		});
		
		$("#fancybox-inner fieldset .review_status").remove();
		postForm('#fancybox-inner #rating_form','#fancybox-inner .review_status','<p><strong>Success! Your review has been submitted.</strong></p><p>We\'ll send you an email as soon as it has been approved by our editors.</p>','<p><strong>There\'s been a problem saving your review.</strong></p><p>Our technical team have been informed so please try again later.</p>');
	}
	return false;
}

/*

function checkForm() {
	var self = this;

	var formFail = function(messageContainer, problem) {
		$('.'+messageContainer, self).html(problem);
		$('.'+messageContainer, self).slideDown('fast');
	}

	if ($(".reviewer_name", this).val()==''||$("#reviewer_name", this).val()==' ') {
		formFail('review_status','Please enter your name');
	}
	else if (!emailPattern.test($(".email", this).val())) {
		formFail('review_status', 'Please enter a valid email address');
	}
	else if ($(".comment", this).val()==''||$(".comment",this).val()==' ') {
		formFail('review_status', 'Please enter a comment');
	}
	else if ($(".comment", this).val().length < 20) {
		formFail('review_status', 'Please write a longer comment');
	}	
	else if ($(".rating", this).val() == 0) {
		formFail('review_status', 'Please choose a star rating');
	}
	else {
		$("#fancybox-inner .review_status").clone().insertAfter('#fancybox-inner #rating_form').removeClass('error_message').html('<img src="/_img/broadband/brands/loading.gif" alt="Loading..." title="Sending review" />').hide();
		$("#fancybox-inner #rating_form fieldset").fadeOut('fast',function(){
			$("#fancybox-inner .review_status").fadeIn('fast');
		});
		
		$("#fancybox-inner fieldset .review_status").remove();
		postForm('#fancybox-inner #rating_form','#fancybox-inner .review_status','<p><strong>Success! Your review has been submitted.</strong></p><p>We\'ll send you an email as soon as it has been approved by our editors.</p>','<p><strong>There\'s been a problem saving your review.</strong></p><p>Our technical team have been informed so please try again later.</p>');
	}
	
	return false;
}
*/