/**************************************************/
var year = 2011;
var limit = 15;
var limit_meniu = 15;
var page = 1;
var page_meniuri = 1;
var start = 0;
var subcateg = 0;
var no_prods = 0;
function do_pagination(){
	var adjacents = 1;
	if(page) {
		start = (page - 1) * limit; 			//first item to display on this page
	}else{
		start = 0;
	}
	if (page == 0) page = 1;					//if no page var is given, default to 1.
	prev = page - 1;							//previous page is page - 1
	next = page + 1;							//next page is page + 1

	lastpage = Math.ceil(total_pages/limit);	//lastpage is = total pages / items per page, rounded up.

	lpm1 = lastpage - 1;
	var pagination = "";
	if(lastpage > 1)
	{
		pagination = pagination + "<div class=\"pagination\">";
		//previous button
		if (page > 1)
			pagination = pagination+ "<li id=\"f-prev\" onclick=\"change_page("+prev+");return false;\"></li>";
		else
			pagination = pagination+ "<li id=\"f-prev\"></li>";

		//pages
		if (lastpage < 7 + (adjacents * 2))	//not enough pages to bother breaking it up
		{
			for (counter = 1; counter <= lastpage; counter++)
			{
				if (counter == page)
					pagination = pagination+ "<li class=\"curent\">"+counter+"</li>";
				else
					pagination = pagination+ "<a href=\"#\" onclick=\"change_page("+counter+");return false;\"><li href=\"#\">"+counter+"</li></a>";
			}
		}
		else{
			if(lastpage > 5 + (adjacents * 2))	//enough pages to hide some

		{
			//close to beginning; only hide later pages
			if(page < 1 + (adjacents * 2))
			{
				for (counter = 1; counter < 4 + (adjacents * 2); counter++)
				{
					if (counter == page)
						pagination = pagination+ "<li class=\"curent\">"+counter+"</li>";
					else
						pagination = pagination+ "<a href=\"#\" onclick=\"change_page("+counter+");return false;\">"+counter+"</a>";
				}
				pagination = pagination+ "...";
				pagination = pagination+ "<a href=\"#\" onclick=\"change_page("+lpm1+");return false;\">"+lpm1+"</a>";
				pagination = pagination+ "<a href=\"#\" onclick=\"change_page("+lastpage+");return false;\">"+lastpage+"</a>";
			}
			else{
				if(lastpage - (adjacents * 2) > page && page > (adjacents * 2))

				{
				pagination = pagination+ "<a href=\"#\" onclick=\"change_page(1);return false;\">1</a>";
				pagination = pagination+ "<a href=\"#\" onclick=\"change_page(2);return false;\">2</a>";
				pagination = pagination+ "...";
				for (counter = page - adjacents; counter <= page + adjacents; counter++)
				{
					if (counter == page)
						pagination = pagination+ "<li class=\"curent\">"+counter+"</li>";
					else
						pagination = pagination+ "<a href=\"#\" onclick=\"change_page("+counter+");return false;\">"+counter+"</a>";
				}
				pagination = pagination+ "...";
				pagination = pagination+ "<a href=\"#\"  onclick=\"change_page("+lpm1+");return false;\">"+lpm1+"</a>";
				pagination = pagination+ "<a href=\"#\"  onclick=\"change_page("+lastpage+");return false;\">"+lastpage+"</a>";
			}else{
				pagination = pagination+ "<a href=\"#\"  onclick=\"change_page(1);return false;\"><li href=\"#\">1</li></a>";
				pagination = pagination+ "<a href=\"#\"  onclick=\"change_page(2);return false;\"><li href=\"#\">2</li></a>";
				pagination = pagination+ "...";
				for (counter = lastpage - (2 + (adjacents * 2)); counter <= lastpage; counter++)
				{
					if (counter == page)
						pagination = pagination+ "<li class=\"curent\">"+counter+"</li>";
					else
						pagination = pagination+ "<a href=\"#\"  onclick=\"change_page("+counter+");return false;\">"+counter+"</a>";
				}
			}
		}}
		}
		//next button
		if (page < counter - 1){
			pagination = pagination+ "<li onclick=\"change_page("+next+");return false;\" id=\"f-next\"></li>";
		}else{
			pagination = pagination+ "<span class=\"disabled\"><li href=\"#\" id=\"f-next\"></li></span>";
		}
		pagination = pagination+ "</div>\n";

		$("#pagination").html(pagination);
	}else{
		$("#pagination").html('');
	}

}
function do_pagination_meniuri(){
	var adjacents = 1;
	if(page_meniuri) {
		start = (page_meniuri - 1) * limit_meniuri; 			//first item to display on this page
	}else{
		start = 0;
	}
	if (page_meniuri == 0) page_meniuri = 1;					//if no page var is given, default to 1.
	prev_meniuri = page_meniuri - 1;							//previous page is page - 1
	next_meniuri = page_meniuri + 1;							//next page is page + 1

	lastpage_meniuri = Math.ceil(total_pages_meniuri/limit_meniuri);	//lastpage is = total pages / items per page, rounded up.

	lpm1_meniuri = lastpage_meniuri - 1;
	var pagination = "";
	if(lastpage_meniuri > 1)
	{
		pagination = pagination + "<div class=\"pagination\">";
		//previous button
		//alert('page_meniuri ' +page_meniuri);
		if (page_meniuri > 1)
			pagination = pagination+ "<li id=\"f-prev\" onclick=\"change_page_meniuri("+prev_meniuri+");return false;\"></li>";
		else
			pagination = pagination+ "<li id=\"f-prev\"></li>";
		//pages
		if (lastpage_meniuri < 7 + (adjacents * 2))	//not enough pages to bother breaking it up
		{
			for (counter = 1; counter <= lastpage_meniuri; counter++)
			{
				if (counter == page_meniuri)
					pagination = pagination+ "<li class=\"curent\">"+counter+"</li>";
				else
					pagination = pagination+ "<a href=\"#\" onclick=\"change_page_meniuri("+counter+");return false;\"><li href=\"#\">"+counter+"</li></a>";
			}
		}
		else{
			if(lastpage_meniuri > 5 + (adjacents * 2))	//enough pages to hide some

		{
			//close to beginning; only hide later pages
			if(page_meniuri < 1 + (adjacents * 2))
			{
				for (counter = 1; counter < 4 + (adjacents * 2); counter++)
				{
					if (counter == page_meniuri)
						pagination = pagination+ "<li class=\"curent\">"+counter+"</li>";
					else
						pagination = pagination+ "<a href=\"#\" onclick=\"change_page_meniuri("+counter+");return false;\">"+counter+"</a>";
				}
				pagination = pagination+ "...";
				pagination = pagination+ "<a href=\"#\" onclick=\"change_page_meniuri("+lpm1_meniuri+");return false;\">"+lpm1_meniuri+"</a>";
				pagination = pagination+ "<a href=\"#\" onclick=\"change_page_meniuri("+lastpage_meniuri+");return false;\">"+lastpage_meniuri+"</a>";
			}
			else{
				if(lastpage_meniuri - (adjacents * 2) > page_meniuri && page_meniuri > (adjacents * 2))

				{
				pagination = pagination+ "<a href=\"#\" onclick=\"change_page_meniuri(1);return false;\">1</a>";
				pagination = pagination+ "<a href=\"#\" onclick=\"change_page_meniuri(2);return false;\">2</a>";
				pagination = pagination+ "...";
				for (counter = page_meniuri - adjacents; counter <= page_meniuri + adjacents; counter++)
				{
					if (counter == page_meniuri)
						pagination = pagination+ "<li class=\"curent\">"+counter+"</li>";
					else
						pagination = pagination+ "<a href=\"#\" onclick=\"change_page_meniuri("+counter+");return false;\">"+counter+"</a>";
				}
				pagination = pagination+ "...";
				pagination = pagination+ "<a href=\"#\"  onclick=\"change_page_meniuri("+lpm1_meniuri+");return false;\">"+lpm1_meniuri+"</a>";
				pagination = pagination+ "<a href=\"#\"  onclick=\"change_page_meniuri("+lastpage_meniuri+");return false;\">"+lastpage_meniuri+"</a>";
			}else{
				pagination = pagination+ "<a href=\"#\"  onclick=\"change_page_meniuri(1);return false;\"><li href=\"#\">1</li></a>";
				pagination = pagination+ "<a href=\"#\"  onclick=\"change_page_meniuri(2);return false;\"><li href=\"#\">2</li></a>";
				pagination = pagination+ "...";
				for (counter = lastpage_meniuri - (2 + (adjacents * 2)); counter <= lastpage_meniuri; counter++)
				{
					if (counter == page_meniuri)
						pagination = pagination+ "<li class=\"curent\">"+counter+"</li>";
					else
						pagination = pagination+ "<a href=\"#\"  onclick=\"change_page_meniuri("+counter+");return false;\">"+counter+"</a>";
				}
			}
		}}
		}
		//next button
		if (page_meniuri < counter - 1){
			pagination = pagination+ "<li onclick=\"change_page_meniuri("+next_meniuri+");return false;\" id=\"f-next\"></li>";
		}else{
			pagination = pagination+ "<span class=\"disabled\"><li href=\"#\" id=\"f-next\"></li></span>";
		}
		pagination = pagination+ "</div>\n";

		$(".pagination_meniuri").html(pagination);
	}else{
		$(".pagination_meniuri").html('');
	}
}
function IsNumeric(val) {

    if (isNaN(parseFloat(val))) {
          return false;
     }
     return true;
}


	function initMenu() {
		$('#subcategs ul').hide();
		$('#subcategs ul:first').show();
		$('#subcategs li a').click(
		function() {
		  var checkElement = $(this).next();
		  if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
		    return false;
		    }
		  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		    $('#subcategs ul:visible').slideUp('normal');
		    checkElement.slideDown('normal');
		    return false;
		    }
		  }
		);
	}
	function populate_meniuri(shop,mcat,subcat,start,off){
		$.post(base_url+"magazine/request_magazine_meniuri", { shop: shop, mcat:mcat, subcat:subcat, start: start,off:off },
		   function(data) {
		   	 subcateg = subcat;

		   	 if(subcateg>0){
		   	 	if(IsNumeric(data.substr(0,1)) && IsNumeric(data.substr(1,1))){
		   	 		total_pages = data.substr(0,1)+data.substr(1,1);
		   	 	}else{
		   	 		total_pages = data.substr(0,1);
		   	 	}

		   	 	do_pagination_meniuri();
		   	 }

		   	 if(IsNumeric(data.substr(0, 1)) && IsNumeric(data.substr(1, 1))){
		   	 	data = data.substr(2);
		   	 }else{
		   	 	data = data.substr(1);
		   	 }
		     $('.meniuri').html(data);
		     if(data=='<center><b>Vizitati magazinul pentru gama de produse</b></center>'){
		    	$(".next-prev").hide();
		     }else{
		     	$(".next-prev").show();
		     }
		     TB_init();
		     do_prod_url();
		   });

	}
	function populate_personaje(shop,mcat,subcat,start,off){
		$.post(base_url+"magazine/request_magazine_personaje", { shop: shop, mcat:mcat, subcat:subcat, start: start,off:off },
		   function(data) {
		   	 subcateg = subcat;

		   	 if(subcateg>0){
		   	 	if(IsNumeric(data.substr(0,1)) && IsNumeric(data.substr(1,1))){
		   	 		total_pages = data.substr(0,1)+data.substr(1,1);
		   	 	}else{
		   	 		total_pages = data.substr(0,1);
		   	 	}

		   	 	do_pagination();
		   	 }

		   	 if(IsNumeric(data.substr(0, 1)) && IsNumeric(data.substr(1, 1))){
		   	 	data = data.substr(2);
		   	 }else{
		   	 	data = data.substr(1);
		   	 }
		     $('#prods_container').html(data);
		     if(data=='<center><b>Vizitati magazinul pentru gama de produse</b></center>'){
		    	$(".next-prev").hide();
		     }else{
		     	$(".next-prev").show();
		     }
		     TB_init();
		     do_prod_url();
		   });

	}
	function populate(shop,mcat,subcat,start,off){
		$.post(base_url+"magazine/request_magazine", { shop: shop, mcat:mcat, subcat:subcat, start: start,off:off },
		   function(data) {
		   	 subcateg = subcat;

		   	 if(subcateg>0){
		   	 	if(IsNumeric(data.substr(0,1)) && IsNumeric(data.substr(1,1))){
		   	 		total_pages = data.substr(0,1)+data.substr(1,1);
		   	 	}else{
		   	 		total_pages = data.substr(0,1);
		   	 	}

		   	 	do_pagination();
		   	 }

		   	 if(IsNumeric(data.substr(0, 1)) && IsNumeric(data.substr(1, 1))){
		   	 	data = data.substr(2);
		   	 }else{
		   	 	data = data.substr(1);
		   	 }
		     $('#prods_container').html(data);
		     if(data=='<center><b>Vizitati magazinul pentru gama de produse</b></center>'){
		    	$(".next-prev").hide();
		     }else{
		     	$(".next-prev").show();
		     }
		     TB_init();
		     do_prod_url();
		   });

	}
	function populate_shops(cat,subcat,start,off){
		$.post(base_url+"magazine/request_shops", { categ: cat,subcat:subcat, start: start,off:off },
		   function(data) {
		    $('#new_shop_container').html(data);
		     TB_init();
		   });
	}
	function trigger_luna_comunicate(val){
	   $.post(base_url+"pressroom/request_comunicate", { year:year,month:val},
	   function(data) {
	    $('#press_container').html(data);
	     TB_init();
	   });
	   setTimeout('resetPressPages()',500);
	}
	function trigger_luna(val){
	   $.post(base_url+"events/request_events", { year:year,month:val},
	   function(data) {
	    $('#home_events').html(data);
	     TB_init();
	   });
	}
function xtractNums(str){
   var patt1=/\d+/g;
   return str.match(patt1);
}


$(document).ready(function() {

	initMenu();
	TB_init();
	if(year == 2011){
		$('#left_arrow_sqare img').hide();
	}
	$('#right_arrow_sqare img').click(function(){
		$('#center_year_content').html('2012');
		$('#right_arrow_sqare img').hide();
		$('#left_arrow_sqare img').show();
		year=2012;
		val = $('#luna').val();
		$.post(base_url+"events/request_events", { year:year,month:val},
		   function(data) {
		    $('#home_events').html(data);
		     TB_init();
		   });
	});
	$('#left_arrow_sqare img').click(function(){
		$('#center_year_content').html('2011');
		$('#left_arrow_sqare img').hide();
		$('#right_arrow_sqare img').show();
		year=2011;
		val = $('#luna').val();
		$.post(base_url+"events/request_events", { year:year,month:val},
		   function(data) {
		    $('#home_events').html(data);
		     TB_init();
		   });
	});

$('#right_arrow_sqare_comunicate img').click(function(){
		$('#center_year_content_comunicate').html('2012');
		$('#right_arrow_sqare_comunicate img').hide();
		$('#left_arrow_sqare_comunicate img').show();
		year=2012;
		val = $('#luna_comunicate').val();
		$.post(base_url+"pressroom/request_comunicate", { year:year,month:val},
		   function(data) {
		    $('#press_container').html(data);
		     TB_init();
		   });
		setTimeout('resetPressPages()',500);
	});
	$('#left_arrow_sqare_comunicate img').click(function(){
		$('#center_year_content_comunicate').html('2011');
		$('#left_arrow_sqare_comunicate img').hide();
		$('#right_arrow_sqare_comunicate img').show();
		year=2011;
		val = $('#luna_comunicate').val();
		$.post(base_url+"pressroom/request_comunicate", { year:year,month:val},
		   function(data) {
		    $('#press_container').html(data);
		     TB_init();
		   });
		setTimeout('resetPressPages()',500);
	});



});
function do_prod_url(){

	if(window.location.href.indexOf("/prev")>= 0)
	{
		var url_str  = window.location;
		var url_parts = document.URL.split("/");
	    var id = xtractNums(url_parts[7]);
	    $("#view"+id).click();
	}
}
function get_shops_by_cat(order_abc,cat,floor,lang){
	 $.post(base_url+"harta/get_shops_by_request", {order_abc:order_abc,cat:cat,floor:floor,lang:lang},
	   function(data) {
	    $('#shops_container').html(data);
		if(cat=='7') {
			$('#shops_container ul ul').append('<li class="catlist"><a onclick="changeVal(\'513\');return false;" href="#" title="513">Loc de Joaca</a></li>');
		}
	   });

}
function get_shops_by_name(name,lang){
	 $.post(base_url+"harta/get_shops_by_name", {name:name,lang:lang},
	   function(data) {
	    $('#shops_container').html(data);
	   });
}


