var forgotPassHTML = '<div style="background-color:#FFFFFF;"><form id="forgotLoginForm" class="font08em"><p>Please enter your email address and click submit to retrieve your login details.</p><input class="inputTextBody" type="text" name="uEmail" id="uEmail" onblur="uEmail=this.value"><br></form><input type="submit" value="submit" id="requestLogin"></div>';
var $dialog = $('<div></div>');
var uEmail = $('#uEmail').val();
var fSent;
var uPassword;
var uAnswer;
var uName;
var uCompany;
var uAddress;
var uTel;

function resetDialogue(){
	$dialog.html(forgotPassHTML);
}

$(document).ready(function(){
	
	var mapLoaded = false;
	if(!mapLoaded){
		setTimeout(function(){
			var latlng = new google.maps.LatLng(53.303318, -2.371805);
			var myLatlng = new google.maps.LatLng(53.303318, -2.371805);               
			
			
			var myOptions = {
				zoom: 14,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.HYBRID
			};

			var map = new google.maps.Map(document.getElementById("mapDiv"), myOptions);

			var panoramaOptions = {
				position: myLatlng,
				pov: {
					heading: 85,
					pitch: 0,
					zoom: 1
				},
				visible: false
			};

			var panorama = new google.maps.StreetViewPanorama(document.getElementById("panoramaDiv"), panoramaOptions);
			map.setStreetView(panorama);
			
			var marker = new google.maps.Marker({
				  position: myLatlng, 
				  map: map,
				  title:"Fall Woman"
			 });

			 var streetLatlng = new google.maps.LatLng(53.303318, -2.371805);
			 var street = new google.maps.Marker({
				  position: streetLatlng,
				  map: map,
				  title:"Street View"
			 });
			

			var contentString = '<p class="grey text verdana line-height15">Fall Woman<br/>35 King Street<br/>Knutsford<br/>Cheshire<br/>WA16 6DW<br/><span class="red">street view</span></p>';

			var infowindow = new google.maps.InfoWindow({
				content: contentString
			});


			google.maps.event.addListener(marker, 'click', function() {
			  infowindow.open(map,marker);
			});

			google.maps.event.addListener(street, 'click', function() {
				  panorama.setVisible(true);
				  $('#panoramaDiv').show();
				  $('#mapDiv').hide();
			});

			
		   
			
			mapLoaded = true;
		},1000);
	}
	
	$('.retry').live('click', function(){
		$dialog.html(forgotPassHTML);
	});
	//alert('DOM ready');
	$dialog.html(forgotPassHTML);
	$dialog.dialog({ 
		dialogClass: 'login-dialog',
		autoOpen: false,
		title: 'Request your login details',
		resizable: false
	});
	//getter
	var dialogClass = $dialog.dialog( "option", "dialogClass" );
	//setter
	
	$('.forgotPass').click(function() {
		$dialog.dialog('open');
		$dialog.dialog( "option", "dialogClass", 'login-dialog' );
	});
		
	
	$(".scrollable").scrollable();
	
	 $(document).ready(function() {
        $('.viewport').mouseenter(function(e) {
            $(this).children('a').children('img').animate({ height: '120', left: '0', top: '0', width: '90'}, 100);
            $(this).children('a').children('span').fadeIn(200);
        }).mouseleave(function(e) {
            $(this).children('a').children('img').animate({ height: '120', left: '0', top: '0', width: '90'}, 100);
            $(this).children('a').children('span').fadeOut(200);
        });
		$('.styleguide').mouseenter(function(e) {
            $(this).children('a').children('img').animate({ height: '250', left: '0', top: '0', width: '180'}, 100);
            $(this).children('a').children('span').fadeIn(200);
        }).mouseleave(function(e) {
            $(this).children('a').children('img').animate({ height: '250', left: '0', top: '0', width: '180'}, 100);
            $(this).children('a').children('span').fadeOut(200);
        });
		$('.largeviewport').mouseenter(function(e) {
            $(this).children('a').children('img').animate({ height: '434', left: '0', top: '0', width: '146'}, 100);
            $(this).children('a').children('span').fadeIn(200);
        }).mouseleave(function(e) {
            $(this).children('a').children('img').animate({ height: '434', left: '0', top: '0', width: '146'}, 100);
            $(this).children('a').children('span').fadeOut(200);
        });
    });
	
	$("#hasOfferN").live('click',function(){
       $("#displayOffer").hide();
    });
	
	$("#hasOfferY").live('click',function(){
       $("#displayOffer").show();
    });
	
	$("#displayOffer").hide();
	
	//Show the paging and activate its first link
	$(".paging").show();
	$(".paging a:first").addClass("active");
	//On Click
	$(".paging a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
	
	//Get size of the image, how many images there are, then determin the size of the image reel.
	if($(".window").length>0){
		var imageWidth = $(".window").width();
	}else{
		var imageWidth = $(".window_pr").width();
	}
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging  and Slider Function
	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
	
		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
	
		//Slider Animation
		$(".image_reel").animate({
			left: -image_reelPosition
		}, 500 );
	
	}; 
	
	$('#menu li.top').hoverIntent( function(){
		var findClass = $(this).attr('id');
		//alert(findClass);
		$('.'+findClass).delay(300).fadeIn(200, function() {
			// Animation complete.
		  }); 
	}, function(){
		var findClass = $(this).attr('id');
		//alert(findClass);
		$('.'+findClass).fadeOut(500, function() {
			// Animation complete.
		  }); 
	} );
	
	$('#ytplayer').hoverIntent(function(){
		$(this).delay(300).animate({
			width: '334px',
			height: '321px'
		  }, 500, function() {
			// Animation complete.
		  });
	}, function(){
		$(this).animate({
			width: '133px',
			height: '211px'
		  }, 500, function() {
			// Animation complete.
		  }); 
	} );
	
	/*$('#ytplayer').mouseenter(function(){
		$(this).delay(300).animate({
			width: '334px',
			height: '321px'
		  }, 500, function() {
			 Animation complete.
		  }); 
	});
	$('#ytplayer').mouseleave(function(){
		$(this).animate({
			width: '133px',
			height: '211px'
		  }, 500, function() {
			 Animation complete.
		  }); 
	});*/
	
	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = $('.paging a.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (7 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	
	$("#loginBtn").live('click',function(){
		submitForm('customerLogin', 'response');
    });
	
	
	$("#professionalSubmit").live('click',function(){
        //handleEnquiryRequest();
		//vars = [uName, uEmail, uTel, uEnquiry];
		//els = ['.uName', '.uEmail', '.uTel', '.uEnquiry'];
		submitForm('valuationForm', 'enquiryForm');
    });
	

	
	
	function submitForm(formID, responseDiv){	
			
		//alert(formID);
		
		setVars(formID);
		
		if(checkRequired(formID)==true){
			
			var args = '?uEmail='+uEmail+'&uPassword='+uPassword+'&fSent='+fSent+'&uName='+uName+'&uCompany='+uCompany+'&uAddress='+uAddress+'&uTel='+uTel+'&uEmail='+uEmail+'&uAnswer='+uAnswer;
			
			//document.location = 'phpScripts/form_processor.php'+args;
			//prompt("","/ajaxFormProcessor.php"+args);
			
			$('#'+responseDiv).html('Processing <img src="images/ajax-loader.gif" />');
			
			$.ajax({
				type: "GET",
				url: "ajaxFormProcessor.php"+args,
				dataType: 'xml',
				success: function(xml){
					var messages = ($(xml).find('message').length);
					
					//alert(messages);
					$(xml).find('message').each(function(){
														 
							eStatus = $(this).find('eStatus').text();
							//alert(eStatus);
							resp = $(this).find('response').text();
							//alert(response);
							//prompt('',$(this).find('sql').text());
							//prompt('',$(this).find('query').text());
					});
					
					$('#'+responseDiv).html(resp);
					
					if(eStatus!='fail'){
						$('#'+responseDiv).css('color','green');
						$('#customerLogin').submit();
						//$('#'+formID).siblings('.loginBtn').css('display','none');
						//$('#'+formID).css('display','none');
					}else{
						$('#'+responseDiv).css('color','red');
						$('#'+formID).siblings('.loginBtn').css('display','inline');
						$('#'+formID).css('display','inline');
					}
				}
				
			});
	
		}
		
	}
	
	
	
	function checkRequired(formID){
		var checkRequired = true;
		
		var requiredFields = $('#'+formID).find('.required');
		//alert(requiredFields.length);
		
		var message = 'Please fill in all required fields\n(Highlighted in Red)\n\n';
	
		for(i=0; i<requiredFields.length; i++){
			//alert(requiredFields[i].id+' : '+requiredFields[i].value);
			if(requiredFields[i].value==""){
				$('#'+formID).find('.'+requiredFields[i].id).css('color','red');
				//alert($('#'+formID).find('.'+requiredFields[i].id).text());
				message += $('#'+formID).find('.'+requiredFields[i].id).text()+'\n';
				checkRequired = false;
			}else{
				$('.'+requiredFields[i].id).css('color','#909191');
			}
			
		}
			
		if(checkRequired==false){
			alert(message);
		}
		
		return checkRequired;
	}

});

$('#requestLogin').live('click', function(){
        	
	setVars('forgotLoginForm');
	
	if(!uEmail || uEmail==''){
		//alert(uEmail);
		alert('Please enter your email address that you have registered with us');
	}else{
		$dialog.html('<p class="font08em"><br/><br/>Please wait while we find your details...</p>');
		
		var args = '?uEmail='+uEmail+'&fSent=requestPass';
		//prompt('',"ajaxFormProcessor.php"+args);
		$.ajax({ 
			url: "ajaxFormProcessor.php"+args, 
			dataType: 'xml',  
			success: function(xml){
				var resp;
				$(xml).find('message').each(function(){
					resp = $(this).find('response').text();
				});
				$dialog.html(resp);
		}});
	}
});
	
$('#contactBtn').live('click', function(){
        	
	setVars('enquiryForm');
	
	if(!uEmail || uEmail==''){
		//alert(uEmail);
		alert('Please enter your email address');
	}else{
		$('#enquiryForm').html('<p class="font08em"><br/><br/>Please wait while we send your message...</p>');
		
		var args = '?uName='+uName+'&uAddress='+uAddress+'&uTel='+uTel+'&uEmail='+uEmail+'&uEnquiry='+uEnquiry+'&fSent=contactEnquiry';
		//prompt('',"ajaxFormProcessor.php"+args);
		$.ajax({ 
			url: "ajaxFormProcessor.php"+args, 
			dataType: 'xml',  
			success: function(xml){
				var resp;
				$(xml).find('message').each(function(){
					resp = $(this).find('response').text();
				});
				$('#enquiryForm').html(resp);
		}});
	}
});

	
function setVars(formID){
		//alert($("#"+formID+' #uName').val());
		uEmail = $("#"+formID+' #uEmail').val();
		fSent = $("#"+formID+' #fSent').val();
		uPassword = $("#"+formID+' #uPassword').val();
		uTel  = $("#"+formID+' #uTel').val();
		uName = $("#"+formID+' #uName').val();
		uCompany = $("#"+formID+' #uCompany').val();
		uAddress = $("#"+formID+' #uAddress').val();
		uEnquiry = $("#"+formID+' #uEnquiry').val();
	
}


function bookmarkPage(url, title){ if (document.all) window.external.AddFavorite(url, title); else if (window.sidebar) window.sidebar.addPanel(title, url, "") }


function setBorder(obj){
	$('.thumbContainer img').css({'border-style':'solid','border-color':'#FFFFFF','border-weight':'4px'});
	$(obj).css({'border-style':'solid','border-color':'#e3dedd','border-weight':'4px'});
}

function showDescription(num){
		//Hide all
		
		$('.descriptions').addClass('hidden');
		//Show active
		$('#description_'+num).removeClass('hidden');
}

function setImage(img){
	var imgSRC = 'phpThumb/phpThumb.php?src=../'+img+'&wl=292&hp=292';
	//alert(imgSRC);
	document.getElementById('mainImg').src=imgSRC;
}

