var url, lang, domain, rating, inter, prev_cont, temp;
var counter=1;


$(document).ready(initAll);
		
		
function initAll() 
{
	
	 
	$("#add_comment_form").submit(add_comment);
	//$("#ha").click(haha);
	$("#sf_add_site_code").hide();
	$("#sf_show_code").click(function(){ 
	
	
		$("#sf_show_code").fadeOut("slow");
		setTimeout('', 600);
		$("#sf_add_site_code").fadeIn(1000);
	
	});
	
	 /* document.getElementById('sf_add_site_code').style.display='block'; document.getElementById('sf_show_code').style.display='none';*/ 

	
	
	url = document.URL;
	
     lang = url.split('/')[3];
     domain = url.split('/')[2];
     file_id = url.split('/')[6];
     
		// Since the <option value="3"> was selected by default, we must remove this selection from Stars.
		//$("#rat").stars("selectID", -1);
			

			$("#rat").children().not("select, #rating_title").hide();
			var $caption = $('<div id="caption"/>');

			$("#rat").stars({
				//disabled: true,
				inputType: "select",
				oneVoteOnly: true,
				captionEl: $caption,
				callback: function(ui, type, value)
				{
				inter = ui;	
				if(value == 1)
					value--;
				
				rating = value;		
				   if(lang == 'pl')
					   $message = 'Zapisywanie...';
				   else
					   $message = 'Saving...';
				   
					$("#messages").text($message).fadeIn(30);
					

					var script = 'http://'+domain+'/pl/download/rate/?debug=false';	

					xmlHttp=GetXmlHttpObject();
			        if (xmlHttp==null)
			         {
			        	alert ("Browser does not support HTTP Request");
			        	return false;
			         }
			        xmlHttp.onreadystatechange=stateChanged;
			       
			       xmlHttp.open("POST", script);
			       xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

			       xmlHttp.send("mod=1&rel_id=" + file_id + "&rate=" + value);	
				}
			});
 

}


		
		function stateChanged() 
	      { 
	    
	      		
	      		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	           	{ 
	      			var cont = xmlHttp.responseText;
	      			
	      			
	      			
	      		// Change widget's title
	      			if(lang == 'pl'){
						   title = 'Srednia ocena:';
	      			       messages = "Ocena zapisana. Dzieki!";
	      			       }
					   else{
						   title = 'Average rating:';
	      			       messages = "Rating saved. Thanks!";
					   }
	      			$("#rating_title").hide();
					$("#rating_title").text(title);
					$("#rating_title").fadeIn("slow");
					// Select stars from "Average rating" control to match the returned average rating value
					inter.select(10 * Math.round((cont/10)));
					
					
					// Update widget's caption
					//$("#caption").text("Srednia ocena: "+cont);
				
					$("#messages").fadeOut(40);
					
					// Display confirmation message to the user
					$("#messages").text(messages).stop().css("opacity", 1).fadeIn(30);
					
					// Hide confirmation message after 2 sec...
					setTimeout(function(){
						$("#messages").fadeOut(1000)
					}, 2000);
	          	
	           	} 
	      }

		
		
	  	function GetXmlHttpObject()
	      {
	      		var xmlHttp=null;
	      		try
	       	{
	       	// Firefox, Opera 8.0+, Safari
	       	xmlHttp=new XMLHttpRequest();
	       	}
	      		catch (e)
	       	{
	       		//Internet Explorer
	       		try
	        		{
	        			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	        		}
	       		catch (e)
	        		{
	        			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	        		}
	       	}
	      		return xmlHttp;
	      }  
	      
	  	
	  	
	  	
	  	function add_comment()
	  	{
	  		
	  		url = document.URL;
	  		 var form = document.forms['add_comment_form'];
	  	   // var form = document.add_comment_form;
	  		
	    	 var cont = form.comment.value;
	    	 temp = cont;
	    	 var id = url.split('/')[6]; 
	    	
	    	 $("#c_messages").hide();
	    	 
	    	 var script;
		    var error, error2;
		        if (lang == 'pl')
		        {
		        	script = "http://"+domain+"/pl/download/add_comment2/?debug=false";
		        	error = "Komentarz nie moze byc pusty!";
		        	error2 = "Juz dodales komentarz o identycznej treci!";
		        }
		        else
		        {
		        	script = "http://"+domain+"/en/download/add_comment2/?debug=false";
		        	error = "Comment cannot be null!";
		        	error2 = "You've already added identical comment!";
		        }  	 
	    	 
	    	 
	    	 
	    	 if(cont=='')
	    	 {
	    		 	$("#c_messages").hide();	
	      			$("#c_messages").html(error);
	      			$("#c_messages").show("slow"); 
	      			return false;
	    	 }
	    	 else if(cont == prev_cont)
	    	 {
	    		    $("#c_messages").hide();	
	      			$("#c_messages").html(error2);
	      			$("#c_messages").show("slow"); 
	      			return false;
	    	 }
	    	 
	    		 
	    	 
	  		
	  		

    	xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null)
         {
        	alert ("Browser does not support HTTP Request");
        	return false;
         }
       

        xmlHttp2.onreadystatechange=stateChanged2 ;
       
       xmlHttp2.open("POST", script);
       xmlHttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	   

        
      
    	   xmlHttp2.send("cont=" + cont + "&id=" + id); 
       
       
	  return false;
	  	
	  	}
	  	
	  	
	  	
	  	
	  	function stateChanged2() 
	      { 
	    
	      		
	      		if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
	           	{ 
	      			var cont = xmlHttp2.responseText;	
	      			var new_comment = $("#new_comment").html();
	      			

	      			new_comment += '<br /><div id="new_comment'+counter+'"></div>';
	      			$("#new_comment").html(new_comment);
	      			
	      			$("#new_comment"+counter).hide();	
	      			
	      			$("#new_comment"+counter).html(cont);
	      			$("#new_comment"+counter).show("slow");
	      			if(counter>2)
	      			$(".sf_add_comment").hide("slow");	
	      			
	      			prev_cont = temp;
	      			
	      			counter++;
	      			
		
	          	
	           	} 
	      }
	  	
	  	
	  	function isset2(varname){
	  		return(typeof(window[varname])!='undefined');
	  	}
		
		