$(function() {
	
	var full_path = "http://www.x-ul.ro/work/time4news/";
	var full_domain = "x-ul.ro";
	
	showNextItem();
	showNextIndexItem();
	$("input#count_iter").val(6);
	
	
	//Display arhiva
	$('#arhiva').hide();
	$('.menu_bar ul li .arhiva').click(function(){
		if($('#arhiva').hasClass('open') == false) {
			$('#regiuni').removeClass("open");
			$('.menu_bar ul li .regiuni').removeClass('selected');
			//$('#regiuni').slideUp("slow");
			
			$('#arhiva').addClass('open');
			$('.menu_bar ul li .arhiva').addClass('selected');
			$('#arhiva').slideDown("slow");
		}else{
			$('#arhiva').removeClass("open");
			$('.menu_bar ul li .arhiva').removeClass('selected');
			$('#arhiva').slideUp("slow");
		}
		return false;
    });
	
	/*
	//select region from cookies
	$("#" + $.cookie('regiune')).addClass('selected_region');
	//Display regions
	$('#regiuni').hide();
	$('.menu_bar ul li .regiuni').click(function(){
		if($('#regiuni').hasClass('open') == false) {
			$('#arhiva').removeClass("open");
			$('.menu_bar ul li .arhiva').removeClass('selected');
			$('#arhiva').slideUp("slow");
			
			$('#regiuni').addClass('open');
			$('.menu_bar ul li .regiuni').addClass('selected');
			$('#regiuni').slideDown("slow");
		}else{
			$('#regiuni').removeClass("open");
			$('.menu_bar ul li .regiuni').removeClass('selected');
			$('#regiuni').slideUp("slow");
		}
		return false;
    });
	$('#reset_regiune').click(function(){
		$('#regiuni').removeClass("open");
		$('.menu_bar ul li .regiuni').removeClass('selected');
		$('#regiuni').slideUp("slow");
		return false;
	});
	
	//Clicked region
	$('#regiuni_list li a').click(function(){
		$('#regiuni_list li a').removeClass("selected_region");
		$(this).addClass('selected_region');
		return false;
	});
	
	//Clicked Save region
	$('#salveaza_regiune').click(function(){
		clicked = $('.selected_region').attr("id");
		//$.cookie('regiune', clicked, { expires: 7, domain: 'x-ul.ro' });
		$.cookie('regiune', clicked, { path: '/', domain: full_domain });
		$('.menu_bar ul li .regiuni').removeClass('selected');
		$('#regiuni').removeClass("open");
		$('#regiuni').slideUp("slow");
		window.location.href = full_path;
		return false;
	});
	
	//Clicked Reset region
	$('#reseteaza_regiune').click(function(){
		$('#regiuni_list li a').removeClass("selected_region");
		$("#" + $.cookie('regiune')).addClass('selected_region');
		$('.menu_bar ul li .regiuni').removeClass('selected');
		$('#regiuni').removeClass("open");
		$('#regiuni').slideUp("slow");
		return false;
	});
	*/
	//================================
	
	//Prev next hot news
	var count = parseInt($("input#count_news_crt").val());
	$('.selectors .next').click(function(){
		if(count < $("input#count_news").val()) {
			$('#news_content').scrollTo('+=500px', 500, { axis:'x' });
			count = count+1;
			$("input#count_news_crt").val(count);
		}else{
			$('#news_content').scrollLeft(0);
			count = 1
			$("input#count_news_crt").val(1);
		}
		return false;
   });
	
	$('.selectors .prev').click(function(){
		$('#news_content').scrollTo('-=500px', 500, { axis:'x' });
		count--;
		return false;
   });
	//================================
	
	//Clicked next region news
	$('#right_arrow').click(function(){
		showNextNews();
		return false;
	});
	//Clicked next region news
	$('#left_arrow').click(function(){
		showPrevNews();
		return false;
	});
	//Clicked on region name
	$('#reg_last_navig li a').click(function(){
		var cl_array = $(this).parent().attr("id").split("_");
		$(".last_news .int ul li a").removeClass('selected_s');
		$(this).addClass('selected_s');
		var iter = parseInt(cl_array[1]);
		$("input#count_iter").val(iter);
		
		
		if(iter > 7 && iter <= 11) {
			for(var i=1;i<7;i++){
				var hide_1 = 3 - i;
				$('#s_'+i).hide();
			}
		}
		if(iter <= 7) {
			for(var i=1;i<=7;i++){
				var show_1 = iter - i;
				$('#s_'+show_1).show();
			}
		}
		var selected = $(this).text().toLowerCase();
		$("#news_content_last").html("<div style=\"height:200px; width:500px; padding-top:50px; padding-left:100px;\"><img src=\"images/loading.gif\" width=\"41\" height=\"39\" /></div>");
		$("#news_content_last").load("bin/latest_news.php #"+selected).ajaxSend(function(){
			$("#news_content_last").html("<div style=\"height:200px; width:500px; padding-top:50px; padding-left:100px;\"><img src=\"images/loading.gif\" width=\"41\" height=\"39\" /></div>");
		});
		
		return false;
	});
	
	
	
	//Clicked fonts
	$('#font_1').click(function(){
		$("#font_ch").css('font-size', '12px');
		return false;
	});
	$('#font_2').click(function(){
		$("#font_ch").css('font-size', '14px');
		return false;
	});
	$('#font_3').click(function(){
		$("#font_ch").css('font-size', '16px');
		return false;
	});
	
	
	
	//Top right
	$('.top_comentate').click(function(){
		$('#top_navig li a').removeClass('selected');
		$(this).addClass('selected');
		$('.titles').hide();
		$('#top_comentate').show();
		return false;
	});
	$('.top_citite').click(function(){
		$('#top_navig li a').removeClass('selected');
		$(this).addClass('selected');
		$('.titles').hide();
		$('#top_citite').show();
		return false;
	});
	$('.ultimele_stiri').click(function(){
		$('#top_navig li a').removeClass('selected');
		$(this).addClass('selected');
		$('.titles').hide();
		$('#ultimele_stiri').show();
		return false;
	});
	
	
	//Toate stirile zilei
	$('#ext').scrollTo('0px', 600, { axis:'x' });
	$('#urmatorul').click(function(){
		showNextIndex();
		var count = parseInt($("input#count_news_index").val());
		if(count < 29) {
			count = count+1;
			$("input#count_news_index").val(count);
		}
		return false;
   });
	
	$('#precedentul').click(function(){
		showPrevIndex();
		var count = parseInt($("input#count_news_index").val());
		if(count > 0) {
			count = count-1;
			$("input#count_news_index").val(count);
		}
		return false;
   });
	//================================
	
	
});
//================================

function showNextIndexItem() {
	var count = parseInt($("input#count_news_index").val());
	if(count < 29) {
		if(count > 0) {
		$('#ext').scrollTo('+=600px', 600, { axis:'x' });
		}
		count = count+1;
		$("input#count_news_index").val(count);
	}else{
		$('#ext').scrollLeft(0);
		count = 1
		$("input#count_news_index").val(1);
	}
	setTimeout("showNextIndexItem()", 9000);
}

function showNextIndex() {
	$('#ext').scrollTo('+=600px', 600, { axis:'x' });
}
function showPrevIndex() {
	$('#ext').scrollTo('-=600px', 600, { axis:'x' });
}



function showNextItem() {
	var count = parseInt($("input#count_news_crt").val());
	if(count < $("input#count_news").val()) {
		$('#news_content').scrollTo('+=500px', 500, { axis:'x' });
		count = count+1;
		$("input#count_news_crt").val(count);
	}else{
		$('#news_content').scrollLeft(0);
		count = 1
		$("input#count_news_crt").val(1);
	}
	setTimeout("showNextItem()", 3500);
}

function showNextNews() {
	var iter = parseInt($("input#count_iter").val());
	var max_no = 14;
	
	if(iter < 14){
		iter = iter + 1;
		$(".last_news .int ul li a").removeClass('selected_s');
		$("#s_"+iter+" a").addClass('selected_s');
		var selected = $("#s_"+iter+" a").text().toLowerCase();
	
		if(iter > 6 && iter <= 12) {
			var hide_1 = iter - 6;
			$('#s_'+hide_1).hide();
		}
		$("input#count_iter").val(iter);
		
		$("#news_content_last").html("<div style=\"height:200px; width:500px; padding-top:50px; padding-left:100px;\"><img src=\"images/loading.gif\" width=\"41\" height=\"39\" /></div>");
		$("#news_content_last").load("bin/latest_news.php #"+selected).ajaxSend(function(){
			$("#news_content_last").html("<div style=\"height:200px; width:500px; padding-top:50px; padding-left:100px;\"><img src=\"images/loading.gif\" width=\"41\" height=\"39\" /></div>");
		});

	}
}

function showPrevNews() {
	var iter = parseInt($("input#count_iter").val());
	var max_no = 14;
	
	if(iter > 6) {
		var show = iter - 6;
		$('#s_'+show).show();
	}
	
	if(iter > 1){
		iter = iter - 1;
		$(".last_news .int ul li a").removeClass('selected_s');
		$("#s_"+iter+" a").addClass('selected_s');	
		var selected = $("#s_"+iter+" a").text().toLowerCase();
		
		$("input#count_iter").val(iter);
		
		$("#news_content_last").html("<div style=\"height:200px; width:500px; padding-top:50px; padding-left:100px;\"><img src=\"images/loading.gif\" width=\"41\" height=\"39\" /></div>");
		$("#news_content_last").load("bin/latest_news.php #"+selected).ajaxSend(function(){
			$("#news_content_last").html("<div style=\"height:200px; width:500px; padding-top:50px; padding-left:100px;\"><img src=\"images/loading.gif\" width=\"41\" height=\"39\" /></div>");
		});
	}
}

function printArticle(mainUrl, identifier) {
	window.open(mainUrl+"print.php?identifier="+identifier,"Printeaza","height=600,width=800,resizable=yes,scrollbars=yes");
	return false;
}

function viewImg(mainUrl, identifier) {
	window.open(mainUrl+"img.php?identifier="+identifier,"Galerie","height=700,width=800,resizable=yes,scrollbars=yes");
	return false;
}

function sendArticleLinkByYM(articleUrl) {
	 window.location.href="ymsgr:im?+&msg="+articleUrl;
}

function blog() {
	window.open("http://www.time4news.ro/blog.php","Bloguri",'height=270,width=250,resizable=no,scrollbars=no,left='+(screen.availWidth/2-50)+',top='+(screen.availHeight/2-50)+'');
	return false;
}
