// openFlag Flag
var openFlag = false;
var base_admin_url = "http://192.168.100.254/Imago2/Admin/upload/";


jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}



function OpenPopup(project_name,image_file,width,height) {
	

	if (openFlag==false) {
		// otwariame go
		openFlag=true;
		// open the popup
		
		
		// Finding and position of the popup
		var BodyCoors=getPageCoords($('body'));

		

		$('#popup_window').css("display","block");
		$('#fade').fadeIn("fast");
		//$('#fade').css("display","block");

		// stretch overlay to fill page and fade in
    	var arrayPageSize = getPageSize();
   		$('#fade').css({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
		
		// --------------------------------
		//  Loading the content

		image_ext = image_file.substr(-3);

		switch(image_ext) {
			
			// flash banner
			case "swf": 
				createPlayer(base_admin_url+image_file,'popup_content',width,height);
			break;

			default:
				$("#popup_content").css({ width: width + 'px', height: height + 'px' });
				$("#popup_content").html("<img src='"+base_admin_url+image_file+"'>");

			break;

		}

		$('#project_name').html(project_name);

		// centering the popup
		$('#popup_window').center();


		// --------------------------------

	} else {
		// zatwariame go
		openFlag=false;
		
		$('#popup_window').css("display","none");
		$('#fade').css("display","none");
	}
	
}	
// -----------------------------


function PopupClose() {
	$('#popup_window').fadeOut("fast");
	//$('#popup_window').css("display","none");
	$('#fade').css("display","none");
	
	// nulirame flaga na listennow
	openFlag=false;

}



function getPageCoords (el) {
  var coords = {x: 0, y: 0};
  do {
    coords.x += el.offsetLeft;
    coords.y += el.offsetTop;
  }
  while ((el = el.offsetParent));
  return coords;
}  



function getPageSize() {
	        
     var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function createPlayer(theFile,destinationDiv,width,height) {

	var s1 = new SWFObject(theFile,'player',width,height,'9');
	s1.addParam('allowfullscreen','false');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('wmode','transparent');
	s1.write(destinationDiv);

	
}