function hc_domready() {
	hc_add_link_click_handlers();
	hc_handle_bookmarked_link();
	hc_trigger_browser_nav_pickup_timer();
}
var hc_current_icon='';
var hc_current_navpage='';
var hc_prev_document_href='';
var hc_themebase='hybridcloud';
var hc_cake_cascade_running=false;
var hc_scalability_graph_cascade_running=false;
var hc_data_centre_cloud_fade_running=false;
// This quick and dirty Web 2.0 page loader looks good but has one drawback - the size of the page must be known as soon as the HTML is received. This means you must specify widths and heights for all images and other non-text content within the HTML or CSS itself - do not rely on the browser to download the image first in order to figure out its size - this will result in the page loading with an ugly glitch.


// This function handles clicks on the icon boxes
function hc_clickicon(icon,offset) {
	if (icon==hc_current_icon) {
		return false;
	}
	$('#icon-image-link-'+icon).css('cursor','progress');
	$('#hiddencontent').css('cursor','progress');
	$('#visiblecontent').css('cursor','progress');
	if (hc_crappybrowser()) { 
		hc_prev_document_href=$('#icon-image-link-'+icon).attr('href');
	}
	$('#contentbuffer').load($('#icon-image-link-'+icon).attr('href'),{contentonly: 'true'}, function() { 
		do_switch(function() {
			$('#icon-image-link-'+icon).css('cursor','');
			$('#hiddencontent').css('cursor','');
			$('#visiblecontent').css('cursor','');
		});
	});
	pageTracker._trackPageview($('#icon-image-link-'+icon).attr('href'));
	window.location.href=window.location.href.substr(0,window.location.href.lastIndexOf('#'))+'#'+$('#icon-image-link-'+icon).attr('href');
	if (!hc_crappybrowser()) { 
		hc_prev_document_href=window.location.href;	
	} else { 
		$('#hidden-iframe').attr('src','/wp-content/themes/'+hc_themebase+'/ie-back-button-enabler.php?'+$('#icon-image-link-'+icon).attr('href'));
	}
	$('#icon-image-over-'+icon).stop();
	$('#icon-image-over-'+icon).fadeTo(300,0);
	if (hc_current_icon=='') {
		$('#icon-nub').css('left',77+offset*109);
		$('#icon-nub').fadeTo(200,1);
		if (hc_current_navpage!='') { 
			$('#text-nav-arrow-'+hc_current_navpage).fadeTo(200,0);
			hc_current_navpage='';
		}
	} else {
		// Change to false to disable animated nub
		if (true) { 
			$('#icon-nub').animate({left: 77+offset*109},300);
		} else { 
			$('#icon-nub').fadeTo(50,0,function() { 
				$('#icon-nub').css('left',77+offset*109);
				$('#icon-nub').fadeTo(50,1);
			});
		}
	}
	hc_current_icon=icon;
	return false;
}

// This handles clicks on the nav links at the top of the page
function hc_clicknav(page) { 
	var link=$('#text-nav-link-'+page);
	link.css('cursor','progress');
	$('#hiddencontent').css('cursor','progress');
	$('#visiblecontent').css('cursor','progress');
	if (hc_crappybrowser()) { 
		hc_prev_document_href=link.attr('href');
	}
	$('#contentbuffer').load(link.attr('href'),{contentonly: 'true'}, function() { 
		do_switch(function() {
			$('#hiddencontent').css('cursor','');
			link.css('cursor','pointer');
			$('#visiblecontent').css('cursor','');
		});
	});
	pageTracker._trackPageview(link.attr('href'));
	window.location.href=window.location.href.substr(0,window.location.href.lastIndexOf('#'))+'#'+link.attr('href');
	if (!hc_crappybrowser()) { 
		hc_prev_document_href=window.location.href;
	} else { 
		$('#hidden-iframe').attr('src','/wp-content/themes/'+hc_themebase+'/ie-back-button-enabler.php?'+link.attr('href'));
	}
	if (hc_current_icon!='') { 
		$('#icon-nub').fadeTo(200,0);
		hc_current_icon='';
	}
	if (hc_current_navpage!='') {
		$('#text-nav-arrow-'+hc_current_navpage).fadeTo(200,0);
		hc_current_navpage='';
	}
	$('#text-nav-arrow-'+page).fadeTo(200,1);
	hc_current_navpage=page;
	return false;
}

function do_switch(callback) { // This has been refactored from all the functions below, cnp code is bad, and there's no excuse for it in a language with first class functions ;)
		$('#visiblecontent').html($('#contentbuffer').html()+"<div style='clear:both;'></div>");
		hc_add_link_click_handlers($('#visiblecontent'));
		$('#contentbuffer').html('');
		$.scrollTo('0%');
		callback();
		if(hc_current_icon=='price' || hc_current_icon=='order') {
			initSliders();
		}
		if (hc_current_icon=='faqs') {
			for (var i=0;i<=hc_total_faq_items;i++) {
				faq_opens[i]=false;	
			} 
			faq_open_in_progress=false;
		}
		if (hc_current_icon=='tech') { 
			for (var i=0;i<=hc_total_tech_items;i++) {
				tech_opens[i]=false;	
			} 
			tech_open_in_progress=false;
		}
}

// This function handles all other link clicks
function hc_link_click_handler() { 
	var link=$(this);

	oursite = 'http://www.hybrid-cluster.com'
	thishref = this.href;
	if(thishref.substr(0,oursite.length)==oursite) {
		thishref = thishref.replace(oursite,'');
	}

	// If the link is the same as one of the navigation links at the top of the page, we want to click on that link instead, so we get the special behaviour relating to a navigation link
	var keynavlinks=$('a[data-keynavlink=true]');
	var navlinkhandled=false;
	keynavlinks.each(function(i,o) { 
		var thislink;
		var tmplink;
		if (link.attr('href').substr(0,7)=='http://') { 
			tmplink=link.attr('href').substr(8);
			thislink=tmplink.substr(tmplink.indexOf('/'));
		} else {
			thislink=link.attr('href');
		}
		if ($(keynavlinks[i]).attr('href').replace(/\//g,'')==thislink.replace(/\//g,'')) {
			$(keynavlinks[i]).click();
			navlinkhandled=true;
		}
	});
	if (navlinkhandled) {
		return false;
	}
	link.css('cursor','progress');
	$('#hiddencontent').css('cursor','progress');
	$('#visiblecontent').css('cursor','progress');
	if (hc_crappybrowser()) { 
		hc_prev_document_href=thishref;
	}
	$('#contentbuffer').load(thishref,{contentonly: 'true'}, function() { 
		do_switch(function() {
			$('#hiddencontent').css('cursor','');
			link.css('cursor','pointer');
			$('#visiblecontent').css('cursor','');
		});
	});
	pageTracker._trackPageview(thishref);
	window.location.href=window.location.href.substr(0,window.location.href.lastIndexOf('#'))+'#'+thishref;
	if (!hc_crappybrowser()) { 
		hc_prev_document_href=window.location.href;
	} else { 
		$('#hidden-iframe').attr('src','/wp-content/themes/'+hc_themebase+'/ie-back-button-enabler.php?'+thishref);
	}
	if (hc_current_icon!='') { 
		$('#icon-nub').fadeTo(200,0);
		hc_current_icon='';
	} 
	if (hc_current_navpage!='') { 
		$('#text-nav-arrow-'+hc_current_navpage).fadeTo(200,0);
		hc_current_navpage='';
	}
	return false;
}
function hc_initialize_nub(offset,page) {
	hc_current_icon=page;
	$('#icon-nub').css('left',77+offset*109);
	$('#icon-nub').fadeTo(100,1);
}
function hc_initialize_navpage(page) {
	hc_current_navpage=page;
	$('#text-nav-arrow-'+page).fadeTo(100,1);
}
function hc_cloud_computing_pageload() {
	hc_trigger_cake_cascade();
	hc_trigger_data_centre_cloud_fade();
}
function hc_scalability_pageload() {
	hc_scalability_graph_cascade_running=true;
	setTimeout("hc_scalability_graph_cascade()",2000);
}
function hc_scalability_graph_cascade() {
	$('#scalable-website-graph-2').fadeTo(1500,1,function() { 
		$('#scalable-website-graph-3').fadeTo(1500,1,function() {
			$('#scalable-website-graph-4').fadeTo(1500,1,function() { 
				$('#scalable-website-graph-5').fadeTo(3000,1,function() { 
					setTimeout("hc_trigger_scalability_graph_cascade_reset()",15000);
				});
			});
		});
	});
}
function hc_trigger_scalability_graph_cascade_reset() {
	$('#scalable-website-graph-2').hide();
	$('#scalable-website-graph-3').hide();
	$('#scalable-website-graph-4').hide();
	$('#scalable-website-graph-5').fadeTo(1200,0,function() { 
		hc_scalability_graph_cascade_running=false;
		hc_scalability_pageload();
	});
}
function hc_trigger_data_centre_cloud_fade() {
	if (hc_data_centre_cloud_fade_running) {
		return;
	}
	hc_data_centre_cloud_fade_running=true;
	setTimeout("hc_data_centre_cloud_appear()",15000);
}
function hc_data_centre_cloud_appear() { 
	if (!$('#data-centre-cloud')) {
		hc_data_centre_cloud_fade_running=false;
		return;
	}
	$('#data-centre-cloud').fadeTo(1200,1,function() { 
		setTimeout("hc_data_centre_cloud_fade()",15000);
	});
}
function hc_data_centre_cloud_fade() { 
	if (!$('#data-centre-cloud')) {
		hc_data_centre_cloud_fade_running=false;
		return;
	}
	$('#data-centre-cloud').fadeTo(1200,0,function() { 
		setTimeout("hc_data_centre_cloud_appear()",15000);
	});
}
function hc_trigger_cake_cascade() {
	if (hc_cake_cascade_running) {
		return;
	}
	hc_cake_cascade_running=true;
	setTimeout("hc_cake_cascade_1()",2000);
}
function hc_cake_cascade_1() {
	if (!$('#mmmm-cake-2')) {
		hc_cake_cascade_running=false;
		return;
	}
	$('#mmmm-cake-2').fadeTo(1200,1,function() { 
		setTimeout("hc_cake_cascade_2()",700);
	});
}
function hc_cake_cascade_2() {
	if (!$('#mmmm-cake-3')) {
		hc_cake_cascade_running=false;
		return;
	}
	$('#mmmm-cake-3').fadeTo(1200,1,function() { 
		setTimeout("hc_cake_cascade_3()",700);
	});
}
function hc_cake_cascade_3() {
	if (!$('#mmmm-cake-4')) {
		hc_cake_cascade_running=false;
		return;
	}
	$('#mmmm-cake-4').fadeTo(1200,1,function() { 
		setTimeout("hc_trigger_cake_cascade_reset()",5000);			
	});
}
function hc_trigger_cake_cascade_reset() { 
	if (!$('#mmmm-cake-2') || !$('#mmmm-cake-3') || !$('#mmmm-cake-4')) {
		hc_cake_cascade_running=false;
		return;
	}
	$('#mmmm-cake-2').hide();
	$('#mmmm-cake-3').hide();
	$('#mmmm-cake-4').fadeTo(1200,0,function() { 
		hc_cake_cascade_running=false;
		hc_trigger_cake_cascade();
	});
}
function hc_overicon(icon) {
	if (icon==hc_current_icon) {
		return;
	}
	$('#icon-image-over-'+icon).stop();
	$('#icon-image-over-'+icon).fadeTo(100,1);
}
function hc_outicon(icon) {
	$('#icon-image-over-'+icon).stop();
	$('#icon-image-over-'+icon).fadeTo(300,0);
}
// If you wish to prevent a link having the click handler added, set the data-noclickhandle attribute to 'true'
function hc_add_link_click_handlers(target) { 
	if (typeof target == 'undefined') {
		target=$(document);
	}
	target.find('a[data-noclickhandle!=true][target!=_blank]').click(hc_link_click_handler);
}
function hc_handle_bookmarked_link() {
	if (window.location.href.lastIndexOf('#')==-1) {
		return false;
	} 
	if (window.location.href.lastIndexOf('#wpcf7')>=0) { // don't mangle wordpress contact form
		return false;
	} 
	$('#hidden-link').attr('href',window.location.href.substr(window.location.href.indexOf('#')+1));
	$('#hidden-link').click();
}
function hc_trigger_browser_nav_pickup_timer() {
	if (!hc_crappybrowser()) { 
		if (hc_prev_document_href!='' && hc_prev_document_href!=window.location.href) { 
			hc_handle_bookmarked_link();
		}	
		hc_prev_document_href=window.location.href;	
		setTimeout("hc_trigger_browser_nav_pickup_timer()",500);
	}
}
function hc_iframe_parent_updater(v) {
	if (hc_prev_document_href!='' && hc_prev_document_href!=v) {
		$('#hidden-link').attr('href',v);
		$('#hidden-link').click();
	}
	hc_prev_document_href=v;
}
function hc_crappybrowser() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function calculate() {
	min = {};
	max = {};
	$.each(data,function(k,v) {
		min[k] = $("#"+k+"-slider").slider("values", 0);
		if(v.range) {
			max[k] = $("#"+k+"-slider").slider("values", 1);
			$("#"+k).val($("#"+k+"-slider").slider("values", 0) + ' - ' + $("#"+k+"-slider").slider("values", 1));
		} else {
			max[k] = min[k];
			$("#"+k).val($("#"+k+"-slider").slider("values", 0));
		}
	});

	server_coeff = 0.048;
	storage_coeff = 0.0015;
	bandwidth_coeff = 0.0005;

	hpm = 730.5;
	
	price_min = min.numservers * min.cpu * min.memory * server_coeff + min.storage * storage_coeff + min.bandwidth * bandwidth_coeff;
	price_max = max.numservers * max.cpu * max.memory * server_coeff + max.storage * storage_coeff + min.bandwidth * bandwidth_coeff;

	$('#price_text').html('<div style="text-align:right;">&pound;'+number_format(price_min,2) + ' - &pound;' + number_format(price_max,2) + ' per hour<br /><small>(' +
	'&pound;'+number_format(price_min*hpm,2) + ' - &pound;' + number_format(price_max*hpm,2) + ' per month)</small></div>');

}

function initSliders() {
	$.each(data,function(k,v) {
		$("#"+k+"-slider").slider({
			range: v.range,
			min: v.min,
			max: v.max,
			values: v.values,
			slide: function(event, ui) {
				window.setTimeout(calculate,10);
			}
		});
		calculate();
	});

};
var faq_opens=[];
var faq_open_in_progress=false;
function hc_expand_faq_from_div(num) {
	if (faq_opens[num]) { 
		return false;
	} else {
		return hc_expand_faq(num);
	}
}
function hc_expand_faq(num,closeothers) {
	if (faq_open_in_progress && closeothers!=false) {
		return false;
	}
	faq_open_in_progress=true;
	$("#spacergif").show();
	if (typeof faq_opens[num] == "undefined" || faq_opens[num]==false) {
		$("#faq-content-"+num).show("blind",{},1000,function() { faq_open_in_progress=false;});
		$("#contract-icon-"+num).fadeIn("slow");
		pageTracker._trackPageview('/faq-item-'+num+'-open/');
		if (typeof closeothers == "undefined" || closeothers==true) { 
			for (var i=0;i<=hc_total_faq_items;i++) { 
				if (faq_opens[i]==true) { 
				hc_expand_faq(i,false);
				}
			}
		}
		faq_opens[num]=true;
	} else {
		$("#faq-content-"+num).hide("blind",{},1000,function() { faq_open_in_progress=false;});
		$("#contract-icon-"+num).fadeOut("slow");
		faq_opens[num]=false;
	}
	$("#spacergif").hide();
	return false;
}
var tech_opens=[];
var tech_open_in_progress=false;
function hc_expand_tech_from_div(num) { 
	return hc_expand_tech(num);
}
function hc_expand_tech(num,closeothers) { 
	if (tech_open_in_progress && closeothers!=false) { 
		return false;
	}
	tech_open_in_progress=true;
	$("#spacergif").show();
	if (typeof tech_opens[num] == "undefined" || tech_opens[num]==false) { 
		$("#tech-content-"+num).show("blind",{},1000,function() { tech_open_in_progress=false;});
		$("#up-icon-"+num).fadeIn("slow");
		$("#tech-floater-"+num).animate({'backgroundColor' : '#fefcfa'});
		pageTracker._trackPageview('/tech-item-'+num+'-open/');
		if (typeof closeothers == "undefined" || closeothers==true) { 
			for (var i=0;i<=hc_total_tech_items;i++) { 
				if (tech_opens[i]==true) { 
				hc_expand_tech(i,false);
				}
			}
		}
		tech_opens[num]=true;

	} else {
		$("#tech-content-"+num).hide("blind",{},1000,function() { tech_open_in_progress=false;});
		$("#up-icon-"+num).fadeOut("slow");
		$("#tech-floater-"+num).animate({'backgroundColor' : '#fef9f1'});
		tech_opens[num]=false;
	}
	$("#spacergif").hide();
	return false;
}

