var currentPage="/";
var currentPageLevel2='';

//Gets Current Page on Init


function init(){
	 
	loadCurrentPage();

	//window.alert(currentPage);

	renderSideMenu();
		 
	showCatalogMenu();

	changeComaForUi('.fullItem .features');
	
	renderBlog();
	
	removeProductCatalogMarkup();
		
	
	if(currentPage=='indexn.html' || currentPage == '/' || currentPage==''){
		$('#splashBox img').css('visibility','visible');
		$('#slides').slides({
				preload: true,
				preloadImage: 'img/loading.gif',
				play: 9000,
				pause: 2500,
				hoverPause: true
		});
		
		
		$('.splashItem').click(function(){
			window.location = $(this).find('#url').html();	
		});
		
	}
		 
	 
}
   
  
  function removeProductCatalogMarkup(){
	//Is in Catalogo using {Product List} from selected Catalog
		 
		if($('#relatedList').html()!=null){
	   		$('#relatedBar .box').html($('#relatedList').html()); 	 
	   		$('#relatedList').remove();
		}
		
		
		$('.box>.productTable').each(function(){
			 
			var text='<ul id="list">';
			//BC puts class productItem on each <TD>
			$(this).find('.listItem').each(function()
			{ 
			 
				//get html inside TD eg <li>
				text += '<li class="listItem">'+$(this).html()+"</li>";
			});	 	
			 //closes ul
			 text +='</ul><div class="clear">&nbsp;</div>';
			 //sets ul to div 
			 $(this).before(text);
			 
			 //Clears HTML ( quicker method than remove)
			 $(this).remove();
			 
		});
		
		 
		$('.productTable.productSmall').each(function(){
			var height=0;
			var text='<ul id="list">';
			//BC puts class productItem on each <TD>
			$(this).find('.listItem').each(function()
			{ 
				var currentHeight = $(this).height();
				if(height < currentHeight){
					height = currentHeight;
				}
				//get html inside TD eg <li>
				text += '<li class="listItem">'+$(this).html()+"</li>";
			});	 	
			 //closes ul
			 text +='</ul>';
			 //sets ul to div 
			 $(this).before(text);
			 
			 //Clears HTML ( quicker method than remove)
			 $(this).remove();
			 $('#list>.listItem').css('height',height);
		});
		
		
		$('#list>.productfeaturelist').each(function(){
			var height=0;
			var text='<ul id="tagList">';
			//BC puts class productItem on each <TD>
			$(this).find('.listItem').each(function()
			{ 
				var currentHeight = $(this).height();
				if(height < currentHeight){
					height = currentHeight;
				}
				//get html inside TD eg <li>
				text += '<li class="listItem">'+$(this).html()+"</li>";
			});	 	
			 //closes ul
			 text +='</ul>';
			 //sets ul to div 
			 $(this).before(text);
			 //Clears HTML ( quicker method than remove)
			 $(this).remove();
			 $('#tagList>.listItem').css('height' , height);
		});
		
	
	}
 
 

	function renderBlog(){
	
		$('.blogPostTitle > a').removeAttr('href');

		$('.postBody').css('display','none');
	
		 $('.relatedItem:last').addClass('last');
	
		$('.blogPost').click(function() {
			$('.postBody').hide(500);	
			$('.blogPost').removeClass('selected');
			$(this).find('.postBody').show(500);	
			$(this).addClass("selected");	
		 
		});

		$('.blogPost:first').click();
	
	}
	
	function showCatalogMenu(){
	
		if(currentPage=='tienda-hp'){
			$('#hp').show();	 
		}
		else if(currentPage=='tienda-epson'){
			$('#epson').show();	 
		}
		else if(currentPage=='tienda-toshiba'){
			$('#toshiba').show();	 
		}
		else if(currentPage=='tienda-linksys'){
			$('#linksys').show();	 
		}
		else{
			$('#btc').show();
		}
		 
	}

	function changeComaForUi(selector){
	 $(selector).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); 
			
		});
	}

//MUST BE INSIDE Div with id="sideBar" 
	function renderSideMenu(){
		
		$('#sideBar ul li').click(function(event){		
			
		var link = $(this).find('ul').html(); 
			
		//If is SubCategoy stop event Bubble to allow click
		if(link==null){
			event.stopPropagation();
		}	
		//If is Category
		else{
		//Hide All	
		$('#sideBar li ul').hide(); 
		//Show Current Category ( current category never collapses )
		$('#sideBar ul li.selected ul').show(); 
		//Show Clicked Category
		$(this).find('ul').show('fast');
		//Stop Link on Click
		return false;
		}
		
		 });
		 
		if($('#sideBar .selected').html()==null){ 
			$('#sideBar li ul').hide();
			$('#sideBar li:first').click();
		}
		else if($('#sideBar .selected ul').html()==null){ 
			$('#sideBar li ul').hide();
		}
		else{
			$('#sideBar .selected').click();
		}
	}
 
	function displayInDollars(){
		//fix currency Costa Rica in $
		if($('.largeItem h3').html() != null){
			if($("h3:contains('₡')").html()!=null){
				$("h3:contains('₡')").replaceWith(function(){
					var str = $(this).html();
					str =str.replace('₡','$');
					return '<h3>'+str+'</h3>';
				});
			}
		}
	}
 
	function showAutomaticBreadcrum(){
		//Fix Breadcrum > First Menu Category
		//Removes link to first level Catalog 
		$('.moduleBar h2 a:first').remove();
		
		var links = $('.moduleBar h2').html();
		
		if(links.length==0){
			$('.moduleBar h2').html(currentPage + ' de DestinosTV.com');
		}
		else{
			 $('.moduleBar h2').html(currentPage + ' en' + links);
		}
	}
	
	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();
	}
 
	//adds a regular MouseOver effect setting and removing .selected class 
	function addMouseOverEffect(selector){
		$(selector).mouseover(function() {
			$(this).addClass("rollOver");	
		});
		
		$(selector).mouseout(function() {
			$(this).removeClass("rollOver");	
		});
		
	}

	//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();
	}


	//Scrolls without scroll bars
	function doScroll(selector,dif,maxHeight){	
		var pos = $(selector).css('top');
		pos=pos.substring(0,pos.length-2);
		if(parseInt(pos) < 1 && parseInt(pos) > maxHeight * -1){
			var newPos= parseInt(pos)+ parseInt(dif);
			$(selector).css('top',newPos+'px');
		}
		
	}



	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);
				}
				 
				
				
			}
			
			//alert(currentPage);
			
	}


	//Adds a Tool Tip to the selected DOM Object, must have an attr tooltip="xxxxxxxx" and tool tip library added
	function addToolTip(selector){
	   $(selector).each(function()
	   {   
		  $(this).qtip({
			 content: $(this).attr('tooltip'), // Use the tooltip attribute of the element for the content
		   
		   
		   style:{
			   width:150,
			   border:{width:1,color:'#cccccc', radius:3},
			  
			  
			  tip:'topMiddle'
			   }
			   ,
			position:{
		   
	   adjust:{
		  y:-30,
		  x:-140 
		  }
			}
		  
		  });
	   });
	}
