$(function() {
	
	$('a[rel*=external]').click(function() {
		window.open(this.href);
		return false;
	});
	
	$('nav#top ul').superfish({
		delay: 400,
		animation: {opacity:'show',height:'show'},
		speed: 'fast',   
		autoArrows: false,
		dropShadows: false           
	});
	
	$('nav#left ul:not(".sfIgnore")').superfish({
		delay: 400,
		animation: {opacity:'show',height:'show'},
		speed: 'fast',   
		autoArrows: false,
		dropShadows: false           
	});
	
	$('input[type=text], #channel-partners textarea, #order #notes').each(function() {
		
		$(this).addClass('dim');
		var default_value = this.value;
		
		$(this).focus(function(){
			if(this.value == default_value) {
				this.value = '';
			}
			$(this).removeClass('dim');
		});
		
		$(this).blur(function(){
			if(this.value == '') {
			   this.value = default_value;
			   $(this).addClass('dim');
			}
		});
		
	});
		
});

function mycarousel_initCallback(carousel) {
    carousel.buttonNext.bind('click', function() {carousel.startAuto(0);});
    carousel.buttonPrev.bind('click', function() {carousel.startAuto(0);});
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

$(document).ready(function(){
	
	$('#imageSlider').jcarousel({
		start: 1,
		auto: 5,
		scroll: 1,
		animation: 500,
		wrap: 'circular'
		//initCallback: mycarousel_initCallback
	});
	
	$('.expander').hide();
	$('#id2').hide();

	$('.showAll').toggle(function(){
		$(this).next().slideToggle('slow');
		$(this).find('.showRight').text('Hide all').toggleClass('showing');
	}, function(){
		$(this).next().slideToggle('slow');
		$(this).find('.showRight').text('Show all').toggleClass('showing');
	});
	
	current = 1;
	
	$('.tabWrap span:nth-child(1)').click(function(){
		current = 1;
		$('.tabWrap span:nth-child(2)').removeClass('current');
		$('.tabWrap span img').attr('src','images/arrow-out.png');
		$(this).addClass('current');
		$(this).find('img').attr('src','images/arrow-down.png');
		$('#id2').hide();
		$('#id1').show();
	});
	
	$('.tabWrap span:nth-child(2)').click(function(){
		current = 2;
		$('.tabWrap span:nth-child(1)').removeClass('current');
		$('.tabWrap span img').attr('src','images/arrow-out.png');
		$(this).addClass('current');
		$(this).find('img').attr('src','images/arrow-down.png');
		$('#id1').hide();
		$('#id2').show();
	});
	
	$('.tabWrap span:nth-child(1)').hover(
		function () {
			$('.tabWrap span:nth-child(2)').removeClass('current');  
			return false;
		}, 
		function () {
			if (current==2) {
		 		$('.tabWrap span:nth-child(2)').addClass('current');
			}
		}
	);
	
	$('.tabWrap span:nth-child(2)').hover(
		function () {
			$('.tabWrap span:nth-child(1)').removeClass('current');  
			return false;
		}, 
		function () {
			if (current==1) {
		 		$('.tabWrap span:nth-child(1)').addClass('current');
			}
		}
	);
	
	$('.noclick').click(function(){
		return false;
	});
	
	var currentPage = $('body').attr('id') + ".php";
	$('nav#left a, nav#top a').each(function() {
		if($(this).attr('href') == currentPage) {
			$(this).parent().addClass("active");
			$(this).parent().parent().parent().addClass("current");
			$(this).parent().parent().parent().parent().addClass("forced");
		}
	});
		
	$('.btnPrint').click(function() {
		window.print();
		return false;
	});
	
	// Form validation - Quick Quote
	$("#quick-quote .btnSendEnquiry").click(function() {
	
		if ($("input#Name").val() == "") {
			$("input#Name").parent().addClass("invalid");
			$("input#Name").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Name").parent().removeClass("invalid");
		}
		
		if ($("input#Email").val() == "") {
			$("input#Email").parent().addClass("invalid");
			$("input#Email").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Email").parent().removeClass("invalid");
		}
		
		if ($("input#Telephone").val() == "") {
			$("input#Telephone").parent().addClass("invalid");
			$("input#Telephone").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Telephone").parent().removeClass("invalid");
		}
		
		if ($("input#Building").val() == "") {
			$("input#Building").parent().addClass("invalid");
			$("input#Building").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Building").parent().removeClass("invalid");
		}
		
		if ($("input#Postcode").val() == "") {
			$("input#Postcode").parent().addClass("invalid");
			$("input#Postcode").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Postcode").parent().removeClass("invalid");
		}
		
		if ($("input#Bandwidth").val() == "") {
			$("input#Bandwidth").parent().addClass("invalid");
			$("input#Bandwidth").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Bandwidth").parent().removeClass("invalid");
		}
	
	});
	
	// Form validation - Contact us / Channel Partners / Order / Order (Broadband)
	$(".contactForm .btnSendEnquiry").click(function() {
	
		if ($("input#Name").val() == "") {
			$("input#Name").parent().addClass("invalid");
			$("input#Name").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Name").parent().removeClass("invalid");
		}
		
		if ($("input#Email").val() == "") {
			$("input#Email").parent().addClass("invalid");
			$("input#Email").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Email").parent().removeClass("invalid");
		}
		
		if ($("input#Telephone").val() == "") {
			$("input#Telephone").parent().addClass("invalid");
			$("input#Telephone").focus();
			$(".error").fadeIn('fast');
			return false;
		} else {
			$("input#Telephone").parent().removeClass("invalid");
		}
	
	});
	
	// Product order list comparison
	function getUrlVars()
	{
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	}
	
	var product = decodeURI(getUrlVars()["product"]);
	//alert(product);
	
	$('select#product option[selected]').removeAttr("selected");
	$('select#product option[value="'+product+'"]').attr("selected", "selected");
	
	// IE fixes »
	if ($.browser.msie && $.browser.version <= 8 ) {
		
		$(".supplierLogos img:nth-child(5)").css("margin-right","0");
		$("nav#bottom ul li:last-child").css("border","none");
		$("aside p:last-child").css("line-height","15px");
		
		$("tr th:last-child, tr td:last-child").css("width","115px");
		$("tr th:last-child, tr td:last-child").css("border-right","none");
		$("tbody tr:last-child th, tbody tr:last-child td, nav#top ul li:last-child").css("background","none");
		$("tbody tr:last-child th, tbody tr:last-child td").css("height","38px");
		$("tbody tr:last-child th, tbody tr:last-child td").css("padding","0");
		$("tbody tr:last-child th, tbody tr:last-child td").css("border","none");
	
	}
	
});
