// mojOsijek.hr JavaScript Document

function trazifokus() {if(document.getElementById('q').value=='Unesite traženi pojam...'){document.getElementById('q').value=''};}
function traziblur() {if(document.getElementById('q').value==''){document.getElementById('q').value='Unesite traženi pojam...'};}

function flogin1() {if(document.getElementById('user').value=='Nadimak'){document.getElementById('user').value=''};}
function flogin2() {if(document.getElementById('user').value==''){document.getElementById('user').value='Nadimak'};}

function fpass1() {if(document.getElementById('passwrd').value=='Lozinka'){document.getElementById('passwrd').value=''};}
function fpass2() {if(document.getElementById('passwrd').value==''){document.getElementById('passwrd').value='Lozinka'};}

function prozor(url) {
	newwindow=window.open(url,'name','height=300,width=500,top=200,left=200');
	if (window.focus) {newwindow.focus()}
	return false;
}

function facebookprozor(url) {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=336,top=200,left=200');return false;}

function navigacija(url,sirina,duzina) {
	newwindow=window.open(url,'name','height='+duzina+',width='+sirina+',top=200,left=200');
	if (window.focus) {newwindow.focus()}
}


function stavihomepage(Linkb){
	Linkb.style.behavior='url(#default#homepage)';
	Linkb.setHomePage('http://www.mojOsijek.hr/');
	return false;
}
	
function zadnjekomentirano() {
	document.getElementById ("zadnjikomentari").style.display="block";
	document.getElementById ("najvisepogleda").style.display="none";
	document.getElementById ("zadnjedodano").style.display="none";
}
function najvisepogleda() {
	document.getElementById ("zadnjikomentari").style.display="none"; 
	document.getElementById ("najvisepogleda").style.display="block"; 
	document.getElementById ("zadnjedodano").style.display="none";
}
function zadnjedodano() {
	document.getElementById('zadnjikomentari').style.display = "none";
	document.getElementById('najvisepogleda').style.display = "none";
	document.getElementById('zadnjedodano').style.display = "block";
}


// Zvijezdice
function ajaxUpdateRating( elemid, url, options )
{
  var params = options.params || "";
  var meth = options.meth || "post";
  var async = options.async || true;
  var startfunc = options.startfunc || "";
  var endfunc = options.endfunc || "";
  var errorfunc = options.errorfunc || "";
  var req = false;
  if( window.XMLHttpRequest )
	req = new XMLHttpRequest();
  else if( window.ActiveXObject )
	req = new ActiveXObject( "Microsoft.XMLHTTP" );
  else
  {
	alert( "Your browser cannot perform the requested action. "+
		 "Either your security settings are too high or your "+
		 "browser is outdated. Try the newest version of "+
		 "Internet Explorer or Mozilla Firefox." );
	return false;
  }
  if( startfunc != "" )
	eval( startfunc );
  req.open( meth, url+( params != "" ? "?"+params : "" ), async );
  req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
  req.onreadystatechange =
	function()
	{
	  if ( req.readyState == 4 )
		{
			if ( req.status == 200 )
			{
		  document.getElementById(elemid).innerHTML = req.responseText;
		  if( endfunc != "" )
			eval( endfunc );
		  return true;
			}
			else
			{
		  if( endfunc != "" )
			eval( endfunc );
		  if( errorfunc != "" )
			eval( errorfunc );
		  return false;
			}
		}
	};
	req.send(null);
}

function rate_it(news_id, rating, path, stars, stars_size, ip){
	
	var days = 365;
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = "rating"+news_id+"="+news_id+expires+"; path=/";
	
	ajaxUpdateRating( "ratingform"+news_id, path+"rating.php", {params:"id=" + news_id + "&rating=" + rating + "&stars=" + stars + "&stars_size=" + stars_size + "&ip=" + ip, endfunc:" update_middle_total('"+news_id+"', '"+path+"')"});
	

}

function update_middle_total(news_id, path){
	if(document.getElementById('rating_middle'+news_id)){
		ajaxUpdateRating("rating_middle"+news_id, path+"ratingmiddle.php", {params:"id="+news_id+"&what=middle"});
	}
	if(document.getElementById('rating_total'+news_id)){
		ajaxUpdateRating("rating_total"+news_id, path+"ratingmiddle.php", {params:"id="+news_id+"&what=total"});
	}
}







/*anketa*/
$(function(){
	var loader=$('#loader');
	var pollcontainer=$('#pollcontainer');
	loader.fadeIn();
	//Load the poll form
	$.get('http://www.mojOsijek.hr/ank/anketa.php', '', function(data, status){
		pollcontainer.html(data);
		animateResults(pollcontainer);
		pollcontainer.find('#viewresult').click(function(){
			//if user wants to see result
			loader.fadeIn();
			$.get('http://www.mojOsijek.hr/ank/anketa.php', 'result=1', function(data,status){
				pollcontainer.fadeOut(500, function(){
					$(this).html(data);
					animateResults(this);
				});
				loader.fadeOut();
			});
			//prevent default behavior
			return false;
		}).end()
		.find('#pollform').submit(function(){
			var selected_val=$(this).find('input[name=poll]:checked').val();
			if(selected_val!=''){
				//post data only if a value is selected
				loader.fadeIn();
				$.post('http://www.mojOsijek.hr/ank/anketa.php', $(this).serialize(), function(data, status){
					$('#formcontainer').fadeOut(100, function(){
						$(this).html(data);
						animateResults(this);
						loader.fadeOut();
					});
				});
			}
			//prevent form default behavior
			return false;
		});
		loader.fadeOut();
	});
	
	function animateResults(data){
		$(data).find('.bar').hide().end().fadeIn('slow', function(){
							$(this).find('.bar').each(function(){
								var bar_width=$(this).css('width');
								$(this).css('width', '0').animate({ width: bar_width }, 500);
							});
						});
	}
	
});