﻿
	var xmlHttp
	var commentId
	var frame
	var spe
	var glo;
	
	function converForPolishWords(t){
		t = t.replace(/ą/gi,'0-0');
		t = t.replace(/ć/gi,'1-1');
		t = t.replace(/ę/gi,'2-2');		
		t = t.replace(/ł/gi,'3-3');		
		t = t.replace(/ń/gi,'4-4');		
		t = t.replace(/ó/gi,'5-5');		
		t = t.replace(/ś/gi,'6-6');		
		t = t.replace(/ź/gi,'7-7');		
		t = t.replace(/ż/gi,'8-8');		
		return t;
	}
	
	// set main photo
	function setMainPhoto(id){ 
		
		xmlHttp = GetXmlHttpObject()	
		var url="scripts/setMainPhoto.php?id="+id;
		xmlHttp.onreadystatechange = stateChangedComment
		xmlHttp.open("GET",url,true)		
		xmlHttp.send(null)			
	
	}

	function ShowInfo(id,what){ 
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null){
			alert ("Browser does not support HTTP Request")
			return
		} 

		var url="scripts/favourites.php";
		url=url+"?id="+id+"&what="+what;
		xmlHttp.onreadystatechange=stateChanged
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	
	}

	function deleteFormFavourites(toFrame,page,id){ 			
		spe = "&delete="+id
		loadPageToFrame(toFrame,page);
	}
	function deleteFormBlackList(toFrame,page,id){ 			
		spe = "&delete="+id
		loadPageToFrame(toFrame,page);
	}	
	
	function addSpecialUrl(toFrame,page,spec){ 			
		spe = spec;
		loadPageToFrame(toFrame,page);
	}	
	function loadPageWith(toFrame,page,spec){
		spe = spec;
		loadPageToFrame(toFrame,page);
	}	
	function addingComment(id){
		commentId = id
		addComment(id)
	}
	function addComment(toFrame,page,id,login){ 	
		glo = 0;
		var t = document.getElementById('comment_text').value;
		loadPageWith(toFrame,page,'&submit=1&login='+login+'&text='+t+'&user_id='+id);
		loadPageWith('comments','show_comments','&login='+login);		
		updateComments();
	}
	
	
	// ladowanie strony do diva //
	
	function loadPageToFrame(toFrame,page){		

		/*xmlHttp = GetXmlHttpObject();
		
		if(spe){
			var url = "loadPageToFrame.php?mod="+page+spe;
		}else{
			var url = "loadPageToFrame.php?mod="+page;
		}

		xmlHttp.onreadystatechange = function() {
		  if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
			if(page == 'show_comments'){
				if(glo ==  1){
				  	document.getElementById(toFrame).innerHTML = document.getElementById(toFrame).innerHTML + xmlHttp.responseText 
				}else{
					document.getElementById(toFrame).innerHTML = xmlHttp.responseText 
					glo = 1;
				}
			}else{
				document.getElementById(toFrame).innerHTML = xmlHttp.responseText 
			}
			
		  }		 
		}


		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)		*/
		// wyswietlamy diva pokazujacego ze strona jest w trakcie ladowania			
		
		// otwieramy glowny plik odpowiedzialny za otworzenie plikow MVC
		if(spe){
			var url = "loadPageToFrame.php?mod="+page+spe;
		}else{
			var url = "loadPageToFrame.php?mod="+page;
		}
			
		// tworzymy obiekt Request
		var req = mint.Request();
   		
   		// funkcja jesli zakonczy sie powodzeniem wyslanie zadania
		req.OnSuccess = function() {
			if(page == 'show_comments'){
				if(glo ==  1){
				  	$(toFrame).innerHTML = $(toFrame).innerHTML + this.responseText;
				}else{
					$(toFrame).innerHTML = this.responseText; 
					glo = 1;
				}				
			}else{
				$(toFrame).innerHTML = this.responseText; 
			}
		}

		// przesylamy formularz
		req.Send(url);			
		
	}
	function updateComments(){
		// tworzymy obiekt Request
		var req2 = mint.Request();

		var url = "loadPageToFrame.php?mod=updateComments"+spe;
		
   		// funkcja jesli zakonczy sie powodzeniem wyslanie zadania
		req2.OnSuccess = function() {
			$('count_comm').innerHTML = this.responseText; 
		}

		// przesylamy formularz
		req2.Send(url);			
	}
	
	function state2(){ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			document.getElementById(frame).innerHTML=xmlHttp.responseText
		} 
	}	
	
	// koniec ladowania strony do diva //
	
	
	// wysylanie polecenia strony znajomemu //
	
	function sendToFriend(){ 
		xmlHttp=GetXmlHttpObject()
		var te = converForPolishWords(document.getElementById('text').value);
		var email = document.getElementById('email').value;
		var url="scripts/sendToFriend.php";
		xmlHttp.onreadystatechange=stateChangedComment
		url=url+"?email="+email+"&text="+te;
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)			
	}	
	
	// -- //


	// wysylanie bledu na stroni //
	
	function sendMessage(){ 

		xmlHttp=GetXmlHttpObject()			
		var te = document.getElementById("message").value;
		var url="loadPageToFrame.php?mod=send_message&submit=1&text="+te;
		xmlHttp.onreadystatechange = stateChangedComment
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)			
		
	}	
	function sendViolation(){ 

		xmlHttp=GetXmlHttpObject()			
		var te = document.getElementById("message").value;
		var url="loadPageToFrame.php?mod=send_violation&submit=1&text="+te;
		xmlHttp.onreadystatechange = stateChangedComment
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)			
		
	}		
	function addFriend(){ 

		xmlHttp=GetXmlHttpObject()			
		var te = document.getElementById("message").value;
		var url="loadPageToFrame.php?mod=add_friend&submit=1&text="+te;
		xmlHttp.onreadystatechange = stateChangedComment
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)			
		
	}	
	// -- //
	
	
	function showComments(id){ 
		if (xmlHttp.readyState == 4){
			xmlHttp=GetXmlHttpObject()
			var url="scripts/showComments.php";
			xmlHttp.onreadystatechange=stateChangedShowComments
			url=url+"?id="+id;
			xmlHttp.open("GET",url,true)
			xmlHttp.send(null)			
		}
	}	
	function loadFile(name,id){ 
		
		// tworzymy obiekt Request
		var req_lf = mint.Request();

		var url="templates/"+name;
		
   		// funkcja jesli zakonczy sie powodzeniem wyslanie zadania
		req_lf.OnSuccess = function() {
			$('add_comment').innerHTML = this.responseText; 
		}

		// przesylamy formularz
		req_lf.Send(url);					

	}		
	
	function showComments2(id){ 


			xmlHttp=GetXmlHttpObject()
			
			var url="scripts/showComments.php";
			xmlHttp.onreadystatechange=stateChangedShowComments
			url=url+"?id="+id;
			xmlHttp.open("GET",url,true)
			xmlHttp.send(null)			

	}			
	
	function stateChanged(){ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			document.getElementById("show_info").innerHTML=xmlHttp.responseText 
		} 
	}
	
	function stateChangedFavourite(){ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			document.getElementById("page").innerHTML=xmlHttp.responseText 
		} 
	}	
	
	function stateChangedComment(){ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			document.getElementById("add_vote").innerHTML=xmlHttp.responseText 
			showComments(commentId)
		} 
	}	
	function stateChangedShowComments(){ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			document.getElementById("show_comments").innerHTML=xmlHttp.responseText 
		} 
	}		
	
	// -- ocenianie piosenek //
	
	function addVote(vote,song_id){
		xmlHttp=GetXmlHttpObject()			
		var url="scripts/addVote.php";
		xmlHttp.onreadystatechange=stateChangedComment
		url=url+"?vote="+vote;
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)		
	}
	
	function updateVotes(user_id){

		// tworzymy obiekt Request
		var req_uv = mint.Request();

		var url="scripts/updateVotes.php?user_id="+user_id;
		
   		// funkcja jesli zakonczy sie powodzeniem wyslanie zadania
		req_uv.OnSuccess = function() {
			$('rat').innerHTML = this.responseText; 
		}

		// przesylamy formularz
		req_uv.Send(url);			
	}
	

	//-- //
	
	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 ShowPendingImage(){
		document.getElementById("show_info").innerHTML="<image src=images/bigrotation2.gif>Trwa aktualizacja danych...</image>";
	}

	function HidePendingImage(){
		document.getElementById("show_info").innerHTML="";
	}
