/**
* Armenpress.am 
* @author 	Vardan Torosyan
* version 	1.0
* main.js 	
*/	

$(document).ready(function(){

	$(".closex").click( function () {
			var oldblocks=($.cookie("blockconfig")==null)?'':$.cookie("blockconfig")+',';
			$.cookie("blockconfig", null);
			$.cookie("blockconfig", oldblocks+$(this).parent().parent().attr('id').replace('bh_', '').replace('otherhline', ''), { expires: 7, path: '/'  });
			$(this).parent().parent().parent().fadeOut("slow");
	 });
	
	
/*	$('.otherheadlines').sortable({
			connectWith: ".otherheadlines"
		}).disableSelection();
*/	
	
	
	$("#mmediabtns > p").click( function () {
		$("#mmediabtns > p").each(function(index) {
				$(this).removeClass('selected');
			  });
		$(this).addClass('selected');
		loading('show', 'mmediabody');
		$.post(rootPath+'/'+curLang+'/ajax/', {xdo:"home"+$(this).attr('alt')},
			  function(data){
				  $('#mmediabody').html(data);
				  loading('hide', 'mmediabody');	  
			  });
	});
	
	$("#dateprevicon, #datenexticon").hover(
			  function () {		$(this).stop().animate({ 	opacity: 1		}, 300 );	 },
			  function () {		$(this).stop().animate({ 	opacity: 0.5	}, 300 );	 }
			);

	$(".playico").hover(
			  function () {		$(this).stop().animate({ 	opacity: 1		}, 200 );	 },
			  function () {		$(this).stop().animate({ 	opacity: 0.7	}, 200 );	 }
			);
				
	$(".newsbycatitem").hover(
			  function () {	$(this).css("background-color", "#F8F8F8"); 	 },
			  function () {	$(this).css("background-color", ""); }
	);
	
	$(".blockheadercontrols span").hover(
			  function () {	$(this).css("color", "#999"); 	 },
			  function () {	$(this).css("color", "#E0E0E0"); }
	);
	 
	$(".blockheadercontrols img").hover(
			  function () {		$(this).stop().animate({ 	opacity: 1		}, 300 );	 },
			  function () {		$(this).stop().animate({ 	opacity: 0.3	}, 300 );	 }
	);
	
	if(curModul!='home')	{
		$("#homeico").hover(
				  function () {	$(this).attr('src', rootPath+'/includes/css/images/home_ico_over.png'); },
				  function () {	$(this).attr('src', rootPath+'/includes/css/images/home_ico.png'); 		}
				);
	}
	
	var hULmaxheight=0;
	$('.headlinebody').each(function(index) {
		if($(this).height()>hULmaxheight) hULmaxheight=$(this).height();
	 });
	 
	 $('.headlinebody').each(function(index) {
		 $(this).height(hULmaxheight);
	 });
	
	/* correcting other headline heights */
	$(".headlineright").each(function(index) {		
		$(this).parent().height($(this).innerHeight()+16);								  
	})
	
	/* correcting other headline heights */
	$(".headlinetopnews").each(function(index) {		
		$(this).parent().height((($(this).innerHeight()+16)>$(this).parent().innerHeight())?($(this).innerHeight()+16):$(this).parent().innerHeight());								  
	})
	
	/* + correcting right panel height */
	if($("#maincontainer").innerHeight()>$("#sidebarcontainer").innerHeight()) 
		$("#sidebarcontainer").height($("#maincontainer").innerHeight()+40);
	
	
	/* clock */
	$('#clock').jclock({
        utc: true,
        utcOffset: +4
      });
	  
	/* face box */
	$('a[rel*=facebox]').facebox({
        loadingImage : '/includes/css/images/loading.gif',
        closeImage   : '/includes/css/images/closelabel.png'
      });
  
	/* more news */
	$('#morenews').click( function () {
	
			$('#morenewsload').css('visibility', 'visible');
		
			$.post(rootPath+'/'+curLang+'/ajax/', {xdo:"news", lastid:$('#lastnewsid').val(), catid:$('#categoryid').val()},
			  function(data){
				//alert(data);
				var newdata=data.split('^--^');
				if(newdata[0]>0) {
					$(newdata[1]).appendTo($('#newsbycatcontainer'));
					$('#lastnewsid').val(($('#lastnewsid').val()*1)+1);
					$('#morenewsload').css('visibility', 'hidden');
					
					/* + correcting right panel height */
					if($("#maincontainer").innerHeight()>$("#sidebarcontainer").innerHeight()) 
						$("#sidebarcontainer").height($("#maincontainer").innerHeight()+40);
						$(".newsbycatitem").hover(
						  function () {	$(this).css("background-color", "#F8F8F8"); 	 },
						  function () {	$(this).css("background-color", ""); }
						);
				  }
					else {
						$('#morenews').css('cursor', 'default');
						$('#morenews').html(' &nbsp; '+noMoreNews);
					}
		 	 });	

	});
	
	
	/* more photos, videos */
	$('#moremedia').click( function () {
			
			$('#morenewsload').css('visibility', 'visible');
		
			$.post(rootPath+'/'+curLang+'/ajax/', {xdo:$('#mediatype').val(), page:$('#mediapage').val()},
			  function(data){
				 // alert(data);
				if(data!='') {
					$(data).appendTo($('#itemsbody'));
					$('#mediapage').val(($('#mediapage').val()*1)+1);
					$('#morenewsload').css('visibility', 'hidden');
					
					/* + correcting right panel height */
					if($("#maincontainer").innerHeight()>$("#sidebarcontainer").innerHeight()) 
						$("#sidebarcontainer").height($("#maincontainer").innerHeight()+40);

				  }
					else {
						$('#moremedia').css('cursor', 'default');
						$('#moremedia').html(' &nbsp; '+noMoreNews);
					}
		 	 });	

	});
	
	$('.morenewsbydate').click( function () {
			window.scrollTo(0,220);
	});
	
	
	get_currency();
	get_weather();	
});


function search_photos(skey) {
	//alert(skey);
	
	loading('show', 'mediasearchfield');

	$.post(rootPath+'/'+curLang+'/ajax/', {xdo:'searchmedia', skey:skey},
	  function(data){
		//  alert(data);
		if(data!='') {
			$('#itemsbody').html(data);
			
			// + correcting right panel height 
			if($("#maincontainer").innerHeight()>$("#sidebarcontainer").innerHeight()) 
				$("#sidebarcontainer").height($("#maincontainer").innerHeight()+40);

		  }
		   else {
				alert('No data');
			}
		  
		  loading('hide', 'mediasearchfield');
		  $('#morenewsbydatecontainer').css('visibility', 'hidden');
	
	 });	
}



function get_currency()	{
	$.post(rootPath+'/'+curLang+'/ajax/', {xdo:"currency"},
	  function(data){
		$('#currencycontainer').html(data);
	  });		
}

function get_weather()	{
	$.post(rootPath+'/'+curLang+'/ajax/', {xdo:"weather"},
	  function(data){
		$('#weathercontainer').html(data);
	  });		
}

function showSmsForm() {
	$('#subscribersmscontent').show();
}

/* ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */
/* //////////////////////////////////////////////		Voting Poll			///////////////////////////////////////////////////// */
/* ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// */

// perform poll voting
function doVote()	{
	
	checkedId=$('form[name=voteform] input:radio:checked').val();
	
	if(checkedId) {
		loading('show', 'voting');
		$.post(rootPath+'/'+curLang+'/ajax/', {xdo:"voting", xaction:'dovoting', ids:checkedId},
		  function(data){
			$('#voting').html(data);
			loading('hide', 'voting');
		  });		
	}
	
}
// loads an other poll
function otherPolls(curId)	{
	loading('show', 'voting');
	$.post(rootPath+'/'+curLang+'/ajax/', {xdo:"voting", xaction:'otherpoll', curpollid:curId},
	  function(data){
		  $('#voting').html(data);
			loading('hide', 'voting');
	  });		
}

// show poll result
function pollResults(pId)	{
	loading('show', 'voting');
	$.post(rootPath+'/'+curLang+'/ajax/', {xdo:"voting", xaction:'pollresult', pid:pId},
	  function(data){
		  $('#voting').html(data);
			loading('hide', 'voting');
	  });		
}

/* end voting poll */

/* page settings*/
function savepagesettings()	{

	var cookiestr='';
	$('#sitesettings input:checkbox').each(function(index) {
			 if($(this).attr('checked')===false)
				cookiestr+=$(this).attr('name')+',';
		});
		
	$.cookie("blockconfig", null);
	$.cookie("blockconfig", cookiestr, { expires: 7, path: '/'  });

	jQuery(document).trigger('close.facebox');
	window.location.reload();
}


/* START login */
function doLogin()	{
	$("#password").val(md5($("#password").val()));
	$("#loginform").submit();
}

/* loading */
function loading(act, container)	{
	if(act=='show') {
		$('#'+container).prepend('<div class="loadingDiv"></div>');
		var loadDiv=$('#'+container+' > .loadingDiv');
		var h=$('#'+container).innerHeight();
		var w=$('#'+container).innerWidth();
		loadDiv.css({"visibility":"visible", 'height':h, 'width':w});	
	}
	if(act=='hide')
		$('#'+container+' > .loadingDiv').remove();
}


/*
*	print page
*/
function printPage()	{
    if (typeof(window.print) != 'undefined') 
        window.print();
	else
		alert("Sorry, can't print!");
}
