$(function(){
	var myBody = $("body");
	myBody.addClass("js");
	var pid = myBody.attr("id");
	
	initSlideShow(pid);
	initMap(pid);
	initTabs(pid);
	initColorBox(pid);
	slideToggle(pid);
	ValidateForm(pid);
	initAccordion(pid);
});

function initTabs(pid){
	if(pid=="services-it-management-remote-backup" || pid=="services-web-development-e-commerce" || pid=="showroom-pricelist" || pid=="services-internet-broadband" || pid=="technical-support-sagepay-setup")
	{
		$("#tabs").tabs();
	}
}

function ValidateForm(pid){
	if ($("#CallBack").length ) 
	{
	$("#CallBack").validate({
			rules: {
				cb_name: "required",
				cb_phone: "required"
			},
			messages: {
				cb_name: "Please enter your Name",
				cb_phone: "Please enter your Phone Number"
			}
		});
		$("#Enquiry").validate({
			rules: {
				qe_name: "required",
				qe_email: "required",
				qe_phone: "required",
				qe_message: "required"
			},
			messages: {
				qe_name: "Please enter your name",
				qe_email: "Please enter your email address",
				qe_phone: "Please enter your phone number",
				qe_message: "Please enter your message"
			}
		});
	}
	
	if(pid=="about-us-survey")
	{
		$("#feedbackform").validate({
			rules: {
				n: "required",
				em: "required",
			},
			messages: {
				n: "Please enter your name",
				em: "Please enter your email address",
			}
		});
	}
}


function slideToggle(pid){
	if(pid=="services-midland-web-design-e-commerce" || "services-midland-internet-shared-hosting")
	{
		$('img.slidecontrol').click(function(){
			var $this = $(this);
			var divID = $this.attr("id").replace("slidecontrol_", "slidedisplay_");
			var $div = $('#'+divID);
			if ($div.is(':visible')) {
				$div.slideUp(500);
			} else {
				$div.slideDown(500);
			}
			return false;
		});
	}
}


function initSlideShow(pid){
	if(pid=="home"){
		$('#hs-show').cycle({
			next: '#hs-next',
			prev: '#hs-prev',
			timeout: 8000,
			pause : true,
			sync: true
		});
		
		$('#ns-show').cycle({
			timeout: 3000,
			pause : true,
			sync: false
		});
	}
}

function initMap(pid)
{
	if(pid=="contact-map"){				
		    var map = new GMap2(document.getElementById('map'));
		    var burnsvilleMN = new GLatLng(52.6514, -2.422);
		    map.setCenter(burnsvilleMN, 13);
		    map.openInfoWindowHtml(map.getCenter(), "<b>Midland Computers is here:</b> <br />Unit 4, Sovereign Park, Halesfield 24, Telford, TF7 4NZ<br /><br />Tel: 01952 588688<br />Email: showroom@midlandcomputers.com");
	}
}

function initColorBox(pid){
	if(pid == 'services-software-cloud-services')
	{
		$(".example7").colorbox({width:"85%", height:"85%", iframe:true});
	}
	else if(pid == 'showroom')
	{
		$("a[rel='example1']").colorbox({slideshow:false});
	}
	else if(pid == "services-engineer-services-engineer-profiles")
	{		
		$("#inline1").colorbox({width:"710px", inline:true, href:"#inline_text1"});
		$("#inline2").colorbox({width:"710px", inline:true, href:"#inline_text2"});
		$("#inline3").colorbox({width:"710px", inline:true, href:"#inline_text3"});
		$("#inline4").colorbox({width:"710px", inline:true, href:"#inline_text4"});
		$("#inline5").colorbox({width:"710px", inline:true, href:"#inline_text5"});
		$("#inline6").colorbox({width:"710px", inline:true, href:"#inline_text6"});
		$("#inline7").colorbox({width:"710px", inline:true, href:"#inline_text7"});
		$("#inline8").colorbox({width:"710px", inline:true, href:"#inline_text8"});
		$("#inline9").colorbox({width:"710px", inline:true, href:"#inline_text9"});
	}
}

function initValidation(pid){
	if(pid == 'sendurlto'){
		$("#form_dosendurlto").validate({
			rules: {
				addr: {
					required: true,
					email: true
				}
			}
		});
	}else if(pid == 'login'){
		$("#form_login").validate({
			rules: {
				Boatshed_Login_Email: {
					required: true,
					email: true
				}
			}
		});
	}
}

function initAccordion(pid)
{
	if (pid == 'services-design-design-services')
	{
		//Set default open/close settings
		$('.acc_container').hide(); //Hide/close all containers
		$('.acc_trigger:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
		
		//On Click
		$('.acc_trigger').click(function(){
			if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
				$('.acc_trigger').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
				$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
			}
			return false; //Prevent the browser jump to the link anchor
		});
	}
}
