$(document).ready(function() {
	init_slideshow();
	//home_highlight();
	//init_paging('#frontpage_tab',8);
	init_tabs();
	/*$(".home-tab-video").click(function(){
		$.get("/2012/templates/ajax_sup2011_v0/render_video_list.ajax.php", { db: "sup", limit: "100" },
		function(data){
			$("#VIDEO").append(data);
		});
	});*/
	$(".home-tab-article a").click(function(){
		$("#ARTICLES .item_list ul").empty();
		$("#ARTICLES .load").show();
		$.get("/2012/templates/ajax_sup2011_v0/render_wp_post_thumb.ajax.php", { category_name: "articles", 
		posts_per_page: "8", offset: "0",paged:"1"},
		function(data){
			$.get("/2012/templates/ajax_sup2011_v0/total_post_thumb.ajax.php", { category_name: "articles",
			posts_per_page: "8" },
				function(data){
					var articles_page = parseInt(data);
					var articles_data = '';
					for (i=1;i<=articles_page;i++)
					{
						if(articles_page==1){continue;}
						if(i==1){articles_data += '<li class="active" rel="'+i+'">'+i+'</li>';}
						if(i!=1){articles_data += '<li rel="'+i+'">'+i+'</li>';}
					}
					if(articles_page>1){articles_data += '<li id="next" rel="2"></li>';}
					$("#ARTICLES #articles_page ul").empty().append(articles_data);

					$("#articles_page li").click(function(){
						$("#articles_page li").removeClass("active");
						$(this).addClass("active");
						init_koobitor_articles_page($(this).attr('rel'),8);
					});
				});
			$("#ARTICLES .item_list ul").empty().append(data).highLight();
			$("#ARTICLES .load").hide();
		});
	});
	$(".home-tab-photo a").click(function(){
		$("#PHOTO .item_list ul").empty();
		$("#PHOTO .load").show();
		$.get("/2012/templates/ajax_sup2011_v0/render_photo_list.ajax.php", { paged: "1" , posts_per_page: "8"},
		function(data){
			$.get("/2012/templates/ajax_sup2011_v0/total_photo_list.ajax.php", { posts_per_page: "8" },
				function(data){
					var photo_page = parseInt(data);
					var photo_data = '';
					for (i=1;i<=photo_page;i++)
					{
						if(photo_page==1){continue;}
						if(i==1){photo_data += '<li class="active" rel='+i+'>'+i+'</li>';}
						if(i!=1){photo_data += '<li rel='+i+'>'+i+'</li>';}
					}
					if(photo_page>1){photo_data += '<li id="next" rel="2"></li>';}
					$("#PHOTO #photo_page ul").empty().append(photo_data);

					$("#photo_page li").click(function(){
						$("#photo_page li").removeClass("active");
						$(this).addClass("active");
						init_koobitor_photo_page($(this).attr('rel'),8);
					});
				});
			$("#PHOTO .item_list ul").empty().append(data).highLight();
			$("#PHOTO .load").hide();
		});
	});
	$(".home-tab-fitness a").click(function(){
		$("#FITNESS .item_list ul").empty();
		$("#FITNESS .load").show();
		$.get("/2012/templates/ajax_sup2011_v0/render_wp_post_thumb.ajax.php", { category_name: "fitness", 
		posts_per_page: "8", offset: "0",paged:"1"},
		function(data){
			$.get("/2012/templates/ajax_sup2011_v0/total_post_thumb.ajax.php", { category_name: "fitness" ,
			posts_per_page: "8"},
				function(data){
					var fitness_page = parseInt(data);
					var fitness_data = '';
					for (i=1;i<=fitness_page;i++)
					{
						if(fitness_page==1){continue;}
						if(i==1){fitness_data += '<li class="active" rel='+i+'>'+i+'</li>';}
						if(i!=1){fitness_data += '<li rel='+i+'>'+i+'</li>';}
					}
					if(fitness_page>1){fitness_data += '<li id="next" rel="2"></li>';}
					$("#FITNESS #fitness_page ul").empty().append(fitness_data);

					$("#fitness_page li").click(function(){
						$("#fitness_page li").removeClass("active");
						$(this).addClass("active");
						init_koobitor_fitness_page($(this).attr('rel'),8);
					});
				});
			$("#FITNESS .item_list ul").empty().append(data).highLight();
			$("#FITNESS .load").hide();
		});
	});
	$(".home-tab-tips a").click(function(){
		$("#TIPS_TECHNIQUES .item_list ul").empty();
		$("#TIPS_TECHNIQUES .load").show();
		$.get("/2012/templates/ajax_sup2011_v0/render_wp_post_thumb.ajax.php", { category_name: "tips-techniques", 
		posts_per_page: "8", offset: "0",paged:"1"},
		function(data){
			$.get("/2012/templates/ajax_sup2011_v0/total_post_thumb.ajax.php", { category_name: "tips-techniques",
			posts_per_page: "8" },
				function(data){
					var tips_page = parseInt(data);
					var tips_data = '';
					for (i=1;i<=tips_page;i++)
					{
						if(tips_page==1){continue;}
						if(i==1){tips_data += '<li class="active" rel='+i+'>'+i+'</li>';}
						if(i!=1){tips_data += '<li rel='+i+'>'+i+'</li>';}
					}
					if(tips_page>1){tips_data += '<li id="next" rel="2"></li>';}
					$("#TIPS_TECHNIQUES #tips_page ul").empty().append(tips_data);

					$("#tips_page li").click(function(){
						$("#tips_page li").removeClass("active");
						$(this).addClass("active");
						init_koobitor_tips_page($(this).attr('rel'),perpage);
					});
				});
			$("#TIPS_TECHNIQUES .item_list ul").empty().append(data).highLight();
			$("#TIPS_TECHNIQUES .load").hide();
		});
	});
	$(".home-tab-catalog a").click(function(){
		$("#CATALOG .item_list ul").empty();
		$.get("/2012/templates/ajax_sup2011_v0/render_catalog_list.ajax.php", { },
		function(data){
			$("#CATALOG").empty().append(data);
		});
	});
});
function init_koobitor_photo_page(page,perpage){
	$("#PHOTO.load").show();
	$.get("/2012/templates/ajax_sup2011_v0/render_photo_list.ajax.php", { paged: page , posts_per_page:perpage},
	function(data){
		$.get("/2012/templates/ajax_sup2011_v0/total_photo_list.ajax.php", {  posts_per_page:perpage },
				function(data){
					var photo_page = parseInt(data);
					var photo_data = '';
					if(page!=1){
						photo_data += '<li id="prev" rel="'+(page-1)+'"></li>';
					}
					for (i=1;i<=photo_page;i++)
					{
						if(photo_page==1){continue;}
						if(i==page){photo_data += '<li class="active" rel="'+i+'">'+i+'</li>';}
						if(i!=page){photo_data += '<li rel="'+i+'">'+i+'</li>';}
					}
					if(page<photo_page){
						photo_data += '<li id="next" rel="'+(parseInt(page)+1)+'"></li>';
					}
					$("#PHOTO #photo_page ul").empty().append(photo_data);

					$("#photo_page li").click(function(){
						$("#photo_page li").removeClass("active");
						$(this).addClass("active");
						init_koobitor_photo_page($(this).attr('rel'),perpage);
					});
				});
		$("#PHOTO .item_list ul").empty().append(data).highLight();
		$("#PHOTO .load").hide();
	});
}

function init_koobitor_tips_page(page,perpage){
	$("#TIPS_TECHNIQUES .load").show();
	$.get("/2012/templates/ajax_sup2011_v0/render_wp_post_thumb.ajax.php", { category_name: "tips-techniques" , 
	posts_per_page: "8", offset: "0",paged:page},
	function(data){
		$.get("/2012/templates/ajax_sup2011_v0/total_post_thumb.ajax.php", { category_name: "tips-techniques" ,
		posts_per_page:perpage},
				function(data){
					var tips_page = parseInt(data);
					var tips_data = '';
					if(page!=1){
						tips_data += '<li id="prev" rel="'+(page-1)+'"></li>';
					}
					for (i=1;i<=tips_page;i++)
					{
						if(tips_page==1){continue;}
						if(i==page){tips_data += '<li class="active" rel="'+i+'">'+i+'</li>';}
						if(i!=page){tips_data += '<li rel="'+i+'">'+i+'</li>';}
					}
					if(page<tips_page){
						tips_data += '<li id="next" rel="'+(parseInt(page)+1)+'"></li>';
					}
					$("#TIPS_TECHNIQUES #tips_page ul").empty().append(tips_data);

					$("#tips_page li").click(function(){
						$("#tips_page li").removeClass("active");
						$(this).addClass("active");
						init_koobitor_tips_page($(this).attr('rel'),perpage);
					});
				});
		$("#TIPS_TECHNIQUES .item_list ul").empty().append(data).highLight();
		$("#TIPS_TECHNIQUES .load").hide();
	});
}
function init_koobitor_fitness_page(page,perpage){
	if((perpage==8 && page == 1) || (perpage==16 && page == 1))$("#FITNESS .load").show();
	if(perpage==24 && page == 1)$("#FITNESS .load16").show();
	$.get("/2012/templates/ajax_sup2011_v0/render_wp_post_thumb.ajax.php", { category_name: "fitness" , 
	posts_per_page: perpage, offset: "0",paged:page},
	function(data){
		$.get("/2012/templates/ajax_sup2011_v0/total_post_thumb.ajax.php", { category_name: "fitness" , 
		posts_per_page:perpage},
			function(data){
				var fitness_page = parseInt(data);
				var fitness_data = '';
				if(page!=1){
					fitness_data += '<li id="prev" rel="'+(page-1)+'"></li>';
				}
				for (i=1;i<=fitness_page;i++)
				{
					if(fitness_page==1){continue;}
					if(i==page){fitness_data += '<li class="active" rel="'+i+'">'+i+'</li>';}
					if(i!=page){fitness_data += '<li rel="'+i+'">'+i+'</li>';}
				}
				if(page<fitness_page){
					fitness_data += '<li id="next" rel="'+(parseInt(page)+1)+'"></li>';
				}
				$("#FITNESS #fitness_page ul").empty().append(fitness_data);

				$("#fitness_page li").click(function(){
					$("#fitness_page li").removeClass("active");
					$(this).addClass("active");
					init_koobitor_fitness_page($(this).attr('rel'),perpage);
				});
			});
		$("#FITNESS .item_list ul").empty().append(data).highLight();
		if((perpage==8 && page == 1) || (perpage==16 && page == 1))$("#FITNESS .load").hide();
		if(perpage==24 && page == 1)$("#FITNESS .load16").hide();
	});
}
function init_koobitor_articles_page(page,perpage){
	if((perpage==8 && page>=1) || (perpage==16 && page>=1))$("#ARTICLES .load").show();
	if(perpage>16 && page==1){
		var temp = "#ARTICLES .load"+(parseInt(perpage)-8);
		$(temp).show();
	}
	if(perpage>=16 && page>=1){
		var temp = "#ARTICLES .load"+(parseInt(perpage));
		$(temp).show();
	}
	$.get("/2012/templates/ajax_sup2011_v0/render_wp_post_thumb.ajax.php", { category_name: "articles" , 
	posts_per_page: perpage, offset: "0",paged:page},
	function(data){
		$.get("/2012/templates/ajax_sup2011_v0/total_post_thumb.ajax.php", { category_name: "articles" , 
		posts_per_page:perpage },
			function(data){
				var articles_page = parseInt(data);
				var articles_data = '';
				if(page!=1){
					articles_data += '<li id="prev" rel="'+(page-1)+'"></li>';
				}
				for (i=1;i<=articles_page;i++)
				{
					if(articles_page==1){continue;}
					if(i==page){articles_data += '<li class="active" rel="'+i+'">'+i+'</li>';}
					if(i!=page){articles_data += '<li rel="'+i+'">'+i+'</li>';}
				}
				if(page<articles_page){
					articles_data += '<li id="next" rel="'+(parseInt(page)+1)+'"></li>';
				}
				$("#ARTICLES #articles_page ul").empty().append(articles_data);

				$("#articles_page li").click(function(){
					$("#articles_page li").removeClass("active");
					$(this).addClass("active");
					init_koobitor_articles_page($(this).attr('rel'),perpage);
				});
			});
		$("#ARTICLES .item_list ul").empty().append(data).highLight();
		if((perpage==8 && page>=1) || (perpage==16 && page>=1))$("#ARTICLES .load").hide();
		if(perpage>16 && page==1){
			var temp = "#ARTICLES .load"+(parseInt(perpage)-8);
			$(temp).hide();
		}
		if(perpage>=16 && page>=1){
			var temp = "#ARTICLES .load"+(parseInt(perpage));
			$(temp).hide();
		}
	});
}
function init_tabs(){
	var artickes_page;
	var fitness_page;
	var tips_page;
	var photo_page;
	$("#ARTICLES .load").show();
	$.get("/2012/templates/ajax_sup2011_v0/render_wp_post_thumb.ajax.php", { category_name: "articles", 
	posts_per_page: "8", offset: "0",paged:"1"},
	function(data){
		$.get("/2012/templates/ajax_sup2011_v0/total_post_thumb.ajax.php", { category_name: "articles",
		posts_per_page: "8" },
			function(data){
				var articles_page = parseInt(data);
				var articles_data = '';
				for (i=1;i<=articles_page;i++)
				{
					if(i==1){articles_data += '<li class="active" rel="'+i+'">'+i+'</li>';}
					if(i!=1){articles_data += '<li rel="'+i+'">'+i+'</li>';}
				}
				if(articles_page>1){articles_data += '<li id="next" rel="2"></li>';}
				$("#ARTICLES #articles_page ul").empty().append(articles_data);

				$("#articles_page li").click(function(){
					$("#articles_page li").removeClass("active");
					$(this).addClass("active");
					init_koobitor_articles_page($(this).attr('rel'),8);
				});
			});
		$("#ARTICLES .item_list ul").empty().append(data).highLight();
		$("#ARTICLES .load").hide();
	});
	$(".articles-show-more li:first").click(function(){
		init_koobitor_articles_page(1,$(this).attr('rel'));
		$(this).attr("rel",parseInt($(this).attr('rel'))+8);
	});
	$(".photo-show-more li:first").click(function(){
		init_koobitor_photo_page(1,$(this).attr('rel'));
		$(this).attr("rel",parseInt($(this).attr('rel'))+8);
	});
	$(".fitness-show-more li:first").click(function(){
		init_koobitor_fitness_page(1,$(this).attr('rel'));
		$(this).attr("rel",parseInt($(this).attr('rel'))+8);
	});
	$(".tips-show-more li:first").click(function(){
		init_koobitor_tips_page(1,$(this).attr('rel'));
		$(this).attr("rel",parseInt($(this).attr('rel'))+8);
	});
	init_slideshow();
}

function set_over(){
	$('.tab_panel').each(function(index,item) {
		$(item).find('.item_list > ul').highLight();
	});
}
function init_slideshow(){if ($('.slideshow').length==0) return; if ($('.slideshow').children().length<=1) return;
	var nav_width=0;
    $('.slideshow').cycle({
		//fx: 'blindX',
		//speed: 500,
		timeout: 5000,
		delay: 0,
		fx: 'fade',
		speed: 1000,
		pager: '#slideshow_nav',
		pagerAnchorBuilder: mainbanner_v0_render_anchor,
		allowPagerClickBubble: true
	});
	//$('.slideshow').cycle('pause').cycle(0); return;
	//Calculate width and position of navigation bar
	$('#slideshow_nav > div').each(function(){nav_width+=20;}); nav_width+=8;
	//alert($('#mainbanner_wrap').width());
	$('#slideshow_nav').css({
		width:nav_width,
		left:($('#mainbanner_wrap').innerWidth()-nav_width)/2
	});
	//alert(nav_width);
	$('#slideshow_nav').hover(
		function(){
			$('#slideshow_nav').stop().animate({opacity:1},'slow');
		},
		function(){
			$('#slideshow_nav').stop().animate({opacity:0},'slow');
		});
	$('#mainbanner_wrap').hover(
		function(){
			$('#slideshow_nav').stop().animate({opacity:1},'slow');
		},
		function(){
			$('#slideshow_nav').stop().animate({opacity:0},'slow');
		});
	$('#mainbanner_wrap').hover(
		function(){
			$('.slideshow').cycle('pause');
		},
		function(){
			$('.slideshow').cycle('resume');
		});
}

function mainbanner_v0_render_anchor(index, e1){
	return '<div></div>';
}


function home_highlight() {
	$('frontpage_tab li').mouseover(function() {
	//	alert('found');
	});
}

