//Global variables
var max_w;
var max_h;
var body_height;
var margin_top;
var image_path;
var image_header;

var image_h;
var image_w;
//padding settings for the content
var content_padding = 15;

//for the Ajax request
var http_image = create_object();
var nocache_image = 0;


//0 means disabled, 1 means enabled;  
var status = 0;

function popup_open(){
popup_center();  
popup_load(); 
}

//loading popup with jQuery  
function popup_load(){ 
 
//loads popup only if it is disabled  
if(status==0){  
	$("#popupBackground").css({  
	"opacity": "0.7"  
	});  
	$("#popupBackground").fadeIn("fast");  
	$("#popup").fadeIn("fast");  
	status = 1;  
}  

}

//disabling popup with jQuery
function popupDisable(){

//disables popup only if it is enabled  
if(status==1){  
	$("#popupBackground").fadeOut("fast");  
	$("#popup").fadeOut("fast");  
	status = 0; 
}
  
}

//centering popup  
function popup_center(){  

//request data for centering  
var windowWidth = document.documentElement.clientWidth;  
var windowHeight = document.documentElement.clientHeight;  
var popupHeight = $("#popup").height();  
var popupWidth = $("#popup").width();  
//centering  
$("#popup").css({  
	"position": "fixed",  
	"top": windowHeight/2-popupHeight/2,  
	"left": windowWidth/2-popupWidth/2  
});  

//only need force for IE6  
$("#popupBackground").css({  
	"height": windowHeight  
});  
  
} 

//call on click on X image
function popupclose(){

document.getElementById('popupBody').innerHTML = '';
popupDisable();
//if nees to reload the page after close
//window.location=window.location;

}


//functions for calling the popup with content

//for youtube videos
function popup_video(path,header, counter_file){

//if wish to include autoplay optional attribute
/*if (autoplay===true){
	autoplay = 1;
}else{
	autoplay = 0;
}*/

if (header){
	document.getElementById('popup_h1').innerHTML = header;
}else{
	document.getElementById('popup_h1').innerHTML = "Video";
}

//call function for finding the popup max width/height
set_max_sizes();

//insert video
document.getElementById('popupBody').innerHTML = '';
document.getElementById('popupBody').innerHTML += "<table border=\"0\" style=\"width:100%;vertical-align:middle;margin-top:" + (margin_top + content_padding) + "px;\"><tr><td>";
document.getElementById('popupBody').innerHTML += "<object height=\"" + max_h + "\" width=\"" + max_w + "\"><param name=\"movie\" value=\"" + path + "hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999&amp;border=1&amp;autoplay=1\" /><param name=\"allowFullScreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><embed allowfullscreen=\"true\" allowscriptaccess=\"always\" height=\"" + max_h + "\" src=\"" + path + "&hl=en_US&fs=1&color1=0x3a3a3a&color2=0x999999&border=1&autoplay=1\" type=\"application/x-shockwave-flash\" width=\"" + max_w + "\"></embed></object>";
document.getElementById('popupBody').innerHTML += "</td></tr></table>";

//open the popup
popup_open();

//counter_file is optional for hitting the video counter
if (counter_file){
	hit_counter(counter_file);
}	

}


//for images
function popup_image(path,header){

image_path = path;
image_header = header;
get_image_size(path);

}

//after getting response from server
function popup_image_continue(path,header){

//call function for finding the popup max width/height
set_max_sizes();

//the proportion width/height of the image
var proportion = image_w/image_h;

//insert the image to the popup
document.getElementById('popupBody').innerHTML = '';
document.getElementById('popupBody').innerHTML = "<table border=\"0\" style=\"width:100%;height:" + body_height +"px;vertical-align:middle;margin-top:" + margin_top + "px;\"><tr><td><img class=\"popup_image\" id=\"popup_image\" alt=\"\" src=" + path + " /></td></tr></table>";

//set width/height and keep the original proportions
if ((image_h > max_h) || (image_w > max_w)){
	if (proportion >= 1){
		//setting by the width
		$("img[class=popup_image]").css("width",max_w + "px");
		if (parseInt(max_w/proportion) > max_h){
			$("img[class=popup_image]").css("height",max_h + "px");
			$("img[class=popup_image]").css("width",parseInt(max_h*proportion) + "px");
		}else{
			$("img[class=popup_image]").css("height",parseInt(max_w/proportion) + "px");
		}
	}else{
		$("img[class=popup_image]").css("height",max_h + "px");
		$("img[class=popup_image]").css("width",parseInt(max_h*proportion) + "px");
	}
}

//set header
if (header){
	document.getElementById('popup_h1').innerHTML = header;
}else{
	document.getElementById('popup_h1').innerHTML = path;
}

//open the popup
popup_open();

}


function create_object(){
	var request_type;
	
	try{
		// Opera 8.0+, Firefox, Safari
		request_type = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			request_type = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				request_type = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// other
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return request_type;
}


function get_image_size(path) {

// Set random number to add to URL request (for IE)
nocache = Math.random();
//http_image.open('get', 'assets/popup/image_size.php?path='+path+'&nocache = '+nocache_image);
http_image.open('get', 'assets/popup/image_size.php?path='+path+'&nocache = '+nocache_image);
http_image.onreadystatechange = get_image_sizeReply;
http_image.send(null);

}

function get_image_sizeReply() {

if(http_image.readyState == 4){
	var response = http_image.responseText;
	//alert(response);
	if (response !== 'error'){
		//if success
		var pos = response.indexOf(';');
		image_w = response.substr(0,pos);
		image_h = response.substr(pos+1);
		popup_image_continue(image_path,image_header);
	}else{
		//error
		document.getElementById('popupBody').innerHTML = "<p>&nbsp;</p><p>&nbsp;</p><p style=\"font-size:medium;" + "padding-left:" + content_padding + "px;\">Error.</p>";
		document.getElementById('popup_h1').innerHTML = "Error";
		popup_open();
	}
}

}


function set_max_sizes(){

//getting the height and width settings of the popup
max_h = parseInt($("#popup").css("height")) - parseInt($("#popupHeader").css("height"));
max_w = parseInt($("#popup").css("width"));
body_height = max_h;

//removing from the maximum height and width for the content the padding
max_h = (parseInt(max_h) - (content_padding*2));
max_w = (parseInt(max_w) - (content_padding*2));

//margin-top for the content of the popup
margin_top = parseInt($("#popupHeader").css("height"));

}


var counter = create_object();	
//for hitting counter of the video - get text file name and enhance it's value by 1
function hit_counter(counter_file){
//document.getElementById('test').innerHTML = "1";
	//var url = "assets/popup/counter.php";
	var url = "assets/popup/counter.php";
	var params = 'file='+counter_file;
//	alert(counter_file);
	counter.open("POST", url, true);

	//Send the proper header information along with the request
	counter.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	counter.setRequestHeader("Content-length", params.length);
	counter.setRequestHeader("Connection", "close");

	//Call a function when the state changes
	counter.onreadystatechange = function() {
		if(counter.readyState == 4 && counter.status == 200) {
			var response = counter.responseText;
			//test
			//document.getElementById('test').innerHTML = response;
			//if details allredy filled - show them
			if ((response != 'false') && (response.length < 20)){
				//counter worked
			//	alert(response);
			}	
		}
	}
	counter.send(params);

}

