$(document).ready(function() {
    $(document).pngFix(); 
	$('a[rel="external"]').each(function () {
		$(this).attr({'target':'_blank'});
		$(this).attr({'title':'This link opens in a new window/tab'});
	});
	
	$('.searchform').validate();
	$('.newsletterform').validate({
		errorLabelContainer: ".messagebox",
		submitHandler: function(form) {
   			// bind to the form's submit event 
        	// inside event callbacks 'this' is the DOM element so we first 
        	// wrap it in a jQuery object and then invoke ajaxSubmit 
        	$('.newsletterform').ajaxSubmit({
				target:			'.newsletterform',   // target element(s) to be updated with server response 
				beforeSubmit: 	showRequest,   // pre-submit callback 
				success:		showResponse  // post-submit callback 
 
        		// other available options: 
        		//url:       url         // override for form's 'action' attribute 
        		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
        		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        		//clearForm: true        // clear all form fields after successful submit 
        		//resetForm: true        // reset the form after successful submit 
 
        		// $.ajax options can be used here too, for example: 
        		//timeout:   3000
			}); 
 
        	// !!! Important !!! 
        	// always return false to prevent standard browser submit and page navigation 
        	return false; 
   		}
	});
	Shadowbox.init();
	scroller();
	tabs();
	
	$('.enquiryform').validate({
		submitHandler: function(form) {
   			// bind to the form's submit event 
        	// inside event callbacks 'this' is the DOM element so we first 
        	// wrap it in a jQuery object and then invoke ajaxSubmit 
        	$('.enquiryform').ajaxSubmit({
				target:			'.enquiryform',   // target element(s) to be updated with server response 
				beforeSubmit: 	showRequest,   // pre-submit callback 
				success:		showResponse  // post-submit callback 
 
        		// other available options: 
        		//url:       url         // override for form's 'action' attribute 
        		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
        		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        		//clearForm: true        // clear all form fields after successful submit 
        		//resetForm: true        // reset the form after successful submit 
 
        		// $.ajax options can be used here too, for example: 
        		//timeout:   3000
			}); 
 
        	// !!! Important !!! 
        	// always return false to prevent standard browser submit and page navigation 
        	return false; 
   		}
	});
	
	

	
});

function showRequest(formData, jqForm, options) {
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData);
	
	// sets up a transparent background to hold a loading graphic
	var OverlayObject = document.createElement("div");
	OverlayObject.style.width = $('.contenttab4').width()+20+"px";
	OverlayObject.style.height = $('.contenttab4').height()+20+"px";
	OverlayObject.style.left = '0px';
	OverlayObject.style.top =  '0px';
	OverlayObject.style.position = "absolute";
	OverlayObject.style.backgroundColor = "#000000";
	OverlayObject.style.zIndex = "2";
	OverlayObject.style.filter = "alpha(opacity=20)";
	OverlayObject.style.mozOpacity = ".20";
	OverlayObject.style.opacity = ".20";
	OverlayObject.style.display = "block";
	OverlayObject.className = "ajaxfade";
	$('.contenttab4').append(OverlayObject);
	$('.contenttab4').append('<img src="./_images/loading-gif.gif" alt="Loading Graphic" title="" class="loading-graphic" />');
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
function showResponse(responseText, statusText)  { 
	$('.ajaxfade').remove();
	$('.loading-graphic').remove();
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
}

function returnbutton() {
	$('.returnbutton').each(function(i){
		$(this).click(function(){
			$('.enquiryform').html(enquiryinnerhtml);
			$('.enquiryform > *').val(0);
		});
	});
}

var firstimg = new Image();
firstimg.onload = function() {
	$('.mediumscrollcontainer').css({'height' : (200/firstimg.width)*firstimg.height});
};

var globalimg = "";
var scrollpos = 0;
var curpos = 0;
var imgwidth = 70;
var totalimg = 0;
var curimage = 0;
scroller = function() {
	firstimg.src = $('.mediumscrollbox > a > img:first').attr('src');
	$('.mediumscrollcontainer').css({'height' : $('.mediumscrollbox > a > img:first').height()});
	$('.scrollbox > img').each(function(i){
		totalimg = totalimg + 1;
		$(this).click(function(){
			leftpos = $(this).attr('src').substring($(this).attr('src').length - 5, $(this).attr('src').length - 4);
			leftpos = -200*i;
			curmedimg = ((leftpos/200)*-1);
			curmedimg = $('.mediumscrollbox > a > img:nth('+curmedimg+')').height();
			$('.mediumscrollbox').css({'left' : leftpos});
			$('.mediumscrollcontainer').css({'height' : curmedimg});
		});
	});
	$('.scrollbox').css({'width' : totalimg*70});
	$('.mediumscrollbox').css({'width' : totalimg*200});
	$('.scrollprev').click(function(){
		if (curimage > 0) {
			scrollpos = scrollpos + imgwidth;
			scrolldiv();
			curimage = curimage - 1;
		}
		scrollbuttons();
	});
	$('.scrollnext').click(function(){
		if (curimage < (totalimg - 3)) {
			scrollpos = scrollpos - imgwidth;
			scrolldiv();
			curimage = curimage + 1;
		}
		scrollbuttons();
	});
	scrollbuttons();
}

scrolldiv = function() {
	if (Math.abs(curpos-scrollpos)<6) {
		curpos = scrollpos;
    } else {
		curpos += (curpos<scrollpos ? 6 : -6);
		setTimeout('scrolldiv()',10);
    }
	$('.scrollbox').css({'left' : curpos+'px'});
}

scrollbuttons = function() {
	if (curimage == 0) {
		$('.scrollprev > img').attr('src', '_images/icon-arrow-left-grey.jpg');
		$('.scrollprev > span').css({'color' : '#999999'});
	} else {
		$('.scrollprev > img').attr('src', '_images/icon-arrow-left.jpg');
		$('.scrollprev > span').css({'color' : '#F59322'});
	}
	if (curimage >= (totalimg - 3)) {
		$('.scrollnext > img').attr('src', '_images/icon-arrow-right-grey.jpg');
		$('.scrollnext > span').css({'color' : '#999999'});
	} else {
		$('.scrollnext > img').attr('src', '_images/icon-arrow-right.jpg');
		$('.scrollnext > span').css({'color' : '#F59322'});
	}
}

tabs = function() {
	pageanchor = window.location.href;
	pageanchor = pageanchor.split("#");
	$('.tab').each(function(i){$(this).removeClass('active')});
	if (pageanchor[1] == "pricing") {
		$('.contenttab').each(function(){$(this).css({'display' : 'none', 'visibilty' : 'hidden'})});
		$('.contenttab3').css({'display' : 'block', 'visibilty' : 'visible'});
		$('.tab3').addClass('active');
	} else if (pageanchor[1] == "enquire") {
		$('.contenttab').each(function(){$(this).css({'display' : 'none', 'visibilty' : 'hidden'})});
		$('.contenttab4').css({'display' : 'block', 'visibilty' : 'visible'});
		$('.tab4').addClass('active');
	} else {
		$('.contenttab').each(function(){$(this).css({'display' : 'none', 'visibilty' : 'hidden'})});
		$('.contenttab1').css({'display' : 'block', 'visibilty' : 'visible'});
		$('.tab1').addClass('active');
	}
	$('.tabs > div').each(function(i){
		$(this).click(function(){
			tab = $(this).attr('class').substring(7, 8);
			$('.tab').each(function(i){$(this).removeClass('active')});
			$(this).addClass('active');
			$('.contenttab').each(function(){$(this).css({'display' : 'none', 'visibilty' : 'hidden'})});
			$('.contenttab'+tab).css({'display' : 'block', 'visibilty' : 'visible'});
		})
	});
}



function changeMonth()
    {
     var tempArr=document.getElementById("enquiryformform").enqmonth.options[document.getElementById("enquiryformform").enqmonth.selectedIndex].value.split("|");
     var ThisMonth = parseInt(tempArr[0],10);
     var ThisYear = parseInt(tempArr[1],10);
     var DaysInMonth = new Array(31, ((ThisYear % 4 == 0 && ThisYear % 100 != 0) || ThisYear % 400 == 0 ? 29 : 29), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
     ThisDay = document.getElementById("enquiryformform").enqdepday.value;
     for (;document.getElementById("enquiryformform").enqdepday.length>28;)
       document.getElementById("enquiryformform").enqdepday.remove(document.getElementById("enquiryformform").enqdepday.length-1);
     for (;document.getElementById("enquiryformform").enqdepday.length<DaysInMonth[ThisMonth-1];)
      {
       document.getElementById("enquiryformform").enqdepday[document.getElementById("enquiryformform").enqdepday.length] = new Option(document.getElementById("enquiryformform").enqdepday.length+1,document.getElementById("enquiryformform").enqdepday.length+1);
      }
     if (ThisDay>DaysInMonth[ThisMonth-1])
      {
       document.getElementById("enquiryformform").enqdepday.value = DaysInMonth[ThisMonth-1];
      }
     else
      {
       document.getElementById("enquiryformform").enqdepday.value = ThisDay;
      }

     document.getElementById("enquiryformform").enqmonth.value=tempArr[0];
     document.getElementById("enquiryformform").enqyear.value=tempArr[1]
    }

   function DurationChange()
    {
     if (document.getElementById("enquiryformform").enqdur.value==0)
      {
       document.getElementById("DurationOther").style.visibility = "visible";
	   document.getElementById("DurationOther").style.display = "block";
      }
     else
      {
       
	   document.getElementById("DurationOther").style.visibility = "hidden";
	   document.getElementById("DurationOther").style.display = "none";
      }
    }
   function FlexibilityChange()
    {
     if (document.getElementById("enquiryformform").enqflex.value==0)
      {
       document.getElementById("FlexibilityOther").style.visibility = "visible";
	   document.getElementById("FlexibilityOther").style.display = "block";
      }
     else
      {
	   document.getElementById("FlexibilityOther").style.visibility = "hidden";
	   document.getElementById("FlexibilityOther").style.display = "none";
      }
    }


   function WhereHear()
    {
     if (document.getElementById("enquiryformform").form_wheredidyouhear.value=="Other Online")
      {
       document.getElementById("WhereHearOther").style.visibility = "visible";
	   document.getElementById("WhereHearOther").style.display = "block";
      }
     else
      {
       document.getElementById("WhereHearOther").style.visibility = "hidden";
	   document.getElementById("WhereHearOther").style.display = "none";
      }
    }
	
	