$(function(){

    var searchForm=$("div.gallery-search form");
    var searchInput=$("div.gallery-search input");
    var searchBt=$("div.gallery-search a");
    var doSearch=function(){
        window.location.href=searchForm.attr("action")+"?keyword="+searchInput.val();
    };
    
    searchInput.keydown(function(e){
        if(e.keyCode==13){
            doSearch();
            return false;
        }
        return true;
    });
    
    searchBt.click(function(){
        doSearch();
        return false;
    });

    var gview=$("#gallery-view");
    var gviewimg=$("#gallery-view-image");
    var c=0;
    var active=null;
    gviewimg.append($("h1",gviewimg).clone(true));
	gviewimg.append($("b",gviewimg).clone(true));
	gviewimg.append($("img",gviewimg).clone(true));
	
	$("h1:eq("+c+"),b:eq("+c+"),img:eq("+c+")",gview).hide();
	
    $("#gallery-popup div.scrollable").scrollable({
		size: 3,
		items: 'ul'
	});	
	if($("#gallery-popup div.scrollable ul li").length<=3)
	    $("#gallery-popup>a").addClass("disabled");
	$("#gallery-popup div.scrollable ul li>a").each(function(){
	    var href="javascript:void('"+$(this).attr("href")+"');";
	    $(this).attr("href",href);
	});
	var xfunc=function(){
	    active=$(this).parent();
		
	    $("h1:eq("+c+")",gview).html($(this).attr("title"));
	    $("b:eq("+c+")",gview).html($("img",this).attr("title"));
	    var imgurl=$(this).attr("href").replace("javascript:void('","").replace("');","");
	    $("img:eq("+c+")",gview).css("background-image","url('"+imgurl+"')");
        
        
	    var hi=$("dt",active).html();
	    var low=$("dd",active).html();
	    var hiEl=$("em.hi-res:eq("+c+")",gviewimg).parent();
	    var lowEl=$("em.low-res:eq("+c+")",gviewimg).parent();

	    if($.trim(hi)!=""){
	        hiEl.show();
	        hiEl.attr("href","http://pressroomcms.ps.co.th/cms/contentmgt/downloadfile.rails?src="+hi);
	    }else{
	        hiEl.hide();
	    }
	     
	    if($.trim(low)!=""){
	        lowEl.show();
	        lowEl.attr("href","http://pressroomcms.ps.co.th/cms/contentmgt/downloadfile.rails?src="+low);
	    }else{
	        lowEl.hide();
	    }
	    
	    $("h1:eq("+c+"),b:eq("+c+"),img:eq("+c+")",gview).fadeIn(1000);
	    c=1-c;
	    $("h1:eq("+c+"),b:eq("+c+"),img:eq("+c+")",gview).fadeOut(1000);
	    
	    $("a.prev",gview).css("visibility",(active.prev().length>0)?"visible":"hidden");
	    $("a.next",gview).css("visibility",(active.next().length>0)?"visible":"hidden");
	    return true;
	};
	$("#gallery-popup div.scrollable ul li>a").click(xfunc).mouseover(xfunc);
	$("a.prev",gview).click(function(){
	    if(active){
	        if(active.prev().length>0)
	            $("a",active.prev()).click();
	    }
	    return false;
	});
	$("a.next",gview).click(function(){
	    if(active){
	        if(active.next().length>0)
	            $("a",active.next()).click();
	    }
	    return false;	
	});
	$("#gallery-popup div.scrollable ul li:first>a").click();
});