//GLOBAL VARIABLES
//Number of panels
var totalPanels=1;
// Number of panel at a time (2 or 3)
var tot=1;
// Current Panel
var startPanel=1;
var firstTime=0;

/*/
	 This page changes the image for the selected page
**/
function checkCurrentPage()
{
   jQuery("p.pager > img").attr('src','img/selected.jpg');
   
   jQuery("p.pager > img").each(function(index, value)
   {
      if(startPanel==tot*index+1)
	  jQuery(this).attr('src','img/listed.jpg');
   });

}

/*
 This page builts the pager
*/
function loadPager() {

	//calculate the required pages
	var totalPages= parseInt(totalPanels/tot);
	if((totalPanels%tot)>0)
	{
	  //add an extra page
	  totalPages++;
	}  

	var pager="";
	var i=0;
	var indice=0;

	for (i=0;i<totalPages;i++)
	{
	   if(parseInt(startPanel/tot)==i)
	   {
		 pager+='	<img title="Page '+(i+1)+'" data-moveto="'+indice+'" data-index="'+i+'" data-moveby="'+tot+'" data-select="img/listed.jpg" data-over="img/selected.jpg" class="bullet" src="img/listed.jpg" style="cursor: pointer;">';
	   }
	   else
	   {
		pager+='	<img title="Page '+(i+1)+'" data-moveto="'+indice+'" data-index="'+i+'" data-moveby="'+tot+'" data-select="img/listed.jpg" data-over="img/selected.jpg" class="bullet" src="img/selected.jpg" style="cursor: pointer;">';
		}
	   indice+=tot;
	}
	jQuery('#mygallery-paginate').html(pager); 
	jQuery(".bullet").unbind('click');
	jQuery(".bullet").click(function()
	{
	 jQuery("p.pager > img").attr('src','img/selected.jpg');
	  jQuery(this).attr('src','img/listed.jpg');
	});
	
	/**
	Enablibng SWIPPING
	**/
	try {
		if(firstTime==0) {
			if(jQuery().swipe()) {
				jQuery(".panel").swipe({
					threshold: {
						x: 5,
						y: 300
					},
					swipeRight: function() {
						stepcarousel.stepBy('mygallery',-1*tot);
						checkCurrentPage();
					},
					swipeLeft: function() {
						if(startPanel+tot>totalPanels) {
							stepcarousel.stepTo('mygallery',1);
						} else {
							stepcarousel.stepTo('mygallery',startPanel+tot);
						}
						checkCurrentPage();
					}
				});						
				firstTime++;
			}
		} 
	} catch(e){}	
}

/**
 This function is triggered when the IPAD is turned
**/
function test()
{

	//jQuery.noConflict();
	//alert(jQuery(".main-page").width());
	var co=startPanel;
	tot= parseInt((jQuery(".main-page").width()+30)/(jQuery(".panel").width()));
	//alert(tot);
	if(tot>3)
	{
		tot=3;
	}
	
	
	mo=parseInt((co-1)/tot)*tot;
	//alert("deberia mover al panel "+mo);
	//calculate the required pages
	var totalPages= parseInt(totalPanels/tot);
	if((totalPanels%tot)>0)
	{
		//add an extra page
		totalPages++;
	}  
	var ta =jQuery(".bullet").attr("data-moveby",tot.toString());
	jQuery('#mygallery').css("width",(tot*(jQuery(".panel").width()+5)));
	//javascript:stepcarousel.stepTo('mygallery', -1);
	jQuery("#left-button").unbind('click');
	jQuery("#right-button").unbind('click');
	jQuery("#left-button").click(function() {
		javascript:stepcarousel.stepBy('mygallery',-1*tot);
		checkCurrentPage();
	});
	jQuery("#carousel-container").width(jQuery("#left-button").width()+jQuery("#right-button").width()+jQuery('#mygallery').width());	 
	jQuery(".promos-section").width(jQuery('#mygallery').width());	
	
	//aca esta el problema
	javascript:stepcarousel.stepTo('mygallery',(parseInt(co/tot)*tot)+1);
	
	
	jQuery("#right-button").click(function (){
		if(startPanel+tot>totalPanels)
		{
		   javascript:stepcarousel.stepTo('mygallery',1);
		}
		else
		{
			 javascript:stepcarousel.stepTo('mygallery',startPanel+tot);
		}
		checkCurrentPage();
	});
	loadPager();
//alert(jQuery('.slideshow').height());
//window.location.reload();
}
/**
SWIPE FUNCTIONS
**/
function swipeLeft()
{
  alert("left");
}
function swipeRight()
{
  alert("right");
}



/* Setup main Menu */
function mainMenuSetup(){
  
  // get menu from XML
  jQuery.ajax({
    type: "GET",
    url: "xml/components/categories/pagenav.xml",
    dataType: "xml",
    success: processAjaxMenu
  });
}
/* Processing ajax requests */
function processAjaxMenu(xml){
  var output = '';
  var categories = jQuery(xml).find('homelist').find('categories').find('category');
  jQuery.each(categories, processCategoryMenu);
 
  
}
/* Process each navigation menu */
function processCategoryMenu(key, value){
  var bgcolor = jQuery(this).attr('bgcolor');
  var category = jQuery(this).text();
  if (category == 'All'){
    category = 'Featured';
  }
  //first is home
  if(key==0)
  {
     jQuery('#main-menu ul.items').append('<li class="category-menu selected-menu" data-cat="'+category+'" onclick="goToPage(\''+category+'\')"  style="background-color:#' + bgcolor + ';"><a href="#"><span>' + category + '</span></a></li>');
  }
  
  else
  {
	  if (category != ''){
		jQuery('#main-menu ul.items').append('<li class="category-menu" data-cat="'+category+'" onclick="goToPage(\''+category+'\')"  style="background-color:#' + bgcolor + ';"><a href="/browse-all-programs.html?cat='+category+'"><span>' + category + '</span></a></li>');
	  }
  }	  
}


/**
	This function check the current page and loads the corresponding categories display based on get variable
**/
function checkURL()
{
  var page = getUrlVars()["page"];
  //page=page.replace("/%20", "/ ");
  page=page.replace(/%20/g, " ");
  //CHecks variable and display variable 
  goToPage(page);
  
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    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;
}



/**
  this function loads the page based on the given category
**/
function goToPage(category)
{
   //alert(category);
}

/**
 Create categories pages based on the nav XML and the series contents
**/
function createSubpages()
{
 //alert("test");
   //Read categories from menu
   jQuery('.category-menu').each(function()
   {
    var category=(jQuery(this).attr("data-cat"));
	//this variable allows to detect when is necessarty to diplsay homepage
	var total_series=0;
	//iterate over panels and then copy matching panels
	jQuery('.panel').each(function(index,panel)
	{
		   //if(jQuery(this))
	});
	
   });
   
   
   

}





/**
When the page is loaded
-The main navigation bar is lodaded. It checks get variables in order to see if a category page needs to be displayed
otherwise it will show the slider homepage
-the screen is measured and the slider is adjusted to  show the required panels.
-The pager is builded

**/
jQuery(document).ready(function(jQuery)
{

    //hide main containers
	
	//jQuery("#carousel-container").hide();
	
    //create menu
	mainMenuSetup();
	
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') 
	{
	}
	else
	{
		jQuery("#bottom").css('position','fixed');
	}
	/* THIS IS PROCESSED IN AJAX
	//create categories pages
	createSubpages();
	
	//Check current URL and loads correspnding page
	checkURL();*/
	
	var content="";
	var panels="";
	tot= parseInt((jQuery(".main-page").width()+30)/(jQuery(".panel").width()));
	if(tot>3)
	{
		tot=3;
	}
	var ta =jQuery(".bullet").attr("data-moveby",tot.toString());
	jQuery('#mygallery').css("width",(tot*(jQuery(".panel").width()+5)));
	javascript:stepcarousel.stepTo('mygallery', -1);
	jQuery("#left-button").click(function() {
		javascript:stepcarousel.stepBy('mygallery',-1*tot);
		checkCurrentPage();
	});
	jQuery("#right-button").click(function (){
		if(startPanel+tot>totalPanels)
		{
			javascript:stepcarousel.stepTo('mygallery',1);
		}
		else
		{
			 javascript:stepcarousel.stepTo('mygallery',startPanel+tot);
		}
		checkCurrentPage();
	});
	jQuery("#carousel-container").width(jQuery("#left-button").width()+jQuery("#right-button").width()+jQuery('#mygallery').width());	
	//** LOAD PROMOS
	var promos="";
 	jQuery.ajax(
	{
		type: "GET",
		url: "xml/home-page/promoinfo.xml",
		dataType: "xml",
		success: function(xml)
		{
			jQuery(xml).find('area').each(function(){
			promos+="<div class='slideshow promo-slider'>";
			jQuery(this).find('promo').each(function() {
			    
				var pageLink=jQuery(this).find('pageLink').text();
				promos+="<div class='promo'>";
				var header=jQuery(this).find('header').text();
				var image=jQuery(this).find('image').text();
				var promotext=jQuery(this).find('promotext').text();
					promos+="<div class='promo-thumb'>";
						promos+="<a href='"+pageLink+"'><img src='"+image+"'/></a>";
						promos+="</div>";
						promos+="<div class='promo-text'><a href='"+pageLink+"'>";	
							promos+="<span class='header'>"+header+'</span><br>';
							promos+=promotext+'<br>';
						promos+="</div></a>";
					promos+="</div>";
				});
				promos+="</div>" ;
			
			});
			jQuery(".promos-section").html(promos);
			jQuery(".promos-section").width(jQuery('#mygallery').width());	
			jQuery('.slideshow').cycle({
				fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			});
	    }
	});
});


