var ok_sign = 1;
var domain = "http://www.getvideo.bg/";
var temp_title_set = "";
////////////////////////////////////////////////////////////////

function displayMoreInfo(id){
	var elem = document.getElementById(id);
	if (elem.style.display == false || elem.style.display == 'none'){
		elem.style.display = 'block';
		$("#expand_marker").html('&ndash;');
		$("#info_wrapper").toggleClass("info_wrapper1");
	}else{
		$("#expand_marker").html('+');
		$("#info_wrapper").toggleClass("info_wrapper1");
		elem.style.display = 'none';	
	}
}

function tintTheImages(id, destination){
	var url = domain+"include/tinter.php";
	$.post(url, {selected_id: ""+id+""}, 
		function (data){
			$("#"+destination+"").html(data);
		});
}

function updateProcess(url, destination, period, format, container, src, dest, lang, video){
	$.post(url, {src: ""+src+"", dest: ""+dest+""},
		   function (data){
				//returns the percentage
				if (ok_sign == 1){
					if (data != 100){
						setProgress(destination, data);
						
						setTitle(search_title_update_text, parseInt(data));
						
						window.setTimeout("updateProcess('"+url+"','"+destination+"', '"+period+"', '"+format+"', '"+container+"', '"+src+"', '"+dest+"', '"+lang+"', '"+video+"')", period);
				   }else{
					   	//start converting
						setProgress(destination, data);
						startConverting(Math.random(), container, dest, format, lang, video);   
				   }
				}else{
					setResult("result_message", 'Error while downloading');
				}					  
		   }
	);

}

//update the process according to the specified duration
function updateConvertingProcess(url, destination, period, src, duration){
	$.post(url, {src: ""+src+""},
		   function (data){
			   	var temp_per = parseInt((parseInt(data)/duration)*100);
				//returns the percentage
				if (ok_sign == 1){
					if (temp_per != 100){
						setProgress(destination, temp_per);
						
						setTitle(convert_title_update_text, temp_per);
						
						window.setTimeout("updateConvertingProcess('"+url+"','"+destination+"', '"+period+"', '"+src+"', '"+duration+"')", period);
				   }else{
						setProgress(destination, temp_per);
						
						refreshTitle(temp_title_set);
						
						//delete the text file
						var delete_url = domain+"include/delete_garbage.php";
						$.post(delete_url, {src: ""+src+""});
						
				   }
				}else if (ok_sign == 2){
					setProgress(destination, 100);
					
					refreshTitle(temp_title_set);
					
					//delete the text file
					var delete_url = domain+"include/delete_garbage.php";
					$.post(delete_url, {src: ""+src+""});
				}
		   });
}


function enitiateDownLoad(src, newname){
	var url = domain+"include/enitiate_download.php";
	$.post(url, {src: ""+src+"", new_name: ""+newname+""},
		   function (data){
				if(data == 0){
					//stop the checker
					ok_sign = 0;
				}
		   });	
}

function enitiateConverting(src, newname, destination, lang, video, format){
	var url = domain+"include/enitiate_converting.php";
	
	var old_name = $("#video_title").html();
	
	//get the untranslated name and push it for translation
	var copyright_engine = $("#copyright_engine").val();
	/////////////////////////////////////////////////////////
	$.post(url, {src: ""+src+"", old_name: ""+old_name+"", new_name: ""+newname+"", video: ""+video+"", conv_form: ""+format+"",copyright_engine: ""+copyright_engine+""},
		   function (data){
			  //$("#success").html(data);
				if(data == -1){
					//stop the checker
					ok_sign = 0;
					setResult("success", 'Error while converting');
					
				}else{
					
					//this case catches both successfull convert and unsuccessful converting so returning the src file
					var exploded_data = data.split(":");
						
					ok_sign = 2;
					var url = domain+"include/finished_converting.php";
					$.post(url, {sec_num: ""+exploded_data[0]+"", lang: ""+lang+"", type: ""+exploded_data[1]+""},
						   function (data){
							   $("#success").html(data);
						   });
				}
		   });	
}

//src - the source file
function startDownloading(id, destination, src, format, lang, video){
	var url = domain+"include/search_progress_loader.php";
	
	$.post(url, {rand_id: ""+id+"", lang: ""+lang+""}, 
		function (data){
			$("#"+destination+"").html(data);
			display("search_progress",0,3);
			
			var set_name_url = domain+"include/set_unique_name.php";

			$.post(set_name_url, {src: ""+src+""}, 
				   function (new_name){
					  	//returns the new name
						//enitiate download
						enitiateDownLoad(src, new_name);
						
						var url_search_process = domain+"manual_scripts/test.php";
						updateProcess(url_search_process, "search_progress", 200, format, destination, src, domain+"converted_files/"+new_name, lang, video);
				   });
		});
}

function startConverting(id, destination, src, format, lang, video){
	var url = domain+"include/convert_progress_loader.php";
	
	//getting the translated name (if any)///////
	var translated_name = $("#translated").val();
	///////////////////////////////////////////
	//get the untranslated name and push it for translation
	var untranslated_name = $("#video_title").html();
	/////////////////////////////////////////////////////////
	$.post(url, {rand_id: ""+id+"", lang: ""+lang+""}, 
		function (data){
			$("#"+destination+"").html(data);
			display("convert_progress",0,1);
			
			var set_name_url = domain+"include/set_unique_name_convert.php";
			$.post(set_name_url, {src: ""+src+"",conv_format: ""+format+"",translated: ""+translated_name+"",untranslated: ""+untranslated_name+""}, 
				   function (new_name){
					  	//returns the new name
						//get the source file duration
						var get_dur_url = domain+"manual_scripts/get_duration.php";
						$.post(get_dur_url, {src: ""+src+""},
							   function (duration){
									   
									//enitiate download
									enitiateConverting(src, new_name, destination, lang, video, format);
									
									var url_convert_process = domain+"manual_scripts/read_file.php";
									updateConvertingProcess(url_convert_process, "convert_progress", 200, new_name, duration);
							   });
				   });
			
			
		});
}

function loadExtendedForm(id, destination1, destination2, src, format, lang, video){
	//updates the images
	tintTheImages(id, destination1);
	
	//saving the page title					   
	temp_title_set = document.title;

	//shows the downloading dialog
	startDownloading(id, destination2, src, format, lang, video);
}

function mouseHover(id, src){
	$("#"+id+"").attr("src", src);
}

function subscribeForm(id, lang){
	
	var url = domain+"include/subscribe_form.php";
	$.post(url, {alpha: ""+id+"", lang: ""+lang+""},
		   function (data){
				if(data.length >0) {
					$('#subscribe_form_inner').html(data);
					$('#subscribe_form').css('left', $('#subscribe_link').offset().left-150);
					$('#subscribe_form').css('top', ($('#subscribe_link').offset().top)+20);
					$('#subscribe_form').slideDown("fast");
				}
		   });
}

function postSubscrioption(lang){
	var alpha = $("#subscribe_mail").val();
	var beta = $("#subscr_t").val();
	var url = domain+"include/process_subscription.php";
	$.post(url, {alpha: ""+alpha+"", beta: ""+beta+"",lang: ""+lang+""},
	   function (data){
			if(data.length >0) {
				var splited = data.split(":");
				$('#error').html('');
				$('#error').html(splited[0]);
				$('#error').show();
				if (parseInt(splited[1]) == 1){
					window.setTimeout('close_it()', 4000);
				}
			}
	   });
	return false;
}

function close_it(){
	$('#subscribe_form').slideUp('fast');
	$('#error').html('');
	$('#subscribe_form_inner').html('')
}


//popup window
function handleEvent(src,pic, fixLeft, fixTop) {
	var div;
	var Xcor = 0;
	var Ycor = 0;
		/*if (document.all){
			Xcor = event.clientX 
			Ycor = event.clientY
		}// Gets position for other browsers
		else{  
			Xcor = oEvent.pageX;
			Ycor = oEvent.pageY;
		}  */
		if(document.getElementById)
			// Standard way to get element
			source = document.getElementById(src); 
		else if(document.all) 
			// Get the element in old IE's 
			source = document.all[src];
			
		Xcor = source.offsetLeft;
		Ycor = source.offsetTop;
		
	
		if(document.getElementById)
			// Standard way to get element
			div = document.getElementById(pic); 
		else if(document.all) 
			// Get the element in old IE's 
			div = document.all[pic]; 
		
		// if the style.display value is blank we try to check it out here 
		if(div.style.display==''&&div.offsetWidth!=undefined&&div.offsetHeight!=undefined){
			div.style.display = (div.offsetWidth!=0&&div.offsetHeight!=0)?'block':'none'; 
		}
		div.style.top = (Ycor+fixTop) + "px";
		div.style.left = (Xcor+fixLeft) + "px";
	
		
		// If the PopUp is hidden ('none') then it will display it ('block').
		// If the PopUp is displayed ('block') then it will hide it ('none').
		div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block';
}
//hide the popup
function HidePopup(pic){
	 var div = document.getElementById(pic);
	 div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block';
}

function set_cookie(name, value, expires){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	if (expires){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" );
}

function setLanguage(code){
	document.location.href = domain+code+"/";	
}

//sends the link to the clip to a specified email
function send_to_a_friend(id, num, lang){
	var url = domain+"include/send_to_a_friend.php";
	if (num == 0){
		$.post(url, {unique_num: ""+id+"", num: ""+num+"", lang: ""+lang+""},
			   function(res){
				   $("#send_to_a_friend_form").html(res);
			   });
	}else{
		var name = $("#sender_name").val();
		var email = $("#sender_email").val();
		var descr = $("#mail_text").val();
		
		var url = domain+"include/send_to_a_friend.php";
		$.post(url, {unique_num: ""+id+"", name: ""+name+"", email: ""+email+"", descr: ""+descr+"", num: ""+num+"", lang: ""+lang+""},
			   function(res){
				   if(res == 1){
						$("#hidden_succ").css("display:block");
						window.setTimeout('close_form()', 4000);
				   }else{
						$("#hidden_fail").html(res);   
				   }
			   });	
	}
}

function close_form(){
	$("#send_to_a_friend_form").html('');
}

function idConverted(id){
	var s = id;
	var pathnr = "" + Math.floor(parseFloat(s) / 1000);
	var l = s.length;
	var i = 1;
	
	while (i <= 7 - l) {
		s = "0" + s;
		++i;
	} // end while
	
	l = pathnr.length;
	
	for (i = 1; i <= 7 - l; i++) {
		pathnr = "0" + pathnr;
	} // end of for
	
	var xc = new Array("R", "1", "5", "3", "4", "2", "O", "7", "K", "9", "H", "B", "C", "D", "X", "F", "G", "A", "I", "J", "8", "L", "M", "Z", "6", "P", "Q", "0", "S", "T", "U", "V", "W", "E", "Y", "N");
	var qsum = 0;
	for (i = 0; i <= 6; i++) {
		qsum = qsum + parseInt(s.charAt(i)) * (i + 1);
	} // end of for
	var s1 = "" + qsum;
	qsum = 0;
	for (i = 0; i < s1.length; i++) {
		qsum = qsum + parseInt(s1.charAt(i));
	} // end of for
	var qstr;
	if (qsum >= 10) {
		qstr = "" + qsum;
	}
	else {
		qstr = "0" + qsum;
	} // end else if
	var code = xc[s.charCodeAt(3) - 48 + qsum + 3]+"";
	code += qstr.charAt(1);
	code += xc[s.charCodeAt(0) - 48 + qsum + 2];
	code += xc[s.charCodeAt(2) - 48 + qsum + 1];
	code += xc[s.charCodeAt(5) - 48 + qsum + 6];
	code += xc[s.charCodeAt(1) - 48 + qsum + 5];
	code += qstr.charAt(0);
	code += xc[s.charCodeAt(4) - 48 + qsum + 7];
	code += xc[s.charCodeAt(6) - 48 + qsum + 4];
	var content_video = pathnr + "/" + code + ".flv";
	
	var url = domain+"include/remember_generated_name.php";
	$.post(url, {content_video: ""+content_video+""});	
}

//encode the koi8r encoding
function encodeTitle(video){
	var url = domain+"include/encodeTitle.php";
	$.post(url, {video: ""+video+""});
}

function playlists(varable, lang, destination){
	var url = "include/generate_playlists.php";
	$.post(url, {varable: ""+varable+"", lang: ""+lang+""},
		   function(data){
			 $("#"+destination+"").html(data);  
		   });
}



//opens the add to playlist dialog box
function open_add_to_playlist_dialog(unique, lang){
	var url = domain+"include/dialog_box.php";
	
	$.post(url, {unique: ""+unique+"", lang: ""+lang+""},
		   function(data){
				$("#list_box").html(data);
				$("#dialog_box").fadeIn("fast");
				$('#dialog_box').css('left', -50);
				$('#dialog_box').css('top', -150);

		   });
}

function close_dialog_box(){
	$("#dialog_box").fadeOut("fast");
	$("#list_box").html('');
	$("#action_result").html('');
}

function add_to_playlist(plist, varable, lang){
	
	var url = domain+"include/add_to_playlist.php";
	$.post(url, {plist: ""+plist+"", variable: ""+varable+"", lang: ""+lang+""}, 
		   function (data){
				$("#action_result").html(data);
				window.setTimeout('close_dialog_box()', 2000);
		   });
}

function set_li_bgcolor(element){
	element.style.backgroundColor = '#F8F8F8';
}

function reset_li_bgcolor(element){
	element.style.backgroundColor = '#FFFFFF';
}

function watch_movie(id, lang){
	var url = domain+lang+"/watch/"+id+'/';	
	document.location.href = url;
}

function get_movie(url, lang){
	var url = domain+lang+"/get_video.html?video="+encodeURIComponent(url)+'&submit_link=Get+Video';	
	document.location.href = url;

}

function redirect(url){
	document.location.href = url;
}

function deletePlaylist(id, message){
	var url = domain+"profile/delete_playlist.php";
	go_on = confirm(message);
	if (go_on) {
		$.post(url , {id: ""+id+""},
			   function (data){
				   
					window.location.reload();   
			   });
	}
}

function delete_video_link(id){
	var url = domain+"profile/delete_video.php";
	$.post(url , {id: ""+id+""},
		   function (data){
				window.location.reload();   
		   });
}