﻿SoditajiPub.imageHandler = {
	image:'',
	imgUrl:'',
	t:'',
		
	startLoad : function() {
        objImage = new Image();     
		objImage.onLoad=this.showPic();     
		objImage.src=this.imgUrl;
    },
	startVideo : function() {
		 this.showVid();       
    },
    getAttrValue : function(attrName){
		return $(this.image).attr(attrName);
	},
    showVid : function(){
    var x = this.getScreenPropertie('width')-70;
    var y = this.getScreenPropertie('height')-100;
    	nIW = 600;
    	nIH = 500;

    	var vidId = this.getAttrValue('exVidId');
    	iH = Math.abs(nIH)+40;
    	var margin_top = ((y-(iH-20))/2)+15;
    	
    	var vidBodyHtml = "<table align='center' cellpadding='0' cellspacing='0' class='gal_table' style='margin-top:"+margin_top+"px;'>"+
		"<tr>"+
		"	<td class='s_fig_1'>&nbsp;</td>"+
		"	<td class='s_fig_8'>&nbsp;</td>"+
		"	<td class='s_fig_7'>&nbsp;</td>"+
		"</tr>"+
		"<tr>"+
		"	<td class='s_fig_2'>&nbsp;</td>"+
		"	<td align='justify' class='s_fig_9'><iframe style=\"visibility:visible;\" id=\"vid_box\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" height=\""+iH+"px\" width=\""+nIW+"px\" scrolling=\"no\" src=\"vid.php?v="+this.imgUrl+"&niw="+nIW+"&nih="+nIH+"&vid="+vidId+"\">"+
		"		Your browser does not support inline frames or is currently configured not to display inline frames."+
		"	</iframe></td>"+
		"	<td class='s_fig_6'>&nbsp;</td>"+
		"</tr>"+
		"<tr>"+
		"	<td class='s_fig_3'>&nbsp;</td>"+
		"	<td class='s_fig_4'>&nbsp;</td>"+
		"	<td class='s_fig_5'>&nbsp;</td>"+
		"</tr>"+
	"</table>";
    	$('#picBody').html(vidBodyHtml);
    },
    
	showPic : function(){
    var x = this.getScreenPropertie('width')-70;
    var y = this.getScreenPropertie('height')-100;
    var imageWidth = this.getAttrValue('tW');
    var imageHeight = this.getAttrValue('tH');
    	if (imageWidth > x) {
				imageHeight = Math.round(imageHeight * (x / imageWidth)); 
				imageWidth = Math.round(x); 
				if (imageHeight > y) { 
					imageWidth = Math.round(imageWidth * (y / imageHeight)); 
					imageHeight = Math.round(y); 
				}
			} else if (imageHeight > y) { 
				imageWidth = Math.round(imageWidth * (y / imageHeight)); 
				imageHeight = Math.round(y); 
				if (imageWidth > x) { 
					imageHeight = Math.round(imageHeight * (x / imageWidth)); 
					imageWidth = Math.round(x);
				}
			}
			$('body').keydown(function(e){ 	
				if (e == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = e.which;
				}
				if(keycode == 27){ // close
					alert('Close');
					this.closePicBox();
				}
			});
		
    	
    	if(imageWidth < 600){
    		nIW = 600;
    	} else {
    		nIW = Math.round(imageWidth);
    	}

		if(imageHeight < 500){
    		nIH = 500;
    	} else {
    		nIH = imageHeight;
    	}
    	var picId = this.getAttrValue('exPicId');
    	iH = parseInt(nIH)+40;
    	var margin_top = ((y-(iH-20))/2)+15;
    	
    	var picBodyHtml = "<table align='center' cellpadding='0' cellspacing='0' class='gal_table' style='margin-top:"+margin_top+"px;'>"+
		"<tr>"+
		"	<td class='s_fig_1'>&nbsp;</td>"+
		"	<td class='s_fig_8'>&nbsp;</td>"+
		"	<td class='s_fig_7'>&nbsp;</td>"+
		"</tr>"+
		"<tr>"+
		"	<td class='s_fig_2'>&nbsp;</td>"+
		"	<td align='justify' class='s_fig_9'><iframe style=\"visibility:visible;\" id=\"img_box\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" height=\""+iH+"px\" width=\""+nIW+"px\" scrolling=\"no\" src=\"pic.php?p="+this.imgUrl+"&h="+imageHeight+"&w="+imageWidth+"&niw="+nIW+"&nih="+nIH+"&pid="+picId+"\">"+
		"		Your browser does not support inline frames or is currently configured not to display inline frames."+
		"	</iframe></td>"+
		"	<td class='s_fig_6'>&nbsp;</td>"+
		"</tr>"+
		"<tr>"+
		"	<td class='s_fig_3'>&nbsp;</td>"+
		"	<td class='s_fig_4'>&nbsp;</td>"+
		"	<td class='s_fig_5'>&nbsp;</td>"+
		"</tr>"+
	"</table>";
    	$('#picBody').html(picBodyHtml);
    	this.showCloseMsgr();
    },
    
    showCloseMsgr : function(){
    	var margin_top = (this.getScreenPropertie('height') - 38)/2;
    	$("#picBody").append("<div id='close_msg' onclick='$(\"#close_msg\").hide();'><img src='images/gal_img_close.png' style='margin-top:"+margin_top+"px;'/></div>");
    	setTimeout("$('#close_msg').fadeOut(3000);",3000);
    },
    
    getScreenPropertie : function (need){
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
		    myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    		//IE 4 compatible
    		myWidth = document.body.clientWidth;
    		myHeight = document.body.clientHeight;
		}
		if(need == 'height'){
		  	return  myHeight;
		} else if (need == 'width'){
		  	return myWidth;
		}
	},

    createDiv : function(){
    var margin_top = (this.getScreenPropertie('height') - 19)/2;
    $("body").append("<div id='picBody' onclick='SoditajiPub.imageHandler.closePicBox();'><img src='images/loader_5.gif' style='margin-top:"+margin_top+"px;'/></div>");
    if(this.t == 'img'){
    	this.startLoad();
    } else {
    	this.startVideo();
    }
    },
    
    toggelGalBody : function(img, type){
    	this.imgUrl = img.href;
    	this.image = img;
    	this.t = type;
    	this.createDiv();
  	},
  	
  	closePicBox : function (){
  		$('#close_msg').remove();
  		$('#picBody').html('');
  		$('#picBody').remove();
  	},
  	openComments : function(x,y){
  			$('#galContentTable').animate({"left":"-"+x+"px"}, 3000);
  	},
  	openPicture : function(x,y){
			var h = this.getScreenPropertie('height')-100;
			var margin_top = ((h-y)/2)+15;
			var width = x+35;
			var height = y+35;
			$('#backToPic').html('&nbsp;');
			$('#backToPic').css({'display':'none'});
			alert("H : "+height+" || W : "+width)
			$('.gal_table').animate({"width":width+"px","height":height+"px","marginTop": margin_top+"px"}, 3000);
  			$('#galContTable').animate({"left":"0px"}, 3000);

  	}
}

