jQuery(document).ready(function() {

	$("a.fancybox").fancybox({
		'hideOnContentClick': false
	}); 
	jQuery("a.map-fancybox").fancybox({
		'hideOnContentClick': false,
		'frameHeight': 370
		
	});
	jQuery("a.map-fancybox-full").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 620,
		'frameHeight': 460
		
	});

	jQuery("#quick-location").submit(function() {
		var location = jQuery("input[name=location]", this).val();

		var url = jQuery(this).attr("action");
		url += "/"+encodeURIComponent(location);
		
		jQuery("<a href='"+url+"'>link</a>").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 640,
		'frameHeight': 480
		}).click();
	
		return false;
	});
	
	
	if (screen.width>1024) {
		jQuery("#container").css("padding", "0 15px");
	}
	
	var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);
	if (isIE) {
		jQuery("p.review-quote-left, p.review-quote-right").css("padding", "0");
	}
	
	
	jQuery("#featured-box #featured-controls .homepage-tab").click(function(){
		var tab = jQuery(this).attr("rel");
		
		jQuery("#featured-box #featured-controls .homepage-tab").removeClass("active");
		jQuery(this).addClass("active");
		
		jQuery("#featured-box #featured-content div[id^=tab-]").css("display","none");
		
		var obj = jQuery("#featured-box #featured-content #tab-"+tab);
		obj.css("display","block");
		if (obj.is(":empty")) {
			obj.html("Loading....");
			
			var url = "/render/tabs?tab="+tab;
			jQuery.get(url, function(data) {
				obj.html(data);
			}, "html");
		}
	});
});

function doBindAjaxSubmit() {

	jQuery(".ajax-submit").submit(function() {
		var form_data = jQuery(this).serialize();
		var url = jQuery(this).attr("action");
		url = url.replace("nojs", "render");
		
		jQuery.post(url, form_data, function(data) {
			if (data.status == "n-success") {
				alert(data.notice);
			}
			else {
				alert(data.notice);
			}
			if (data.fbclose == true) {
				jQuery("a.map-fancybox").fancybox.close();
			}
		}, "json");
		
		return false;
	});
		
		
	jQuery(".ajax-submit-self").submit(function() {
		var self = this;
		var url = jQuery(this).attr("action");
		url = url.replace("nojs", "render");
		var target = self;
		if (jQuery(this).attr("target")) target = jQuery(this).attr("target");
		jQuery.post(url, jQuery(this).serialize(), function(data) {
			jQuery(target).html(data);
			
			updateFBStyle();
		});
	
		return false;
	});
}

function uploadByUrlAddMore() {
	var elm = jQuery("<p><input type='text' name='i-upload_by_url[]' class='text' style='float: none'/> <a href='javascript:void(0)' onclick='jQuery(this.parentNode).remove()'><span class='remove-button'></a></p>");
	jQuery('#upload_by_url').append(elm);
	jQuery('#p-main-content').scrollTo(jQuery('#p-main-content').height()+50,0);
}


function p_getContentDimensions() {
	var width=0,height=0;
				//alert(jQuery('#p-main-content').outerHeight(true)+", "+jQuery(".p-login-form .notification").outerHeight(true));
	var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);
				
	jQuery("#fancy_ajax").children().each(function(e){
		if (this.id == "p-alt-content" && jQuery("#p-alt-content").css("display") == "none") return;
		var p = jQuery(this).position();
		var w = jQuery(this).width();
		var h = jQuery(this).outerHeight(true);
		//if (isIE) alert(this.id+", "+this.className+", "+this.clientHeight);
		if (this.id == "p-main-content" || this.id == "p-alt-content") {
			h = 0;
			var cid = "#"+this.id;
			if (this.id == "p-alt-content") cid = "#p-alt-text";
			//alert("cid: "+cid);
			jQuery(cid).children().each(function(e) {
				var p1 = jQuery(this).position();
				var h1 = jQuery(this).outerHeight(true);
				//alert(this.id+", "+this.className+", "+p1.top+", "+h1);
				if (p1.top + h1 > h) h = p1.top + h1;
			});
			//h += 38;
		}
		//alert(this.id+", "+this.className+", "+p.top+", "+h);
		
		if (p.top + h > height) height = p.top + h;
		if (p.left+ w > width) width = p.left + w;
	});
	
	//height += 20;

	if (height > 500) height = 500;
	if (height < 150) height = 150;
	if (width > 900) width = 900;
	if (width < 425) width = 425;
	
	//height += 18;
	
	var p_height = height - 76;
	if (isIE) jQuery("#p-alt-content, #p-alt-text").css("width","auto");
	jQuery("#p-alt-content").height(p_height);
	jQuery("#p-main-content").height(p_height);
	//alert(height);
	return {width:width,height:height};
}

function updateFBStyle() {
	var _content_height = jQuery("#fancy_content").height();
	if (isIE) jQuery("#p-alt-content, #p-alt-text").css("width","auto");
	var p_height = _content_height - 76;
	jQuery("#p-alt-content").height(p_height);
	jQuery("#p-main-content").height(p_height);
	var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);

	
	return false; // FTW...
	var ops = p_getContentDimensions();
	$("#fancy_outer").animate(ops, false, false, $.fn.fancybox.scrollBox);
}


/* ################## SLIDESHOW */

var slideShowTimeoutHandler = false;
var slideCurrent = 2;
var slideShowDelay = 4000;

jQuery(document).ready(function(){
	jQuery("#slideshow #slide-controls a").click(function(){
		
		var r = this.id.match(/sc-([0-9]+)/);
		if (!r) return false;
		
		slideCurrent = parseInt(r[1]);
		s_slideTo(true);
		
		return false;
	});
	
	jQuery("#slideshow .wrap, #slideshow #slide-controls").hover(function(){
		clearTimeout(slideShowTimeoutHandler);
	}, function() {
		slideShowTimeoutHandler = setTimeout("s_slideTo()", slideShowDelay);
	});
	
	slideShowTimeoutHandler = setTimeout("s_slideTo()", slideShowDelay);
});

function s_slideTo(click) {
	if (!click || click == undefined) click = false;
	var n = slideCurrent;
	var w = jQuery("#slideshow").width();
	jQuery("#slideshow #slide-controls a").removeClass("active");
	jQuery("#slideshow #slide-controls #sc-"+n).addClass("active");
	
	var offset = -1 * w * (n -1);
	
	jQuery("#slideshow .wrap").animate({"left": offset});
	
	var total = jQuery("#slideshow #slide-controls a").length;
	
	slideCurrent++;
	if (slideCurrent > total) slideCurrent = 1;
	
	clearTimeout(slideShowTimeoutHandler);
	if (!click) slideShowTimeoutHandler = setTimeout("s_slideTo()", slideShowDelay);
}


/* ################## HAVING TROUBLES */

function showHideHavingTroubles(self) {
	jQuery('#bug-report').toggle();
	var title = jQuery(self).html()=='hide'?'show':'hide';
	jQuery(self).html(title);
	
	var url = '/json/set-cookie/?cn=helpbox&cv='+title;
	jQuery.get(url);

}

/* ################## CAPTCHA */

function updateCaptcha(elm) {
	var src = elm.src;

	src += (src.indexOf('?')==-1?'?':'') + Math.random();
	jQuery(elm).attr("src", src);
}

/* ################# UPDATE LOCATIONS */

function updateLocations(postcode) {
	//locations
	var url = "/json/locations/";
	jQuery.get(url, {'postcode':postcode}, function(data) {
		jQuery("#locations").html(data);
	}, "html");
}
