var dCr='http://destinoscr.incompanysolutions.com';
var dW='http://destinosworld.incompanysolutions.com';
var currentPage="destinosworld.incompanysolutions.com";
var currentPageLevel2='';
var currentCountry=null;
//Gets Current Page on Init

function init(){
	loadCurrentPage();
	 
	//Set for CSS AutoConfig 
	$('body').addClass(currentPage);
	 
	loadPais();
	
	sideNavHeightsAndPosition();
	 
	showProductsInCurrentCountry('.item'); 
	
	showCustomProductDetails();
	 
	showAutomaticBreadcrum();
 
	displayInDollars();
	
	removeProductCatalogMarkup();
	
	if(currentPage==''){
		runSlides();
	} 
	 
}


function hideCrContent(){
	$('.CR').hide();
}

//el domain destinosworld.incompanysolutions.com siempre va ingresar a la pagina de Costa Rica
function loadPais(){	
	//check if redirected to save cookie
	var newPais = getParameterByName('pais');
	
	if(newPais!='undefined' && newPais!=null){
		//save cookie
		icreateCookie('dtv-country',newPais,100); 
		currentCountry = newPais;
	}
	else{
		var tempC = ireadCookie('dtv-country');	
		if(tempC!='undefined' && tempC!=null){
			currentCountry = tempC;
		} 
		else{
			//determine current Path
			var host = document.location.host;
			
			//alert(host);
			//alert(VIH_HostIP);
			var ip_init = VIH_HostIP.split('.')[0];
			
			//alert(ip_init);
			if(ip_init=='201'){currentCountry = 'Costa Rica';}
			else{currentCountry = 'Internacional';}
			//alert(currentCountry);
			//www.destinostv.co.cr
			//if(host == 'destinoscr.incompanysolutions.com' && currentCountry == 'Internacional'){
				//alert('situacion 1');
			if(host == 'www.destinostv.co.cr' && currentCountry == 'Internacional'){			
				window.location = 'http://www.destinostv.com/';
			}
			
			if(host == 'www.destinostv.com' && currentCountry == 'Costa Rica'){
				//alert('situacion 2');
				window.location = 'http://www.destinostv.co.cr/';
			}
					
			//if(host=='dCr'){currentCountry = 'Costa Rica';}
			//else{currentCountry = 'Internacional';}
		}
	}

	if(currentCountry !='Costa Rica'){
		
	
		hideCrContent();
		
		$('#currentCountry').html(currentCountry);
		$('.changeCountry').click(function(){window.location='/cambiarPais?redirect=';}); 
	}
	
	$('#currentCountry').html(currentCountry); 
	 
	$('.changeCountry').click(function(){window.location='/cambiarPais?redirect=false';}); 
	 
}

function runSlides(){
	if($('#slides').html()!=null){
	  	$('#slides').slides({
				preload: true,
				preloadImage: 'img/loading.gif',
				play: 5000,
				pause: 2500,
				hoverPause: true
			});
	}
}

/////////////////////////////
//// Menu Functions
/////////////////////////////


function sideNavHeightsAndPosition(){
	if($('.sideNav').html()!=null){
		 
	 
	$('.sideNav>li>div>div>ul').each(function(){
		var tallest = 0;
	
		$(this).children().each(function(i){
		
		if($(this).find('li').html!=null){
		
			if (tallest < $(this).height()) { 
				tallest = $(this).height(); 
			}
		}
	});
	
		$(this).find('>li').css('height', tallest);
	
	});
	
	 
	}
	 
	
	$('.sideNav>li>div').each(function(){
		
		
		
		var h=$(this).height();
		var p=$(this).offset().top;
		var t = $(window).height();
		
		
		if(p + 100 > t ){
		   $(this).css('top','-'+ h/1.3 +'px');
		} 
		else if(p + h > t){
			$(this).css('top','-'+h/2 +'px');
		}
		
	});
	
}



/////////////////////////////
//// Product functions
/////////////////////////////

function swapComaToList(){
	$('.coma1').each(function()
   	{
		var custom3 = $(this).html();
	 
		var arr = custom3.split(',');
		var result='';
		if(arr.length>1){
			result = arr[0];
		}
		$(this).html(result);		
		
	});
	
	$('.coma2').each(function()
   	{
		var custom3 = $(this).html();
	 
		var arr = custom3.split(',');
		var result='';
		if(arr.length>2){
			result = arr[1];
		}
		$(this).html(result);		
	});
	
}

function showProductDetailsForPaquetes(){
	
	
	//ul's del small product
	$('.paquetePreview').each(function()
   	{  
		var features = $(this).html().split(',');
		
		var lis="<ul>";
		var l=features.length;
		if(l>4){l=4;}
		for (i=0;i<l;i++)
		{
		 lis +='<li>'+ features[i] +'</li>';
		}
		 
		lis +='</ul>';
		$(this).empty();
		$(this).html(lis); 
		
	});
	
	//Ul's del Large Product
	$('#features #details').each(function()
		{  
			var features = $(this).html().split(',');		
			var lis="<ul>";
			for (i=0;i<features.length;i++)
			{
			 lis +='<li>'+ features[i] +'</li>';
			}
			lis +='</ul>';
			$(this).empty();
			$(this).html(lis); 
			
		});
	
}

function showProductDetailsForProgramas(){
	  
	 
}

function showCustomProductDetails(){
	swapComaToList();
	
	$('.coma1').show();
	$('.coma2').show();

	if(currentPage == 'paquetes'){
		showProductDetailsForPaquetes()
	}
	
	else if(currentPage == 'programas'){
		showProductDetailsForProgramas()
	}
	
	else if (currentPage == 'Default.aspx'){
		showProductDetailsForPaquetes();
		$('.programaPreview').hide();
	}
	
	else{
	if(currentPage.length>8){
		if(currentPage.substr(0,8)=='_webapp_'){
			$('body').addClass('programas');
			$('.paquetePreview').hide();
			$('.buy').hide();
			$('.price').hide();
		}
	}
	}
	
	}



/////////////////////////////
//// Country Functions
/////////////////////////////


function showAllProducts(selector){
	$(selector).css('visibility','visible');
}

function showProductsInCurrentCountry(selector){
	$('.hideOtherCountry ' + selector).each(function()
   	{  
		if($(this).find('.price').html() ==''){
			$(this).remove();
		}
		else{
			$(this).css('visibility','visible');
		}
	});
}

function displayInDollars(){
	//fix currency Costa Rica in $
	if($('.price').html() != null){
		if($(".price:contains('₡')").html()!=null){
			$(".price:contains('₡')").replaceWith(function(){
				var str = $(this).html();
				str =str.replace('₡','$');
				return '<h3>'+str+'</h3>';
			});
		}
	}
}
 
/////////////////////////////
//// Display Correction Functions
/////////////////////////////
 
function showAutomaticBreadcrum(){
	//Fix Breadcrum > First Menu Category
	//Removes link to first level Catalog 
	if($('.breadBox.auto').html()!=null){
		$('.breadBox h2 a:first').remove();
		 
		var temp =  $('.breadBox h2').html();
		temp = temp.substring(2);
		temp +=' -';
		temp = temp.replace(':','>');
		$('.breadBox h2').html(temp);
	}
}

function removeFeaturedProductTagMarkUp(selector){
//is in Paquete home ( using {Featured Product List} Tag	
		//Removes Markup that BC inserts in Product Items
		removeMarkup(selector + ' .productfeaturelist');
   	 	$(selector + ' .productItem').remove();
}

function removeProductCatalogMarkup(){
	//Is in Catalogo using {Product List} from selected Catalog
		
		$('.productTable').each(function(){
		
			var text='<ul id="list">';
 
			//BC puts class productItem on each <TD>
			$(this).find('.item').each(function()
			{   
				//get html inside TD eg <li>
				text += '<li class="item">'+$(this).html()+"</li>";		
			});	
		 
	 	
			 //closes ul
			 text +='</ul>';
			 //sets ul to div 
			 $(this).before(text);
			 //Clears HTML ( quicker method than remove)
			 $(this).remove();
		});
}
 
 
//Sets menu selected item correctly for sub-pagr
//must have a templated to each sub Page
function displayCurrentPageInMainMenu(){
	if($('#menu li.selected').html()==null){
		//is in sub page
		$('#menu li a').each(function()
   		{   	 
			if($(this).html().toLowerCase() == currentPage.toLowerCase()){
				$(this).parent().addClass('selected');
			}
		});
					
		//If no selected Menu Page = selected Index
		if($('#menu li.selected').html()==null){
			$('#menu li:first').addClass('selected');
		}
	
	}
}

 

//Remove an the outter mark up of an object
function removeMarkup(selector){
	 $(selector).children().insertBefore(selector);
	$(selector).remove();
}


 
/////////////////////////////
//// Cross Domain Cookie Functions
/////////////////////////////
 
 
function icreateCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}


function ireadCookie(name){
	//alert(document.cookie);
	var nameEQ = name + "=";var ca = document.cookie.split(';');
    for(var i=0;i<ca.length;i++){var c = ca[i];	while (c.charAt(0)==' ') {c = c.substring(1,c.length);}	if (c.indexOf(nameEQ) == 0) {return c.substring(nameEQ.length,c.length);}}
	return null;
}


function ieraseCookie(name){createCookie(name,"",-1);}
			

function getParameterByName(name) {

   	var match = RegExp('[?&]' + name + '=([^&]*)')
              	.exec(window.location.search);

  	return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

}


function getParameterByName(name) {

   					 var match = RegExp('[?&]' + name + '=([^&]*)')
                    				.exec(window.location.search);

  					  return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

				}


function loadCurrentPage(){
//Get Current URL
		var _host = document.location.host;
		var _url1 = document.location.href;
		var index = _url1.indexOf(_host);
		
		
		if(index > -1){
			indexBad = _url1.indexOf('?');
			if(indexBad==-1){
				indexBad = _url1.indexOf('#');
			}
			
			if(indexBad>-1){
				 _url1 = _url1.substring(0,indexBad); 
			}
			
			_url1 = _url1.substring(index+_host.length + 1);
			
			index = _url1.indexOf('/');
			var indexLevel2 =0;
			var secondLevel='';
			if(index > -1){
				currentPage = _url1.substring(0,index);
				if(_url1.length>index){
					var secondLevel = _url1.substring(index+1);
				}
				indexLevel2 = secondLevel.indexOf('/');
			}
			else{
				//we are un root
				currentPage = _url1;	
			}
			
			if(indexLevel2>0 || secondLevel.length>0){
				currentPageLevel2 = secondLevel.substring(0,index);
			}	
		}	
}
 

