///////////////////////////////////////////////////////////////////////
//   Script Name: Sh content viewer                                 //
//   Version: 0.95                                                 //
//   Author: Bracsek  (bracsek(at)dweb(dot)ro)                    //
//   Author URI: http://bracsek.extra.hu                         //
//   Last update 2009/04/20                                     //
//   Copyright(c) 2009 DWEB(R)                                 //
//   changlog:                                                //
//   External lybrarys: Jquery.js,ui.core.js,ui.draggable.js //
//   Included lybrarys: f_client*()                         //
//                                                         //            
////////////////////////////////////////////////////////////

//
// <a href='imageurl' rel='sh' [rev='gal*'] [title=".htmlspecialchars('mit ')."]  >...</a>
var box1,box2;

var shBox = function(){

this.box;     //elsotetedo box 
this.boxC;    //fixed pozicioju div
this.boxCellContent; //ebben jelenik meg a tartalom
this.boxCTable;
var opacityLevel = "0.4";
var fadingTime = 700;
var animateLength = 400;
var keptarak = new Array();
var keptarakLinkek = new Array();

this.sh_imageloader;
this.boxCell;
this.boxLabel;
this.init = function(){
	this.hatterDoboz();
	this.changeAnchors();
}

this.fadeIn = function(){
	var selects = document.getElementsByTagName('select');
	for (var i=0; i<selects.length;i++){
		selects[i].style.visibility='hidden';
	}
	$(box).css({opacity:"0.0"});
	box.style.display = "block";
	$(box).animate({opacity:opacityLevel},fadingTime, function(){boxC.style.display = "block";} );
	boxC.onclick = function(){shbox.fadeOut();}
}

this.fadeOut = function(){
	boxC.style.display = "none";
	$(box).animate({opacity:"0.0"},fadingTime/2, function(){box.style.display = "none";});
	var selects = document.getElementsByTagName('select');
	for (var i=0; i<selects.length;i++){
			selects[i].style.visibility='visible';
	}
}

this.loadContentIframe = function(url, width, height){
	
	boxLabel.innerHTML = "";
	$(boxCellContent).css('background','url(js/shimages/tolto.gif) center center no-repeat');
	if (box.style.display != "block" ) shbox.fadeIn();
	$(boxCellContent).animate({width:width,height:height},animateLength, function(){
		var iframe = "<iframe src='"+url+"' border=0 frameborder=0 ALLOWTRANSPARENCY=true width='"+width+"' height='"+height+"' ></iframe>";
		$(boxCellContent).css('background','transparent');
		//alert(iframe)
		boxCellContent.innerHTML = iframe;
	});
	
}

this.loadContentImage = function(img, gal, index, subtext){
	image = null;
	image= new Image();
	image.src=img;
	$(boxCellContent).empty();
	boxLabel.innerHTML = "";
	//$(boxCellContent).html("<img src='js/shimages/tolto.gif' />");
	$(boxCellContent).css('background','url(js/shimages/tolto.gif) center center no-repeat');
	
	//$(boxCellContent).html("<embed alt='Loading...' mode='transparent' src='http://dev.dweb.ro/stagecompany/tolto.swf' height='30' width='70'  >");
	//$(boxCellContent).css({marginTop:$(boxCellContent).height()/2-50});
	
	//$(boxCellContent).css({height:$(boxCellContent).height()/2+50});
	if (box.style.display != "block" ) shbox.fadeIn();
	shbox.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;}   }   shbox.animateImage(image.src,image.width,image.height,'"+gal+"','"+index+"','"+subtext+"');  window.clearTimeout(shbox.sh_imageloader); } ", 100 );
	
}

this.animateImage = function(url,width,height,gal,index, subtext){
	window.clearTimeout(shbox.sh_imageloader);
	
	$(box).css('opacity',opacityLevel-0.01);
	$(box).animate({opacity:opacityLevel},animateLength);
	
	$(boxCellContent).animate({width:width,height:height},animateLength, function(){
		$(boxCellContent).css({marginTop:0});
		$(boxCellContent).css({opacity:"1.0"});
		$(boxCellContent).css({height:height});
		var img = document.createElement("img");
		img.src = url;
		img.width  = width;
		img.height = height;
	$(boxCellContent).empty();	
	if (gal){
		var div = document.createElement("div");
		div.style.height=height+"px";div.style.width=width+"px";
		div.style.position = "absolute";
		var divb = document.createElement("div");
		divb.style.height="100%";divb.style.width= width/2 -2 +"px";
		divb.style.border = "0px solid #ff0000";
		divb.style.background = "transparent url('images') ";
		divb.style.cursor = "pointer";
		divb.style.cssFloat = "left";
		divb.style.styleFloat = "left";
		var divj = document.createElement("div");
		divj.style.height="100%";divj.style.width= width/2 -2 +"px";
		divj.style.cursor = "pointer";
		divj.style.border = "0px";
		divj.style.background = "transparent url('images') ";
		divj.style.cssFloat = "right";
		divj.style.styleFloat = "right";
		
		
		
		divb.onmouseover = new Function("this.style.background='url(js/shimages/nyilj.png) 0px 50px no-repeat'");
		divj.onmouseover = new Function("this.style.background='url(js/shimages/nyilb.png)  right 50px no-repeat'");
		if($.browser.msie&&$.browser.version < 7){
			divb.onmouseover = new Function("this.style.background='url(js/shimages/nyilj.gif) 0px 50px no-repeat'");
			divj.onmouseover = new Function("this.style.background='url(js/shimages/nyilb.gif)  right 50px no-repeat'");
		}
		divb.onmouseout = new Function("this.style.background='transparent url(js/shimages/empty.gif)'");
		divj.onmouseout = new Function("this.style.background='transparent url(js/shimages/empty.gif)'");

		
		if(index>0){
				divb.onclick = function(){ shbox.loadContentImage(keptarakLinkek[gal][(index*1-1)].href,gal,index*1-1,keptarakLinkek[gal][(index*1-1)].subtext)};
			} else {
				divb.onclick = function(){ shbox.loadContentImage(keptarakLinkek[gal][keptarakLinkek[gal].length-1].href,gal,keptarakLinkek[gal].length-1,keptarakLinkek[gal][keptarakLinkek[gal].length-1].subtext)};
			}
			
			if(index<(keptarakLinkek[gal].length-1)){
				divj.onclick = function(){ shbox.loadContentImage(keptarakLinkek[gal][(index*1+1)].href,gal,index*1+1,keptarakLinkek[gal][(index*1+1)].subtext)};
			} else {
				divj.onclick = function(){ shbox.loadContentImage(keptarakLinkek[gal][0].href,gal,0,keptarakLinkek[gal][0].subtext)};
			}
		
		div.appendChild(divb);
		div.appendChild(divj);
		boxCellContent.appendChild(div);
		}
		
		$(boxCellContent).css({opacity:0.95});
		boxCellContent.appendChild(img);
		$(boxCellContent).animate({opacity:1.0},200);
		
		var boxLabelText = document.createElement("span");
		
		if (gal) {
			boxLabelText.innerHTML = (index*1+1) +"/" + keptarakLinkek[gal].length;
			boxLabel.appendChild(boxLabelText);
		}
		
		if (typeof(subtext)!="undefined"&&subtext!="null"){
			boxLabelText.innerHTML += "&nbsp;&nbsp;"+ subtext;
		}
		
		$(boxLabelText).css('opacity',0.5);
		$(box).css('opacity',opacityLevel-0.01);
		$(box).animate({opacity:opacityLevel},animateLength);
		$(boxLabelText).animate({'opacity':1.0},300);
		
		
	})

}

this.sh = function(settings){
	shbox.handleWindowResize();
	if (typeof(settings.type)  =="undefined"){ settings.type   = "img"; }
	if (typeof(settings.url)   =="undefined"){ settings.url    = "http://google.com"; }
	if (typeof(settings.sizeX) =="undefined"){ settings.sizeX  = ""; }
	if (typeof(settings.sizeY) =="undefined"){ settings.sizeY  = ""; }
	if (typeof(settings.gal) =="undefined"){ settings.gal  = ""; }
	if (typeof(settings.index) =="undefined"){ settings.index  = ""; }
	if (settings.type=="img") { shbox.loadContentImage(settings.url,settings.gal,settings.index,settings.subtext); } 
	if (settings.type=="iframe") { shbox.loadContentIframe(settings.url,settings.width,settings.height); } 
	
}

this.hatterDoboz = function(){

	preloadimages("js/shimages/sprite.gif","js/shimages/sprite2.gif","js/shimages/mover.jpg","js/shimages/nyilj.png","js/shimages/nyilb.png","js/shimages/nyilj.gif","js/shimages/nyilb.gif");

	if ($.browser.msie&&$.browser.version<7){
		styleSheetObj=document.styleSheets[0];
		styleSheetObj.addRule(".fixme","left: expression( ( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );");
		styleSheetObj.addRule(".fixme","top: expression( ( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );");
	}

    box = document.createElement("div");
	box.className ="fixme";
	box1 = box;
	box.style.zIndex = 100;
	box.style.left = "0px";
	box.style.background = "#000000";
	box.style.position = "absolute";
	box.style.display = "none";
	box.style.width = $(document).width()+"px";
	box.style.height = $(document).height()+"px";
	box.innerHTML =" "


    boxC = document.createElement("div");
	boxC.className ="fixme";
	box2 = boxC;
	boxC.style.display = "none";
	boxC.style.width = "100%";
	boxC.style.zIndex = 101;
	boxC.style.left = "0px";
	boxC.style.background = "transparent";

	if ($.browser.msie&&$.browser.version>=5.5&&$.browser.version<7){
		boxC.style.position = "absolute";
		boxC.style.height = $(window).height()-2+"px";
		box.style.width = $(document).width()+"px";
		box.style.height = $(window).height()+"px";
	} else {
		boxC.style.position = "fixed";
	}
	
	
	var boxTable = document.createElement("table");
	boxTable.width="100%";
	var boxTbody = document.createElement("tbody");
	var boxRow = document.createElement("tr");
	boxCelll = document.createElement("td");
	boxTable.appendChild(boxTbody);
	boxCelll.width= "100%";
	boxCelll.height= $(window).height();
	boxCelll.align= "center";
	boxTbody.appendChild(boxRow);
	boxRow.appendChild(boxCelll);
	
	
	boxCTable = document.createElement("table");
	boxCTable.id ="shboxTable";
    boxCTable.cellPadding="0"
    boxCTable.cellSpacing="0"
	
	
	//select tiltasa
	if (typeof boxCTable.onselectstart!="undefined") boxCTable.onselectstart=function(){return false}
	else if (typeof boxCTable.style.MozUserSelect!="undefined") 	boxCTable.style.MozUserSelect="none"
	else boxCTable.onmousedown=function(){return false}
	boxCTable.style.cursor = "default"
	
	
	boxCTable.className = "box";
	var boxCTbody = document.createElement("tbody");
	var boxCRow1 = document.createElement("tr");
	var boxCRow2 = document.createElement("tr");
	var boxCRow3 = document.createElement("tr");
	var boxCCell11 = document.createElement("td");
	boxCCell11.className = "shboxTopLeft";
	var boxCCell12 = document.createElement("td");
	boxCCell12.className = "shboxTopCenter";
	var muver = document.createElement("div");
	muver.className="shboxMover";
	boxCCell12.appendChild(muver);
	boxCCell12.style.cursor = "move";
	boxCCell12.align = "center";
	var boxCCell13 = document.createElement("td");
	boxCCell13.className = "shboxTopRight";
	boxCCell13.onmouseover = new Function("this.className = 'shboxTopRighthover'; ");
	boxCCell13.onmouseout  = new Function("this.className = 'shboxTopRight'; ");
	var boxCCell21 = document.createElement("td");
	boxCCell21.className = "shboxCenterLeft";
	boxCell = document.createElement("td");
	boxCellContent = document.createElement("div")
	boxCell.valign="center";
	boxCell.className = "shboxCenterCenter";
	boxCell.style.height = "100px";
	boxCell.style.width = "80px";
	boxCell.appendChild(boxCellContent);
	
	var boxCCell23 = document.createElement("td");
	boxCCell23.className = "shboxCenterRight";
	var boxCCell31 = document.createElement("td");
	boxCCell31.className = "shboxBottomLeft";
	var boxCCell32 = document.createElement("td");
	boxCCell32.className = "shboxBottomCenter";
	boxCCell32.align = "center";
	boxLabel = boxCCell32;
	var boxCCell33 = document.createElement("td");
	boxCCell33.className = "shboxBottomRight";

	boxCRow1.appendChild(boxCCell11);
	boxCRow1.appendChild(boxCCell12);
	boxCRow1.appendChild(boxCCell13);
	boxCRow2.appendChild(boxCCell21);
	boxCRow2.appendChild(boxCell);
	boxCRow2.appendChild(boxCCell23);
	boxCRow3.appendChild(boxCCell31);
	boxCRow3.appendChild(boxCCell32);
	boxCRow3.appendChild(boxCCell33);
	boxCTbody.appendChild(boxCRow1);
	boxCTbody.appendChild(boxCRow2);
	boxCTbody.appendChild(boxCRow3);
	boxCTable.appendChild(boxCTbody);
	
	boxCCell13.style.cursor = "pointer";
	boxCCell13.onclick = function(e){ shbox.fadeOut();shbox.stopEvent(e); }
	boxCTable.onclick = function(e){ shbox.stopEvent(e); }
	
	boxCTable.style.border = "0px";
	
	boxCell.padding = "0px";
	boxCell.margin = "0px";
	boxCell.valign = "middle";
	boxCell.align = "center";
	
	boxCelll.appendChild(boxCTable);
	
	boxC.appendChild(boxTable); 
	document.body.insertBefore(box,document.body.firstChild);
	document.body.insertBefore(boxC,document.body.firstChild);

	$(window).bind('resize', function() {   shbox.handleWindowResize(); });
	
	if(!($.browser.msie&&$.browser.version<7)) $(boxCTable).draggable({ opacity: 0.35,  revert: 'invalid' });

	boxCTable.onmouseover = function(e){
		if(((e && e.target)||(window.event && window.event.srcElement)).className=="shboxTopCenter"||((e && e.target)||(window.event && window.event.srcElement)).className=="shboxMover"){
			if(!($.browser.msie&&$.browser.version<7))  {
			   $(boxCTable).draggable('enable');
			} else {
				boxCCell12.style.cursor = "default";
			}
		} else {
			$(boxCTable).draggable('disable');
		}
	}
}

this.changeAnchors = function(){
	var ak = document.getElementsByTagName("a");
	for (var i=0;i<ak.length;i++){
		if(ak[i].getAttribute("rel")=="sh"){	
		if(ak[i].getAttribute("rev")){
			if (!keptarak[ak[i].getAttribute("rev")]) keptarak[ak[i].getAttribute("rev")]=0;
			if (typeof(keptarakLinkek[ak[i].getAttribute("rev")])=="undefined") keptarakLinkek[ak[i].getAttribute("rev")]= new Array();
			keptarakLinkek[ak[i].getAttribute("rev")][keptarak[ak[i].getAttribute("rev")]] = new Object();
			keptarakLinkek[ak[i].getAttribute("rev")][keptarak[ak[i].getAttribute("rev")]].href=ak[i].href;
			keptarakLinkek[ak[i].getAttribute("rev")][keptarak[ak[i].getAttribute("rev")]].subtext=ak[i].getAttribute("title");
			ak[i].onclick = new Function("shbox.sh({img:'img',url:'"+ak[i].href+"', gal:'"+ak[i].getAttribute("rev")+"' ,index :'"+keptarak[ak[i].getAttribute("rev")]+"' , subtext : '"+ak[i].getAttribute("title")+"' }); return false;" ) ; 
			//ak[i].href = "javascript:;"; // shbox.sh({img:'img',url:'"+ak[i].href+"', gal:'"+ak[i].getAttribute("rev")+"' ,index :'"+keptarak[ak[i].getAttribute("rev")]+"' });";
			ak[i].target = "_self";
			keptarak[ak[i].getAttribute("rev")]++;
		} else {
			ak[i].onclick = new Function("shbox.sh({img:'img',url:'"+ak[i].href+"' }); return false;" ) ; 
			//ak[i].href = "javascript:;";
			ak[i].target = "_self";
		}
		}
	}
}

this.handleWindowResize = function(){
	box.style.width = $(window).width()+"px";
	box.style.width = $(document).width()+"px";
	box.style.height = $(document).height()+"px";
	if ($.browser.msie&&$.browser.version>=5.5&&$.browser.version<7){
		boxC.style.position = "absolute";
		boxC.style.height = $(window).height()-2+"px";
		box.style.width = $(window).width()+"px";
		box.style.height = $(window).height()+"px";
	} else {
		boxC.style.position = "fixed";
	}
	
	

	if ($.browser.msie&&$.browser.version==8){
		box.style.width = ($(window).width()-10)+"px";
	}
	
	boxC.style.height= $(window).height()-10+"px";
	boxCelll.height= $(window).height();
	boxCelll.style.height= $(window).height()+"px";
	
}

this.stopEvent = function(e){	
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

this.init();
return this;
}

var shbox;
$(document).ready(function() {
shbox = new shBox();
});







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;
}

var myimages=new Array()

function preloadimages(){
	for (i=0;i<preloadimages.arguments.length;i++){
		myimages[i]=new Image()
		myimages[i].src=preloadimages.arguments[i]
	}
}
