

$(document).ready(function(){

	setupForm();
	pngfix();
	linkswipe();
	Galleria.loadTheme('js/jQuery/galleria/themes/classic/galleria.classic.min.js');

	$('a[rel="lightbox"]').colorbox();

	var targ;

	$("#gallery").galleria({
		autoplay: 6000,
		transitionSpeed: 1000,
		transitionInitial: 'fade',
        width: 958,
        height: 443,
		imageCrop: 'width',
		minScaleRatio: 1,
		extend: function() {
            this.bind(Galleria.IMAGE, function(e) {

                $(e.imageTarget).css('cursor','pointer');

			    $(e.imageTarget).click(this.proxy(function() {

					targ = $(e.imageTarget).attr('src');

					$('a[href="'+targ+'"]').click();

			    }));

                //$(e.imageTarget).css('cursor','pointer').click(this.proxy(function() { this.openLightbox(); }));

            });
			
        }

    });

	$('body').css('visibility','visible');
	$('body').hide().fadeIn('slow');

/***test***/
	$('a span#enquiry').toggle(
		function(){
			$('#contactMain').hide();
			$('#contactForm').slideToggle(
				500, function(){
					$('#contactMain').fadeIn('fast');
					//setupForm();
				}
			)
		},
		function(){
			$('#contactMain').fadeOut('fast', function(){
				$('#contactForm').slideToggle(
					500
				)
			});
		}
	);
/**********/

	/*$('a span#enquiry').toggle(
		function(){
			$('#contactMain').hide();
			$('#contactForm').animate(
				{ 'margin-top':'0' },	
				500, function(){
					$('#contactMain').fadeIn('fast');
					setupForm();
				}
			)
		},
		function(){
			$('#contactMain').fadeOut('fast', function(){
				$('#contactForm').animate(
					{ 'margin-top':'-303px' },	
					500
				)
			});
		}
	);*/

	//$('a span#enquiry').click();

});

/** setup forms  ***********************/

function setupForm(){
	$('form').submit(function(evt) {

		evt.preventDefault();
		processForm();
	});
}

/** process forms  ***********************/

function processForm(){

	var cname = $('form input[name=name]').attr('value');
	var address = $('form input[name=address]').attr('value');
	var pcode = $('form input[name=postcode]').attr('value');
	var tele = $('form input[name=telephone]').attr('value');
	var email = $('form input[name=email]').attr('value');
	if (checkNotEmpty(email)){
		
		var atpos=email.indexOf("@");
		var dotpos=email.lastIndexOf(".");
		if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length){
		  alert("Not a valid e-mail address!\n Please remove it or enter a new one.");
		  return false;
		}
	}
	var windows = ''+$('form input[name=windows]').attr('checked');
	var doors = ''+$('form input[name=doors]').attr('checked');
	var patio_doors = $('form input[name=patio_doors]').attr('checked');
	var kitchens = ''+$('form input[name=kitchens]').attr('checked');
	var conservatories = ''+$('form input[name=conservatories]').attr('checked');
	var time = $('form input[name=time]:checked').attr('value');
	var cmessage = $('form textarea[name=message]').attr('value');

	if (mandatoryFields(cname,email)){

		var myRequest = getXMLHTTPRequest();

		url = "sendCurl.php?";
		url += "name="+cname;
		url += "&address="+address;
		url += "&pcode="+pcode;
		url += "&phone="+tele;
		url += "&email="+email;
		url += "&windows="+windows;
		url += "&doors="+doors;
		url += "&patio_doors="+patio_doors;
		url += "&kitchens="+kitchens;
		url += "&conservatories="+conservatories;
		url += "&time="+time;
		url += "&message="+cmessage;

		url = url.toLowerCase();

		myRequest.open("GET",url,true);

		myRequest.onreadystatechange = function(){

			if (myRequest.readyState == 4){

				if (myRequest.status == 200){

					//console.log(myRequest.response);

					$('a span#enquiry').click();

				} else {

					$('a span#enquiry').click();

				}
			}

		}

		myRequest.send(null);
	
	} else {

	alert('Please ensure you have entered your full name and an email address!');

	}

}

function checkNotEmpty(element){

	if (element==null || element==""){
		return false;
	}
	return true;
}

function mandatoryFields(element1,element2){

	if (!checkNotEmpty(element1) ||
		!checkNotEmpty(element2)){
	  return false;
	}
	return true;
}


function getXMLHTTPRequest(){
	var req = false;
	try{
		req = new XMLHttpRequest(); /* firefox */
	}
	catch(err1){
		try{
			req = new ActiveXObject("Msxm12.XMLHTTP"); /* some IE */
		}
		catch(err2){
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP"); /* some IE */
			}
			catch(err3){
				req = false;
			}
		}
	}
return req;
}


function linkswipe() {

     $("span#viewDoors").each(function(i) {
          // make the iteration object a jquery object
          var a = $('span#viewDoorsSmall');
          // grab the text
          var title = a.html();
          // and the width
          var w = a.outerWidth();
          // append a span with the content in, to mask over the link      
          a.append("<span class='mask' style='width: 0; display: none;font-size:19px;line-height:36px;'>"+title+"</span>");
          // store a reference to the span
          var span = a.find("span");
          // do the hover funcs
          $("span#viewDoors").hover(function(){
              // stop the flicker!
              span.stop(true,true);
              //
              span.show().animate({
                width: w+'px'
              }, 200);
          }, function() {
              span.animate({
                width: '0px'
              }, 400, function(){ 
                  span.hide();          
              });
          });
      });    	

      $("span#enquiry").each(function(i) {
          // make the iteration object a jquery object
          var a = $('span#eOnline');
          // grab the text
          var title = a.html();
          // and the width
          var w = a.outerWidth();
          // append a span with the content in, to mask over the link      
          //a.append("<span class='mask' style='width: 0; display: none;font-size:20px;line-height:23px;'>"+title+" &rArr;</span>");
          a.append("<span class='mask' style='width: 0; display: none;font-size:20px;line-height:23px;'>"+title+"</span>");
          // store a reference to the span
          var span = a.find("span");
          // do the hover funcs
          $("span#enquiry").hover(function(){
              // stop the flicker!
              span.stop(true,true);
              //
              span.show().animate({
                //width: w+25+'px'
                width: w+'px'
              }, 200);
          }, function() {
              span.animate({
                width: '0px'
              }, 400, function(){ 
                  span.hide();          
              });
          });
      });    	
    }



/* png fix */
this.pngfix=function(){var ie55=(navigator.appName=="Microsoft Internet Explorer"&&parseInt(navigator.appVersion)==4&&navigator.appVersion.indexOf("MSIE 5.5")!=-1);var ie6=(navigator.appName=="Microsoft Internet Explorer"&&parseInt(navigator.appVersion)==4&&navigator.appVersion.indexOf("MSIE 6.0")!=-1);if(jQuery.browser.msie&&(ie55||ie6)){$("*").each(function(){var bgIMG=$(this).css('background-image');if(bgIMG.indexOf(".png")!=-1){var iebg=bgIMG.split('url("')[1].split('")')[0];$(this).css('background-image','none');$(this).get(0).runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+iebg+"',sizingMethod='crop')"}})}};

