/*
Title:      Main JavaScript	

*/
//if (top!=self) top.location.replace(location.href); 

// ------[ IE6 Cache Control (remove flicker on mouseover) ]------------------------------------------------- //	
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

// ------[ Master Onload for all page. ]------------------------------------------------- //	
window.HostPapa = window.HostPapa || {};

HostPapa = { };

// ------[ Redirection for Language DropDown ]------------------------------------------------- //
HostPapa.Language = {		
	Redirect : function () {					
		document.location = this.value;									
	}	
}

HostPapa.TLDCountry = {		
	Redirect : function () {			
		var prefix = "www";
		if(HostPapa.test == "1") {
			prefix = "dev";
		} 
		
		switch (this.value){
			case "BE":
				if(HostPapa.orderLanguage == "en") {
					$lang = "en";
				} else {
					$lang = "home";
				}

				document.location = "http://"+prefix+".hostpapa.eu/"+$lang+"/country/be";
				break;
				
			case "CA":
				if(HostPapa.orderLanguage == "fr") {
					$lang = "fr";
				} else {
					$lang = "";
				}			
				document.location = "http://"+prefix+".hostpapa.ca/" + $lang;
				break;
				
			case "FR":
				if(HostPapa.orderLanguage == "en") {
					$lang = "en";
				} else {
					$lang = "home";
				}			
				document.location = "http://"+prefix+".hostpapa.eu/"+$lang+"/country/fr";
				break;			
				
			case "NL":
				if(HostPapa.orderLanguage == "en") {
					$lang = "en";
				} else {
					$lang = "home";
				}			
				document.location = "http://"+prefix+".hostpapa.eu/"+$lang+"/country/nl";
				break;
				
			case "ES":
				if(HostPapa.orderLanguage == "en") {
					$lang = "en";
				} else {
					$lang = "home";
				}			
				document.location = "http://"+prefix+".hostpapa.eu/"+$lang+"/country/es";
				break;
				
			case "GB":
				document.location = "http://"+prefix+".hostpapa.co.uk/";		
				break;
				
			case "US":
				document.location = "http://"+prefix+".hostpapa.com/";			
				break;		
		}					
	}	
}

// ------[ Feature, for the featured items on the home ]------------------------------------------------- //	
HostPapa.Feature = {
	currentPage: 0,
	
	Init : function() {
		$('#feature .items li:gt(0)').hide();
		
		// Build the paging
		toInject = "";
		$('#feature .items li').each(function(i){
			toInject += "<li><a href='#' onclick='HostPapa.Feature.GoToPage("+i+");return false;'>"+(i+1)+"</a></li>";
		});
		
		$('#feature .paging').html(toInject);
		$('#feature .paging li:first').addClass('selected');
		
		$('#feature').hover(function(e){
			clearTimeout(slideshow);
			},
			function(){
				HostPapa.Feature.SetTimer();
		});
		
		HostPapa.Feature.SetTimer();
	},
	
	GoToPage : function(pageToShow,callback){
		clearTimeout(slideshow);
		
		// Fade out the current page
		$('#feature .items li:eq('+HostPapa.Feature.currentPage+')').fadeTo('normal',0,function(){
			$(this).hide();
			
			$('#feature .items li:eq('+pageToShow+')').css('opacity',0).show().fadeTo('normal',1);
		});
		
		// Select the correct paging item
		$('#feature .paging li.selected').removeClass('selected');
		$('#feature .paging li:eq('+pageToShow+')').addClass('selected');
		
		HostPapa.Feature.currentPage = pageToShow;
		
		if(callback) callback();
	},
	
	SetTimer : function(){
		slideshow = setTimeout(function(){
			nextPage = HostPapa.Feature.currentPage+1;
			if(nextPage == $('#feature .paging li').size()){
				nextPage = 0;
			}
			
			HostPapa.Feature.GoToPage(nextPage,function(){ HostPapa.Feature.SetTimer(); });
		},10000);
	}
};

// ------[ Phone Number Panel ]------------------------------------------------- //
HostPapa.Phone = {
	timer : null,
	
	Init : function() {		
		button = document.getElementById('header-phone-button');
		panel = document.getElementById('header-phone-list');
		
		button.panel = panel;
		panel.button = button;
		
		YAHOO.util.Event.addListener(button, "mouseover", HostPapa.Phone.Show);
		YAHOO.util.Event.addListener(panel, "mouseover", HostPapa.Phone.Show);
		YAHOO.util.Event.addListener(panel, "mouseout", HostPapa.Phone.Hide);
		
		HostPapa.Phone.SetCountryValue(HostPapa.Order.CountryState.countryValue);
		YAHOO.util.Event.addListener("ct_country", "change", HostPapa.Phone.SetCountryValue);	
	},
	
	Show : function() {									
		clearTimeout(HostPapa.Phone.timer);
		HostPapa.Phone.timer = null;
		
		YAHOO.util.Dom.setStyle(this.panel, "display", "block");	
	},
	
	Hide : function(e) {   								
		var timer = 1000;

		var fader = new YAHOO.util.Anim(this,
			{ opacity: {from: 1, to: 0 } },
			'1.0',  
			YAHOO.util.Easing.easeOut);  
		
		fader.onComplete.subscribe(function() {
			var el = this.getEl();
			
			YAHOO.util.Dom.setStyle(el, "display", "none");									
			YAHOO.util.Dom.setStyle(el, "opacity", "1");			
		});

		HostPapa.Phone.timer = setTimeout(function() { fader.animate(); }, timer);		
	}, 
	
	SetCountryValue : function(country) {
		country = (typeof(country) == 'string') ? country : this.value;	
		phoneplh = document.getElementById('header-phone-country');
		//phonebutton = document.getElementById('header-phone-button');
		
		if(country == "FR") {
			YAHOO.util.Dom.setStyle('header-phone-button', "display", "none");
			phone = document.getElementById('phone-FR');
			phoneplh.innerHTML = phone.innerHTML;	
			
		} else if(country == "ES") {
			YAHOO.util.Dom.setStyle('header-phone-button', "display", "none");
			phone = document.getElementById('phone-ES');		
			phoneplh.innerHTML = phone.innerHTML;
			
		} else if(country == "BE") {
			YAHOO.util.Dom.setStyle('header-phone-button', "display", "none");
			phone = document.getElementById('phone-BE');
			phoneplh.innerHTML = phone.innerHTML;
			
		} else if(country == "NL") {
			YAHOO.util.Dom.setStyle('header-phone-button', "display", "none");
			phone = document.getElementById('phone-NL');
			phoneplh.innerHTML = phone.innerHTML;
			
		} else {
			YAHOO.util.Dom.setStyle('header-phone-button', "display", "block");
			phoneplh.innerHTML = "";
		}
		
	}
};


// ------[ Feature, for the featured items on the home ]------------------------------------------------- //	
HostPapa.TabBrowser = {

	Init : function() {	
	  //Get all the LI from the #tabMenu UL
	  if($('#tabMenu > li')) {
		  $('#tabMenu > li').click(function(){
				
			//perform the actions when it's not selected
			if (!$(this).hasClass('selected')) {    
			
	
			$('#tabMenu a').attr("href", "javascript:void(0);");
			
			
			//remove the selected class from all LI    
			$('#tabMenu > li').removeClass('selected');
			
			//After cleared all the LI, reassign the class to the selected tab
			$(this).addClass('selected');
			
			//Hide all the DIV in .boxBody
			$('.boxBody div').slideUp('1500');
			
			//Look for the right DIV index based on the Navigation UL index
			$('.boxBody div.boxContent:eq(' + $('#tabMenu > li').index(this) + ')').slideDown('1500');
			
		   }
		
		  }).mouseover(function() {
		
			//Add and remove class, Personally I dont think this is the right way to do it, 
			//if you have better ideas to toggle it, please comment    
			$(this).addClass('mouseover');
			$(this).removeClass('mouseout');   
			
		  }).mouseout(function() { 
			
			//Add and remove class
			$(this).addClass('mouseout');
			$(this).removeClass('mouseover');    
			
		  });
	  }
	  	
	 
	}
	
};

$(document).ready(function(){
	HostPapa.TabBrowser.Init();
});

// ------[ Feature, for the featured items on the home ]------------------------------------------------- //	
HostPapa.Carousel = {

	Init : function(id) {	
		jQuery(id).jcarousel({
			auto: 8,
			wrap: 'wrap',
			animation: 2000,
			initCallback: HostPapa.Carousel.mycarousel_initCallback
		});	
	
	},
	
	mycarousel_initCallback : function(carousel) {
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});
	
		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});
	
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	}
	
	
};


// ------[ WebMail ]------------------------------------------------- //	
HostPapa.WebMail = {
	Init : function(id) {	
		form = document.getElementById("webmail");
		if(form) {
			form.onsubmit = HostPapa.WebMail.Submit;
		}	
	},
	
	Submit : function() { 		
		value = document.getElementById("domainname").value 
		
		if(value) {
			window.open("http://" + value + "/webmail", 'tools'); 		
		}
		
		return false;
	}
}

$(document).ready(function(){
	HostPapa.WebMail.Init();
});
// ------[ cPanel ]------------------------------------------------- //	
HostPapa.Cpanel = {
	Init : function(id) {	
		form = document.getElementById("cpanel");
		if(form) {
			form.onsubmit = HostPapa.Cpanel.Submit;
		}	
	},
	
	Submit : function() { 		
		value = document.getElementById("domainname").value 
		
		if(value) {
			window.open("http://" + value + "/cpanel", 'tools'); 		
		}
		
		return false;
	}
}

$(document).ready(function(){
	HostPapa.Cpanel.Init();
});


// ------[ Popup Window ]------------------------------------------------- //
HostPapa.PopWindow = function(url,w,h,scroll,tools,name,center) {
	var str = "height=" + h + ",innerHeight=" + h;
	str += ",width=" + w + ",innerWidth=" + w;
	if(!center) var center = false;
	if(!scroll) scroll = 0;
	if(!tools) tools = 0;
	if(!name) name = "pop";
	if((window.screen) && (center)) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - w) / 2;
		var yc = (ah - h) / 2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		}
	pop = window.open(url,name,'toolbar=' + tools + ',location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll + ',resizable=1,' + str).focus();
};


// ------[ Get URL Param ]------------------------------------------------- //
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

// ------[ fix Invalid URL for image ]------------------------------------------------- //

$(document).ready(function(){	
	if($('img')) {
		$('img').each(function(i) {
			src = $(this).attr('src');
	
			if(src.indexOf('assets/') == 0) {
				$(this).attr('src', "/" + src);
			}
		});
	}
});




