// JavaScript Document
$(document).ready(function(){
	 try {
	fancybox();
	} catch (e) {}
    if($('.clickImg').is('a')){
        $('.clickImg').click(function(){
          popupImage($(this).attr('value'));
          return false;
        })
        
      }
	 if($('.clickImg2').is('a')){
        $('.clickImg2').click(function(){
          popupImage($(this).attr('value'));
          return false;
        })
        
      }
	roArt();
	//<img src="images/fpress-img.gif">
	$('.qdf').mouseover(function(){
		$('#showimg').html('<img src="'+$(this).attr('value')+'">');
	});
	$('.qdf').mouseout(function(){
		//$('#showimg').html('');
	});
	
	$('.cilImg').click(function(){
	//alert($(this).prev("strong") .attr('value'));
		$('#popup').remove();
		$('body').append('<div id="popup"><img  src="'+$(this).prev("strong") .attr('value')+'"></div>');
		//alert(document.body.clientWidth);
		//alert(document.body.offsetWidth);

		$('#popup').css({'position':'absolute','top':0,'left':0,'height':'100%','width':'100%','background':'#fff'});
		$('#popup').find('img').load(function(){
			var imgH=$(this).height();
			var imgW=$(this).width();
			var webW=parseInt(document.body.clientWidth);
			var webH=parseInt(document.body.clientHeight);
			var pleft;
			var ptop;
			if(imgW>webW){ 
				pleft='0px';
				$('body').css({'width':imgW+'px'});
				$('#popup').css({'width':imgW+'px'});
			}else {pleft=(webW-imgW)/2+'px';}
			if(imgH>webH){ 
				ptop='0px';
				$('body').css({'height':imgH+'px'});
				$('#popup').css({'height':imgH+'px'});
			}else{ ptop=(webH-imgH)/2+'px';}
			var pp=ptop+" 0 0 "+pleft;
			$('#popup').find('img').css({'position':'static','border':'1px solid #666','margin':pp});
		});
		
		
		
		
		//$('#popup').css({'padding':pp});
		$('#popup').click(function(){$(this).hide();$('body').css({'height':'','width':''});});
	});
	
	if($('#sImg').offset()) $('#lImg').css({'top':($('#sImg').offset().top-135)+'px'});
	if($.browser.msie){
		if(($.browser.version)=='6.0'){
			if($('#sImg').offset()) $('#lImg').css({'top':($('#sImg').offset().top-139)+'px'});
		}
		
	}else{
		if($('#sImg').offset())
		$('#lImg').css({'top':$('#sImg').offset().top+'px','left':'550px'});
	}
	if($('#sImg').height()<400) {
		$('#minH').height(400-$('#sImg').height());
													
	}else $('#minH').height(0);
	//alert();//293 325 182
	var dfH=0;
	$('#sImg > a').mouseover(function(){
		//if($.browser.msie)	var dfH=182;
		//else var dfH=312;
		$('#lImg').find('img').attr('src',$(this).attr('value'));
		if(dfH==0) dfH=parseInt($('#lImg').css('top'));
		
		var ind=$('#sImg > a').index(this)+1;
		if(ind%2>0)
			ind=Math.floor(ind/2)+1;
		else
			ind=Math.floor(ind/2);
		ind -=1;
		indH=dfH+ind*98;
		
		var limgH=$('#lImg').height()+40;
		var okH=$('#sImg').height()-12;
		var ah=$('#minH').height()+$('#sImg').height();
		if(limgH>ah){
			$('#minH').height(limgH-$('#sImg').height());
		}else if($('#minH').height()>0){
			$('#minH').height(limgH-$('#sImg').height());
		}
		//if($('#minH').height()<($('#lImg').height()+40)) $('#minH').height($('#lImg').height()+40);
		//var limgTop=($('#lImg').offset().top-143);
		//$('#lImg').css({'top':indH+'px'});
		
		$('#lImg').stop();
		if((limgH+indH-dfH)<okH){
			$('#lImg').animate({top: indH+'px'}, 500);
		}else if(okH-limgH>0)
			$('#lImg').animate({top: (okH-limgH+dfH)+'px'}, 500);
		else
			$('#lImg').animate({top: dfH+'px'}, 10);
		//Math.floor(ind/2)//316-173 = 143 414
		//alert($(this).offset().top );
	});
	
	$('#lImg').find('img').load(function(){
		var bh=$('#lImg').height()+40;
		var ah=$('#minH').height()+$('#sImg').height();
		if(bh>ah)
			$('#minH').height(bh-$('#sImg').height());
	});
	 
});

      function popupImage(img){
        var mstr = "<HTML><HEAD><LINK REL='stylesheet' TYPE='text/css' HREF='/common.css?'><script language='javascript' type='text/javascript' src='/js/jquery.js'></script><script language='javascript' type='text/javascript' src='/js/init.js'></script></HEAD><BODY><CENTER><TABLE>";
          mstr+= "<TR><TD ALIGN=CENTER><A CLASS=redlnk HREF='javascript:window.close()' style='color:#d20010; font-size:11px;'>Close</A></TD></TR>";
          mstr+= "<TR><TD ALIGN=CENTER HEIGHT=10>&nbsp;</TD></TR>";
          mstr+= "<TR><TD ALIGN=CENTER><IMG SRC='"+img+"' BORDER=0 ></TD></TR>";
          mstr+= "</TABLE></CENTER></BODY></HTML>";
                      if ((navigator.appName=="Netscape")&& (parseInt(navigator.appVersion)<=4)) {
                              winwidth = screen.availWidth;
                              winheight = screen.availHeight
                   }
          msg=window.open("","","top=0,left=0,scrollbars=yes,resizable=yes");
          msg.document.write(mstr);
          return false;
        }
		
	
	function roArt(){
		if($('#imgListBox').find('a').length>15){
			np();
		}else{
			$('#np').hide();
		}
		$('#imgListBox').find('a').click(function(){
			$('#imgListBox').find('a').removeClass('sel');
			$(this).addClass('sel');
			showInfoD($('#imgListBox').find('a').index(this));
			return false;
		});
		if($('#imgListBox').find('a').length) showInfoD(0);
	}
	
	function showInfoD(index){
		$('#art_img').html('&nbsp;');
		$('#art_name').hide();
		$('#art_title').hide();
		$('#art_year').hide();
		$('#art_dim').hide();
		$('#art_med').hide();
		$('#art_pri').hide();
		var d=data[index];
		$('#art_img').html('<img src="'+d[5]+'"/>');
		if(d[0]){
			$('#art_name').html('<strong>artist : </strong>'+URLdecode(d[0]));
			$('#art_name').show();
		}
		if(d[1]){
			$('#art_title').html('<strong>title of work : </strong>'+URLdecode(d[1]));
			$('#art_title').show();
		}
		if(d[2]){
			$('#art_year').html('<strong>year : </strong>'+URLdecode(d[2]));
			$('#art_year').show();
		}
		if(d[3]){
			$('#art_dim').html('<strong>dimension : </strong>'+URLdecode(d[3]));
			$('#art_dim').show();
		}
		if(d[4]){
			$('#art_med').html('<strong>medium : </strong>'+URLdecode(d[4]));
			$('#art_med').show();
		}
		if(d[6]){
			$('#art_pri').html('<strong>price : </strong>HKD'+URLdecode(d[6]));
			$('#art_pri').show();
		}
	}
	
	function np(){
		var l=parseInt(($('#imgListBox').find('a').length-15)/3);
		if((l*3+15)<$('#imgListBox').find('a').length) l++;
		var top_=0-(l*93);
		$('#np_next').click(function(){
			if(parseInt($('#imgListBox').css('top'))>top_ && $('#np_next').attr('value')!=1){
				$('#np_next').attr('value',1);
				$('#np_pre').attr('value',1);
				var q=(parseInt($('#imgListBox').css('top'))-93)+'px';
				$("#imgListBox").animate({ "top": q }, 500,'linear',function(){$('#np_next').removeAttr('value');$('#np_pre').removeAttr('value');}); 
			}
			return false;
		});
		
		$('#np_pre').click(function(){
			if(parseInt($('#imgListBox').css('top'))<0 && $('#np_pre').attr('value')!=1){
				$('#np_next').attr('value',1);
				$('#np_pre').attr('value',1);
				var q=(parseInt($('#imgListBox').css('top'))+93)+'px';
				$("#imgListBox").animate({ "top": q }, 500,'linear',function(){$('#np_next').removeAttr('value');$('#np_pre').removeAttr('value');}); 
			}
			return false;
		});
	}

function URLdecode(str) {
    var ret = "";
    for(var i=0;i<str.length;i++) {
        var chr = str.charAt(i);
        if(chr == "+") {
            ret += " ";
        }else if(chr=="%") {
            var asc = str.substring(i+1,i+3);
            if(parseInt("0x"+asc)>0x7f) {
                ret += decodeURI("%"+ str.substring(i+1,i+9));
                i += 8;
            }else {
                ret += String.fromCharCode(parseInt("0x"+asc));
                i += 2;
            }
        }else {
            ret += chr;
        }
    }
    return ret;
}

function fancybox(){
	$(".fancybox").fancybox({
		'width'				:  560,
		'height'			:  394,
		'transitionIn'	: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe',
		'padding':0,
		'margin':0,
		'showCloseButton':false,
		'centerOnScroll':true
	});
}
