////////////////////////////////////////////////////////////////////
//   Script Name: Sh content viewer                              //
//   Version: 0.9                                               //
//   Author: Bracsek  (torokvmit(at)gmail(dot)com)             //
//   Author URI: http://bracsek.extra.hu                      //
//   Last update 2008/09/23                                  //
//   Copyright(c) 2008 DWEB(R)                              //
//   changlog:                                             //
//    - 2008/09/23 added ajax content with scrollable div //
//    External lybrarys: Jquery.js. scroller.js          //
//    Included lybrarys: getPageSizeWithScroll(),       //
//       getScrollXY(),getMouseXY(e),f_client*()       //            
////////////////////////////////////////////////////////

//
// <a href='imageurl' rel='sh'>...</a>
// <a href='iframecontenturl' rel='sh' onclick='sh('iframecontenturl','iframeXwidthXheight[Xnoscroll]')'  >...</a>
// ajax in overflow:auto;
// <a href=\"javascript:;\"   onclick=\"sh('sourcerelativeurl','ajaxXwidthXheight[Xvariable1:value1,variable2:value2...]')\" >...</a> //the variables is post with post method
// ajax in ojavascript scrollable div
// <a href=\"javascript:;\"   onclick=\"sh('sourcerelativeurl','sajaxXwidthXheight[Xvariable1:value1,variable2:value2...]')\" >...</a> //the variables is post with post method

//init
if (!sh_animationlength)     var sh_animationlength      = 700;                      //content div animating time in milisec
if (!sh_faderanimationlength)var sh_faderanimationlength = 600;                     //fading time in milisec
if (!sh_opa)	 			 var sh_opa                  = 60;                     //fading level 1 full 0 transparent
if (!sh_opaszin)             var sh_opaszin              = '#484645';             // color of fading 
if (!sh_loadszin)            var sh_loadszin             = '#5A0614';            // bgcolor of content window
if (!sh_animateborder)	     var sh_animateborder        = "1px solid #000000"; // color of color border  29607A

// global variables
var image = new Image();
var sh_imageloader;
var sh_contentglobal;
var sh_xe,sh_ye,sh_movve;

//////////move functions//////////
// sh_move(event) onmousemove
function sh_move(e){
	if (sh_movve==1){
		document.getElementById("sh_animate").style.left=(getMouseXY(e)[0]-sh_xe)+"px";
		document.getElementById("sh_animate").style.top=(getMouseXY(e)[1]-sh_ye)+"px";
	}
}

// sh_moveinit(event) onmousedown
function sh_moveinit(e){
	sh_movve=1;
	var x = document.getElementById("sh_animate").style.left.replace("px","");
	var y = document.getElementById("sh_animate").style.top.replace("px","");
	sh_xe = getMouseXY(e)[0] - x;
	sh_ye = getMouseXY(e)[1] - y;
}

// sh_stop() onmouseup
function sh_stop(){
	sh_movve=0;
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// stop multi onclick action
function stopp(e){
	if (window.event) {
		window.event.cancelBubble = true;
	}
	else {
		e.stopPropagation();
	}
}


// close sh window
function rejt(){
		var selects = document.getElementsByTagName('select');
		for (var i=0; i<selects.length;i++){
			selects[i].style.visibility='visible';
		}
		$("#shadowdiv").animate ({opacity:0},sh_faderanimationlength/2);
		document.getElementById("shadowcontentdiv").style.display="none";
		document.getElementById("scontent").style.width="0px";
		document.getElementById("scontent").style.height="0px";
		
		window.setTimeout('document.getElementById("shadowdiv").style.display="none"',sh_faderanimationlength/2);
}


//content loader after animation
function load(){
document.getElementById("scontent").style.overflow="hidden";
document.getElementById("sh_animate").style.textAlign="left";
	if (sh_contentglobal.indexOf("sajaxX")!=-1){
			var map = {};
			if (sh_contentglobal.split("X")[3]){
				var valtozok = sh_contentglobal.split("X")[3].split(",");
				for (var i = 0 ; i< valtozok.length;i++){
					map[valtozok[i].split(":")[0]]= valtozok[i].split(":")[1];
				}
			}
			document.getElementById("scontent").style.padding="20px";
			document.getElementById("scontent").style.paddingTop="0px";
			document.getElementById("scontent").style.margin="0px";
			document.getElementById("scontent").style.textAlign="left";
			document.getElementById("scontent").style.width=(image.width)+"px";
			document.getElementById("scontent").style.height=(image.height-20)+"px";
						
			document.getElementById("scontent").innerHTML = scroller(image.width, image.height,  '');
			scroller_init();
			$('#scroller_content').load(image.tsrc, map);
			return;
	} 
	
		if (sh_contentglobal.indexOf("ajaxX")!=-1){
			var map = {};
			if (sh_contentglobal.split("X")[3]){
				var valtozok = sh_contentglobal.split("X")[3].split(",");
				for (var i = 0 ; i< valtozok.length;i++){
					map[valtozok[i].split(":")[0]]= valtozok[i].split(":")[1];
				}
			}
			document.getElementById("scontent").style.padding="20px";
			document.getElementById("scontent").style.paddingTop="0px";
			document.getElementById("scontent").style.margin="0px";
			document.getElementById("scontent").style.textAlign="left";
			document.getElementById("scontent").style.width=(image.width-20)+"px";
			document.getElementById("scontent").style.height=(image.height-20)+"px";
			document.getElementById("scontent").style.overflow="auto";
			$('#scontent').load(image.tsrc, map);
			return;
	} 
	
		document.getElementById("scontent").style.width=(image.width)+"px";
		document.getElementById("scontent").style.height=(image.height-20)+"px";
		document.getElementById("scontent").innerHTML=sh_contentglobal;
		document.getElementById("scontent").style.padding="20px";
		document.getElementById("scontent").style.paddingTop="0px";
		document.getElementById("scontent").style.margin="0px";
		document.getElementById("shadowcontentdiv").style.display="block";
		document.body.focus();
}

//animation
function animate(content){
		document.getElementById("sh_animate").style.position="relative";
		document.getElementById("sh_animate").style.width="0px";
		document.getElementById("sh_animate").style.height="0px";
		document.getElementById("sh_animate").style.overflow = "hidden";
		document.getElementById("sh_animate").style.marginLeft=parseInt(((image.width/2)+""))+"px";
		document.getElementById("sh_animate").style.marginTop=parseInt(((image.height/2)+""))+"px";
		document.getElementById("sh_animate").style.display="none";
		document.getElementById("sh_animate").style.background=sh_loadszin;
		document.getElementById("scontent").innerHTML="";
		document.getElementById("shadowcontentdiv").style.display="block";
		sh_contentglobal= content;
		document.getElementById("scontent").innerHTML="";
		document.getElementById("scontent").style.width="0px";
		document.getElementById("scontent").style.height="0px";
		
		window.setTimeout('load()',sh_animationlength);
		var plus = 40;
		if (navigator.appName=="Microsoft Internet Explorer") {
			plus = 20;
		}
		$("#sh_animate").animate ({marginLeft:"0",marginTop:"0",width:(image.width+plus) ,height:(image.height+plus)},sh_animationlength);			  
		document.getElementById("sh_animate").style.position="absolute";
		document.getElementById("sh_animate").style.left=parseInt(((f_clientWidth()-image.width)/2-20))+"px";
		document.getElementById("sh_animate").style.top=parseInt((((f_clientHeight()-image.height)/2-20)))+"px";
		document.getElementById("shadowdiv").onclick= new Function('rejt();');
		document.getElementById("shadowcontentdiv").onclick= new Function('rejt();');
}

//background fader
function sh_fader(){
	if (navigator.appName=="Microsoft Internet Explorer") {
		document.getElementById("shadowdiv").style.filter='alpha(opacity=0)';
	}else {
		document.getElementById("shadowdiv").style.mozOpacity="0.00";
		document.getElementById("shadowdiv").style.opacity= "0.00";
	}
	document.getElementById("shadowdiv").style.display="block";
	$("#shadowdiv").animate ({opacity:"0."+sh_opa},sh_faderanimationlength);	
}


//main function
function sh(content,img){
document.getElementById("shadowdiv").style.height=getPageSizeWithScroll()[1]+"px";
document.getElementById("scontent").innerHTML="";
//hidden select tags for ie 6
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if ((browser=="Microsoft Internet Explorer")&& (version<6.5)){
	var selects = document.getElementsByTagName('select');
	for (var i=0; i<selects.length;i++){
		selects[i].style.visibility='hidden';
	}
}


//for ajax with and witout scroller
	if (img.indexOf("ajaxX")!=-1){
		var dim = img.split("X");
		image.height=dim[2];
		image.width=dim[1];
		if (image.height > f_clientHeight()-40 || image.width > f_clientWidth()-40) { 
			if(image.height /image.width > f_clientHeight() /  f_clientWidth()) {
				image.width = ((image.width* (f_clientHeight()-80))/image.height); 
				image.height = f_clientHeight()-60; 
			} else {
				image.height = ((image.height* (f_clientWidth()-80))/image.width); 
				image.width = f_clientWidth()-60; 
			}
			
		}
		image.tsrc=content;
		sh_fader();
		animate(img);
		img = null;
		return;		
	}
	
// for iframes		
		if (img.indexOf("iframeX")!=-1){
			var dim = img.split("X");
			image.height=dim[2];
			image.width=dim[1];
			if (image.height > f_clientHeight()-40 || image.width > f_clientWidth()-40) { 
				if(image.height /image.width > f_clientHeight() /  f_clientWidth()) {
					image.width = ((image.width* (f_clientHeight()-80))/image.height); 
					image.height = f_clientHeight()-60; 
				} else {
					image.height = ((image.height* (f_clientWidth()-80))/image.width); 
					image.width = f_clientWidth()-60; 
				}
				
			}
			
			sh_fader();
			
			if (dim[3]=='noscroll'){
				var scroll=" SCROLLING=NO ";
			} else {
				var scroll="";
			}
			
			animate("<iframe src='"+content+"' "+scroll+"  width='"+image.width+"' height='"+image.height+"' frameborder=\"0\" border=0 style='border:0px solid #ff0000;' ></iframe> ");
			img = null;
			return;
		}
		
//for html content		
		if (!img){
			sh_fader();
			
			if (!sh_higher){
			var sh_higher = document.createElement("div");
			} else {
			sh_higher = document.createElement("div");
			}
			sh_higher.id = "sh_higher";
			document.body.insertBefore(sh_higher, document.body.lastChild);
			
			document.getElementById("sh_higher").style.display="block";
			document.getElementById("sh_higher").innerHTML=""+content;
			
			document.getElementById("sh_higher").style.height="auto";
			if (document.all) {
				gh = document.getElementById("sh_higher").offsetHeight+10;
				gw = document.getElementById("sh_higher").offsetWidth+10;
			} else {
				gh = document.getElementById("sh_higher").offsetHeight;
				gw = document.getElementById("sh_higher").offsetWidth;
			}
			image.height=gh;
			image.width="400";
			document.getElementById("sh_higher").style.display="none";
			animate(content);
		
// for images
		} else {
			sh_fader();
			if (img!='') {
				image = null;
				image= new Image();
				image.src=img;
				sh_contentglobal=content;
				sh_imageloader = window.setInterval ( "if (image.width) { if (image.height > f_clientHeight()-40 || image.width > f_clientWidth()-40) { if(image.height /image.width > f_clientHeight() /  f_clientWidth()) { image.width = ((image.width* (f_clientHeight()-80))/image.height);   image.height = f_clientHeight()-60; }  else { image.height = ((image.height* (f_clientWidth()-80))/image.width); image.width = f_clientWidth()-60;}   }   animate('<img src=\"'+image.src+'\" width=\"'+image.width+'\" height=\"'+image.height+'\" />');  window.clearTimeout(sh_imageloader); } ", 100 );
				//animate(content);
			}
		}
		
}

// verticall scroll content
function shxscroller(){
		if (image.height<(getPageSizeWithScroll()[1]-80)){
			document.getElementById("shadowcontentdiv").style.top=getScrollXY()[1]+"px";
		}
}
	
//script loader	
function initshadow(){

	document.getElementsByTagName('body')[0].style.margin='0px';
	document.getElementsByTagName('body')[0].style.padding='0px';
	document.getElementsByTagName('body')[0].style.border= '0px solid transparent';

		var sh_shadowdiv = document.createElement("div");
		
		sh_shadowdiv.id="shadowdiv";
		sh_shadowdiv.style.zIndex ="10";
		sh_shadowdiv.style.display ="none";
		sh_shadowdiv.style.margin="0px";
		sh_shadowdiv.style.padding="0px";
		sh_shadowdiv.style.position="absolute";
		sh_shadowdiv.style.top="0px";
		sh_shadowdiv.style.left="0px";
		sh_shadowdiv.style.width="100%";
		sh_shadowdiv.style.height="100%";
		sh_shadowdiv.style.background=sh_opaszin;
		sh_shadowdiv.onclick = new Function('this.style.display="none";document.getElementById("shadowcontentdiv").style.display="none";');
		document.body.appendChild(sh_shadowdiv);
		
		var sh_shadowcontentdiv = document.createElement("div");
		sh_shadowcontentdiv.id="shadowcontentdiv";
		sh_shadowcontentdiv.align = "center";
		sh_shadowcontentdiv.valign = "center";
		sh_shadowcontentdiv.style.zIndex ="11";
		sh_shadowcontentdiv.style.textAlign ="center";
		sh_shadowcontentdiv.style.display ="none";
		sh_shadowcontentdiv.style.margin="0px";
		sh_shadowcontentdiv.style.padding="0px";
		sh_shadowcontentdiv.style.position="absolute";
		sh_shadowcontentdiv.style.top="0px";
		sh_shadowcontentdiv.style.left="0px";
		sh_shadowcontentdiv.style.width="99%";
		sh_shadowcontentdiv.style.height="99%";	
		sh_shadowcontentdiv.onclick = new Function('this.style.display="none";document.getElementById("shadowdiv").style.display="none";');
		sh_shadowcontentdiv.innerHTML = '<table onMousemove=\'sh_move(event);\' onMouseup=\'sh_stop();\' style=\'width:100%;height:100%\'><tr><td  style=\'width:100%;height:100%\' align=center valign=center ><div id=sh_animate style=\'background:'+sh_loadszin+';border:'+sh_animateborder+';overflow : hidden;z-index:14;text-align:center;cursor:move;\' onclick=\'stopp(event);\' onMousemove=\'sh_move(event);\' onMouseup=\'sh_stop()\'  onMousedown=\'sh_moveinit(event);stopp(event)\' ><div onMousedown="sh_stop();stopp(event)" id=scontent style=\'z-index:15;cursor:default\' ></div></div></td></tr></table>'+
		'';		
		document.body.appendChild(sh_shadowcontentdiv);
		
		
		document.getElementById("shadowdiv").style.height=getPageSizeWithScroll()[1]+"px";
		
		//explorer szellesseg fix
		if (navigator.appName=="Microsoft Internet Explorer") {
			document.getElementById("shadowdiv").style.filter='alpha(opacity=0.'+sh_opa+')';
			document.getElementById("shadowdiv").style.height=(getPageSizeWithScroll()[1]-4)+"px";
		}else {
			document.getElementById("shadowdiv").style.mozOpacity="0."+sh_opa;
			document.getElementById("shadowdiv").style.opacity= "0."+sh_opa;
		}
		
		var i=0;
		var links = document.getElementsByTagName('a');
		for (i=0; i<links.length;i++){	
			if (links[i].rel=='sh'){
				if (typeof(links[i].onclick)=='function'&&typeof(sh)=='function') {
					if ((""+links[i].onclick+"").indexOf('sh(')!=-1){
					links[i].href='javascript:;';
					}
				} else {
					if (typeof(sh)=='function'){	
						links[i].onclick = new Function('sh("<img style=\\\"border:3px solid #ff0000;\\\" src=\\"'+links[i].href+'\\" />","'+links[i].href+'")');
						links[i].href='javascript:;';
					}
				}
			}
		}
		setInterval ( "shxscroller()", 100 );	
		document.getElementById("sh_animate").innerHTML="<div style='height:22px;'><p align=right style='margin:0px;padding:0px;margin-top:-0px;right:0px;padding-top:2px;padding-right:4px;' ><font color='#ff0000' style='font-weight:bold;'><a style='color:#ff0000;font-size:14px;font-weight:normal;text-decoration:none' href='javascript:void(0);' onclick='rejt(\"\");' >X</a></font></p></div>"+document.getElementById("sh_animate").innerHTML;
}

// loading script
if (window.attachEvent) {
	window.attachEvent("onload", initshadow); 
} else {
	window.addEventListener("load", initshadow, false);
}

// "library :))"
function getPageSizeWithScroll(){
		if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
			xWithScroll = document.body.offsetWidth;
	  	}
		return [ xWithScroll, yWithScroll ];;
}

	
function getScrollXY() { 
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return [ scrOfX, scrOfY ];
}

// mouse capturing
var IE = document.all?true:false
//if (!IE) document.captureEvents(Event.MOUSEMOVE);

if (window.addEventListener) {
	window.addEventListener("Event.MOUSEMOVE", {}, true); 
} else {
	if  (document.captureEvents && Event.MOUSEMOVE) {
		document.captureEvents(Event.MOUSEMOVE);
	}
}





function getMouseXY(e) {
  if (IE) {
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else { 
    tempX = e.pageX
    tempY = e.pageY
  }  
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  return [ tempX, tempY ];
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
