﻿/*************/

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus=new Array();
popupStatus["#popupContact"]=0;
popupStatus["#popupPreview"]=0;
popupStatus["#popupAdvert"]=0;

//loading popup with jQuery magic!
function loadPopup(popup){
	if (popup=="#popupContact") {
		if(popupStatus["#popupContact"]==0){
			jQuery(".backgroundPopup").css({
				"background": "#000000",
				"opacity": "0.7"				
			});
			jQuery(".backgroundPopup").stop().fadeIn("slow");
			jQuery(popup).fadeIn("slow");
			popupStatus["#popupContact"] = 1;
		}
	} else if (popup=="#popupPreview") {
		if(popupStatus["#popupPreview"]==0) {
			jQuery('object').hide();
			jQuery(".backgroundPopup").css({
				"background": "#000000",
				"opacity": "0"
			});

			jQuery(".backgroundPopup").stop().fadeIn("slow");
			jQuery(popup).fadeIn("slow");
			popupStatus["#popupPreview"] = 1;
		}
	} else if (popup=="#popupAdvert") {
		if(popupStatus["#popupAdvert"]==0){
		  jQuery('object').hide();
			jQuery(".backgroundPopup").css({
				"background": "#FFFFFF",
				"opacity": "1"

			});


			jQuery(".backgroundPopup").stop().fadeIn("slow");
			jQuery(popup).fadeIn("slow");
			popupStatus["#popupAdvert"] = 1;
		}
	}

	//loads popup only if it is disabled
/*
	if(popupStatus==0) {
		if(popup=="#popupAdvert") {
			jQuery(".backgroundPopup").css({
				"background": "#FFFFFF",
				"opacity": "1"				
			});
		});
		jQuery(".backgroundPopup").fadeIn("slow");
		jQuery(popup).fadeIn("slow");
		popupStatus = 1;
	}
*/

}

//disabling popup with jQuery magic!
function disablePopup(popup){
	if(popup=="#popupAdvert") {
		if(popupStatus["#popupAdvert"]==1) {
			fadeOut("slow");
			jQuery(".backgroundPopup").css({"display":"none"});
			jQuery("#popupAdvert").css({"display":"none"});
			jQuery(popup).fadeOut("slow");
		  jQuery('object').show();
			popupStatus["#popupAdvert"]=0;
		}
	} else if(popup=="#popupPreview") {
		if(popupStatus["#popupPreview"]==1) {
			jQuery(".backgroundPopup").fadeOut("slow");
			jQuery(popup).fadeOut("slow");
			popupStatus["#popupPreview"]=0;
		}
	} else if(popup=="#popupContact") {
		if(popupStatus["#popupContact"]==1) {
			jQuery(".backgroundPopup").fadeOut("slow");
			jQuery(popup).fadeOut("slow");
			popupStatus["#popupContact"]=0;
		}
	}
/*
	//disables popup only if it is enabled
	if(popupStatus==1){
		jQuery(".backgroundPopup").fadeOut("slow");
		jQuery(popup).fadeOut("slow");
		popupStatus = 0;
	}
*/
}

//centering popup
function centerPopup(popup){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery(popup).height();
	var popupWidth = jQuery(popup).width();
	//centering
	if(popup=="#popupAdvert") {
		jQuery(popup).css({
			"position": "fixed",
			"top": (windowHeight/2-popupHeight/2)-25,
			"left": windowWidth/2-popupWidth/2
		});
	} else if(popup=="#popupPreview") {
		jQuery(popup).css({
			"position": "fixed",
			"top": (windowHeight/2-popupHeight/2)-25,
			"left": windowWidth/2-popupWidth/2
		});
	} else jQuery(popup).css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	jQuery(".backgroundPopup").css({
		"height": windowHeight
	});
	
}

document.write("<script type='text/javascript' src='js/cookies_script.js'></script>");

//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function() {
			
	/*
	//LOADING POPUP
	//Click the button event!
	jQuery("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
	*/
				
	//CLOSING POPUP
	//Click the x event!
	jQuery("#popupContactClose").click(function(e){
		e.preventDefault();
		disablePopup("#popupContact");
	});
	jQuery("#popupPreviewClose").click(function(e){
		e.preventDefault();
		disablePopup("#popupPreview");
	});
	//Click out event!
	jQuery(".backgroundPopup").click(function(e){
		e.preventDefault();
		disablePopup();
	});
	
	// ESC
	jQuery(document).keydown(function(e){
		if(e.keyCode==13 && popupStatus["#popupPreview"]==1) {
			current=jQuery("ul#p_ul_icons").find("li.active");
			url=current.find("a").attr("href");
			location.href=url;
		} else if(e.keyCode==27 && popupStatus["#popupPreview"]==1) {
			disablePopup("#popupPreview");
		} else if(e.keyCode==39 && popupStatus["#popupPreview"]==1) {
			current=jQuery("ul#p_ul_icons").find("li.active");
			next=current.next();
			if(next.parent().attr("id")!="p_ul_icons")
				next=jQuery("ul#p_ul_icons").find("li:first-child");
			
			current.removeClass("active");
    		current.find("div.p_title").remove();
	    	next.addClass("active");
    		p_title_page=next.find("a").attr("title");
			p_title="<div class=\"p_title\"><span class=\"p_title_left_column\"></span><span class=\"p_title_right_column\"></span><p>"+p_title_page+"</p></div>";
	    	next.append(p_title);
		} else if(e.keyCode==37 && popupStatus["#popupPreview"]==1) {
			current=jQuery("ul#p_ul_icons").find("li.active");
			prev=current.prev();
			if(prev.parent().attr("id")!="p_ul_icons")
				prev=jQuery("ul#p_ul_icons").find("li:last-child");
			
			current.removeClass("active");
    		current.find("div.p_title").remove();
	    	prev.addClass("active");
    		p_title_page=prev.find("a").attr("title");
			p_title="<div class=\"p_title\"><span class=\"p_title_left_column\"></span><span class=\"p_title_right_column\"></span><p>"+p_title_page+"</p></div>";
	    	prev.append(p_title);
		} else if(e.keyCode==37 && popupStatus["#popupPreview"]==1) {
		
		}
	});
	
	//Press Escape event!
	/*
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	*/
	jQuery("input#key, #search_form #category").live("click", function() {
		if(jQuery("#d_search_box_info").css("display")=="block") {
			jQuery("#d_search_box_info span p").animate({"opacity": "0"}, "slow", function() {
				jQuery("#d_search_box_info").slideUp(500);
			});
		}
	});
	
	jQuery('#search_form').submit(function () {
		if(jQuery("input#key").attr("value")=="...") {		
        	jQuery("input#key").focus();
        } else {
        	var key=jQuery("#search_form #key").attr("value");
			var category=jQuery("#search_form #category option:selected").attr("value");
			if(category==-1) {
				jQuery("#d_search_box_info span p").html("* Please choose an option.");
				jQuery("#d_search_box_info span p").animate({"opacity": "0"}, 0);
				jQuery("#d_search_box_info").slideDown(500, function() {
					jQuery("#d_search_box_info span p").animate({"opacity": "1"}, "slow");
				});
			} else if(category==0) {
				location.href = "search_geo.php?name="+encodeURIComponent(key);
			} else {
				jQuery.ajaxSetup({ xhr:function() {
					//return new window.XMLHttpRequest();
					try {
						if(window.ActiveXObject)
							return new window.ActiveXObject("Microsoft.XMLHTTP");
					} catch(e){}
					return new window.XMLHttpRequest();
				}});
				jQuery.ajax({
	        	    type: 'POST',
    		        url: 'includes/check_search_key.php',
	    	        data: 'key='+key,
            		success: function(data) {
            			if(data=="0") {
							jQuery("#d_search_box_info span p").html("* Please be more specific. Enter a postcode or a nearest borough.");
							jQuery("#d_search_box_info span p").animate({"opacity": "0"}, 0);
							jQuery("#d_search_box_info").slideDown(500, function() {
								jQuery("#d_search_box_info span p").animate({"opacity": "1"}, "slow");
							});
						} else {
							var key=jQuery("#search_form #key").attr("value");
							var category=jQuery("#search_form #category option:selected").attr("value");
							jQuery.ajax({
								type: 'GET',
								url: "includes/multimapxml.php?key="+key,
								cache: false,
								dataType: (jQuery.browser.msie) ? 'text' : 'xml', // Browser recognition.
								success: function(data) {
									var xml;
									if(typeof data == 'string') {
										xml = new ActiveXObject('Microsoft.XMLDOM');
										xml.async = false;
										xml.loadXML(data);
									} else {
										xml = data;
									}
									
									total_results=jQuery(xml).find('NumResults').text();
									if(total_results!=0) {								
										var points=new Array();
										var i=0;
										jQuery(xml).find('Result').each(function () {
											var marker = jQuery(this);
								
											var point=new Array();
								
											point["DisplayName"]=marker.find("DisplayName").text();
											point["Lat"]=marker.find("Lat").text();
											point["Lon"]=marker.find("Lon").text();
								
											points[i]=point;
											i++;				
										});			
								
										if(total_results>1) {
											jQuery("#u_results").html("");
											for (i in points) {
												a_link="<li><a href=\"#\" class=\"a_result_options\" id=\""+i+"\" ><input type=\"hidden\" id=\"category\" value=\""+category+"\" /><input type=\"hidden\" id=\"DisplayName\" value=\""+points[i]["DisplayName"]+"\" /><input type=\"hidden\" id=\"Lat\" value=\""+points[i]["Lat"]+"\" /><input type=\"hidden\" id=\"Lon\" value=\""+points[i]["Lon"]+"\" />"+points[i]["DisplayName"]+"</a></li>";
												jQuery("#u_results").append(a_link);
											}
											//centering with css
											centerPopup("#popupContact");
											//load popup
											loadPopup("#popupContact");
										} else {
											setCookie("key", key, 30);
											location.href = "search_geo.php?Lat="+points[0]["Lat"]+"&Lon="+points[0]["Lon"]+"&key="+key+"&category="+category;
											/**
											centerPopup("#popupAdvert");
											loadPopup("#popupAdvert");
											
											if(getCookie("advertising")==1) {
												location.href = "search_geo.php?Lat="+points[0]["Lat"]+"&Lon="+points[0]["Lon"]+"&key="+key+"&category="+category;
											} else {
												setTimeout(function() { 
													setCookie("advertising", 1, 1);
													location.href = "search_geo.php?Lat="+points[0]["Lat"]+"&Lon="+points[0]["Lon"]+"&key="+key+"&category="+category;
												}, ((jQuery("#popupAdvert").find("input#i_seconds").attr("value"))*1000));				
											}
											**/
										}
									} else {
										jQuery("#d_search_box_info span p").html("* No Matches, please check entry.");
										jQuery("#d_search_box_info span p").animate({"opacity": "0"}, 0);
										jQuery("#d_search_box_info").slideDown(500, function() {
											jQuery("#d_search_box_info span p").animate({"opacity": "1"}, "slow");
										});
									}
								},
								error: function(data) {
									alert("Error");
								}
							});
						}
					}
       			});
			}
       	}
       	return false;
	});
	    
    jQuery(".a_result_options").live("click", function() {
    	option=jQuery(this).attr("id");
    	this_link=jQuery(this);
    	cat=this_link.find("#category").val();
    	name=this_link.find("#DisplayName").val();
    	lat=this_link.find("#Lat").val();
    	lon=this_link.find("#Lon").val();
    	
    	disablePopup("#popupContact");
    	setCookie("key", name, 30);
    	centerPopup("#popupAdvert");
		loadPopup("#popupAdvert");
		if(getCookie("advertising")==1)
			location.href = "search_geo.php?Lat="+lat+"&Lon="+lon+"&key="+name+"&category="+cat;
		else {
			setTimeout(function() {
				setCookie("advertising", 1, 1);
				location.href = "search_geo.php?Lat="+lat+"&Lon="+lon+"&key="+name+"&category="+cat;
			}, ((jQuery("#popupAdvert").find("input#i_seconds").attr("value"))*1000));
		}
    	return false;
    });
    
    jQuery("a#follow_us_cm").live("click", function(e) {
    	e.preventDefault();

    	//centering with css
		centerPopup("#popupPreview");
		//load popup
		loadPopup("#popupPreview");

    });
    
    jQuery("ul#p_ul_icons").find("li.active").each(function() {
    	p_title_page=jQuery(this).find("a").attr("title");
    	p_title="<div class=\"p_title\"><span class=\"p_title_left_column\"></span><span class=\"p_title_right_column\"></span><p>"+p_title_page+"</p></div>";
    	jQuery(this).append(p_title);
    });
    
    
    
    jQuery("ul#p_ul_icons li").mouseenter(function() {
    	jQuery(this).parent().find("li").removeClass("active");
    	jQuery(this).parent().find("div.p_title").remove();
    	jQuery(this).addClass("active");
    	p_title_page=jQuery(this).find("a").attr("title");
		p_title="<div class=\"p_title\"><span class=\"p_title_left_column\"></span><span class=\"p_title_right_column\"></span><p>"+p_title_page+"</p></div>";
    	jQuery(this).append(p_title);
    });
});

