﻿var isdrag = false;
var thisx, thisy;
var dobj;
var runningfade = 0;

// perform draging of popup div
function movemouse( e ) {
	if( isdrag ) {
		if (!e){ // cross browser conversion
			var e = window.event;
		}			
		if(e.clientX){ // cross browser co-ordinates - works in most browsers, safari, FF, even IE8
			dobj.style.left = tx + e.clientX - thisx + "px";
			dobj.style.top = ty + e.clientY - thisy + "px";
		}
		else{ //IE safe
			dobj.style.left = tx + event.clientX - thisx + "px";
			dobj.style.top = ty + event.clientY - thisy + "px";
		}				
		resizeOverlay(); // update overlay size and fix scrolling
		return false;
	}
}
function selectmouse( e ) {
	var fobj;
	
	if (!e){ // cross browser conversion
		var e = window.event;
	}		

	if (e.target){ // cross browser event target - works in most browsers, safari, FF, even IE8
		fobj = e.target;
	}
	else if (e.srcElement){ // IE only ?
		fobj = e.srcElement;
	}
	else if (event.srcElement){ // IE only ?
		fobj = event.srcElement;
	}
	else{
		return; // disable drag function (fail safe)
	}
	
	if (fobj.nodeType == 3){ // defeat Safari bug - does not seem to be doing much, but recommended.
		fobj = fobj.parentNode;
	}
	
	if (fobj.id == "dragdiv" || fobj.id == "dragpopuptitle") { //
		isdrag = true;
		dobj = document.getElementById("listingimagepopup");
		tx = parseInt(dobj.style.left+0);
		ty = parseInt(dobj.style.top+0);
			
		if(e.clientX){ // cross browser co-ordinates - works in most browsers, safari, FF, even IE8
			thisx = e.clientX;
			thisy = e.clientY;
		}
		else if(e.pageX){
			thisx = e.pageX;
			thisy = e.pageY;
		}
		else{ //IE safe
			thisx = event.clientX;
			thisy = event.clientY;
		}
		
		document.onmousemove=movemouse;
		return false;
	}
}
//document.onmousedown=selectmouse; // call directly from DIV instead as onMouseDown="selectmouse(event);"
document.onmouseup=new Function("isdrag=false"); // clear mouse event switch

// Browser safe opacity handling function
function setOpacity( value ){	
	var value2 = value;
	var value3;
	var maxoverlayopacity = 70;
	var maxpopupopacity = 95;
	
	if (value <= 100){
		if (value2 >= maxoverlayopacity){
			value2 = maxoverlayopacity;
		}
		document.getElementById("overlay").style.opacity = value2 / 100;
		document.getElementById("overlay").style.filter = 'alpha(opacity=' + value2 + ')';
	}
	if (value >= 100){
		value3 = value - 100;
		if (value3 >= maxpopupopacity){
			value3 = maxpopupopacity;
		}
		document.getElementById("listingimagepopup").style.opacity = value3 / 100;
		document.getElementById("listingimagepopup").style.filter = 'alpha(opacity=' + value3 + ')';
	}
}

// fade window to show popup	
function fadeInMyPopup(){
	for( var i = 0 ; i <= 200 ; i++ ){
		setTimeout( 'setOpacity(' + i + ')' , 3 * i );
	}
}

// Fade window back to normal, and call de-activate divs
function fadeOutMyPopup(){
	if (runningfade == 0){
		runningfade = 1;
		for( var i = 0 ; i <= 200 ; i++ ){
			setTimeout( 'setOpacity(' + (200 - i) + ')' , (3 * i));
		}
		setTimeout( 'closeMyPopup()' , 800 );
	}
}

// de-activate divs
function closeMyPopup(){
	document.getElementById("listingimagepopup").style.display = "none"
	document.getElementById("overlay").style.display = "none";
	runningfade = 0;
}

// activate popup and call fade window
function fireMyPopup(){
	document.getElementById("overlay").style.opacity = 0;
	document.getElementById("overlay").style.filter = 'alpha(opacity=' + 0 + ')';
	
	document.getElementById("listingimagepopup").style.opacity = 0;
	document.getElementById("listingimagepopup").style.filter = 'alpha(opacity=' + 0 + ')';
	
	document.getElementById("listingimagepopup").style.display = "block";
	document.getElementById("overlay").style.display = "block";

	resizeOverlay();
	fadeInMyPopup();	
}

// adjust overlay size to fill visible window
function resizeOverlay() {
	if (document.getElementById("listingimagepopup").style.display == "block"){
		var vp = getViewport();
		
		var test1 = parseInt(document.body.scrollWidth+0);
		var test2 = parseInt(document.body.offsetWidth+0);
		if (test1 > test2){ // document size on FF and Safari OK -IE4 compatible
			var calcwidthscroll = parseInt(document.body.scrollWidth+0);
			var calcheightscroll = parseInt(document.body.scrollHeight+0);
			var calcright = parseInt(document.getElementById("listingimagepopup").style.left+0) + document.getElementById("listingimagepopup").scrollWidth;
		}
		else{ // Explorer Mac, would also work in Explorer 6 Strict, Mozilla and Safari
			var calcwidthscroll = parseInt(document.body.offsetWidth+0);
			var calcheightscroll = parseInt(document.body.offsetHeight+0);
			var calcright = parseInt(document.getElementById("listingimagepopup").style.left+0) + document.getElementById("listingimagepopup").offsetWidth;
		}
				
		//keep div width inside visible area if possible, otherwise resize it accordingly
		if (calcright < vp.width && calcwidthscroll < vp.width){
			document.getElementById("overlay").style.width = (calcwidthscroll - 0) + "px";
		}
		else if(calcright < vp.width && vp.width <= calcwidthscroll){
			document.getElementById("overlay").style.width = (vp.width - 0) + "px";
		}
		else if(calcright >= calcwidthscroll && calcwidthscroll < vp.width){
			document.getElementById("overlay").style.width = (calcright + 1) + "px";
		}
		else{ // set div width to be same as scroll - increases width
			document.getElementById("overlay").style.width = (calcwidthscroll - 2) + "px";
		}
		
		// set overlay height to be same as scroll height (should be full document height)
		document.getElementById("overlay").style.height = calcheightscroll + "px";
	}
}
// work out visible overlay size when window re-sized	
function getViewport() {
	var v = {width:0, height:0};
	
	if (window.innerHeight) { // windows size in all except IE, slightly buggy in Safari, scrolls not fully returning. - non IE (should be most compatible)
		v.height = parseInt(window.innerHeight+0);
		v.width = parseInt(window.innerWidth+0);
	}
	else if (document.body.clientHeight) { // document size on FF and Safari OK -IE4 compatible
		v.height = parseInt(document.body.clientHeight+0);
		v.width = parseInt(document.body.clientWidth+0);
	}
	else{ // document/window size in FF - IE 6+ in 'standards compliant mode'
		v.height = parseInt(document.documentElement.clientHeight+0);
		v.width = parseInt(document.documentElement.clientWidth+0);
	}
	return v;
}
	
// get popup content width and size popup to fit content (needed for IE only)
function resizeImagePopup(){
	// Check for IE 6 or 7, to set width of popup DIV (as auto means full width in older versions)
	var getIEversion = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer'){
		var navua = navigator.userAgent;
		var navre = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (navre.exec(navua) != null){
			getIEversion = parseFloat( RegExp.$1 );
			
			if (getIEversion > -1 && getIEversion < 8.0 ){
				document.getElementById("listingimagepopup").style.width = document.getElementById("fullsizeimage").width + 16 + "px";
			}	
		}
	}
}



