
// JavaScript (JQuery) Document

//dimScreen() from http://docs.jquery.com/Plugins/dimScreen#Example
//by Brandon Goldman
jQuery.extend({
    //dims the screen
    dimScreen: function(speed, opacity, callback) {
        if(jQuery('#__dimScreen').size() > 0) return;
        
        if(typeof speed == 'function') {
            callback = speed;
            speed = null;
        }

        if(typeof opacity == 'function') {
            callback = opacity;
            opacity = null;
        }

        if(speed < 1) {
            var placeholder = opacity;
            opacity = speed;
            speed = placeholder;
        }
        
        if(opacity >= 1) {
            var placeholder = speed;
            speed = opacity;
            opacity = placeholder;
        }

        speed = (speed > 0) ? speed : 500;
        opacity = (opacity > 0) ? opacity : 0.5;
        return jQuery('<div></div>').attr({
                id: '__dimScreen'
                ,fade_opacity: opacity
                ,speed: speed
            }).css({
            background: '#000'
            ,height: '100%'
            ,left: '0px'
            ,opacity: 0
            ,position: 'absolute'
            ,top: getScrollTop() + 'px'
            ,width: '100%'
            ,zIndex: 999
        }).appendTo(document.body).fadeTo(speed, opacity, callback);
    },
    
    //stops current dimming of the screen
    dimScreenStop: function(callback) {
        var x = jQuery('#__dimScreen');
        var opacity = x.attr('fade_opacity');
        var speed = x.attr('speed');
        x.fadeOut(speed, function() {
            x.remove();
            if(typeof callback == 'function') callback();
        });
    }
});

$("document").ready(function(){

	$("ul.subs").hide();
	//$("ul#navigation li#home ul.subs").remove();

	$("ul#navigation li.tab").hover(function(){
	 $("ul.subs:visible").hide();
	 var subnavid = $(this).attr("id");
	 $("ul.subs#" + subnavid + "_subnav").show();
	});

	$("ul.subs").hover(function(){
	 } , function() {
	 $(this).hide();
	});
		
// Submit a form when btn_submit clicked
	$(".btn_submit").click(function(event){
		event.preventDefault();	
		$(this).parents("form").submit();
	});
	
	$(".btn_close").click(function(event){
		event.preventDefault();
		$(this).parent("div").fadeOut(500);
		$.dimScreenStop();
	});

	$(".enlarge_img").click(function(event){
		event.preventDefault();
		var imgsrc = $(this).attr("value");
		newImg.src = imgsrc;
		
		var imgboxtopinitial = getScrollTop() + getWindowHeight();
		$(".imgbox img").hide();
		$(".imgbox img").attr("src", imgsrc);
		$(".imgbox").css({
			top: imgboxtopinitial+"px",
			width: '200px',
			height: '50px',
			marginLeft: "-100px"
		});
		$.dimScreen(500, 0.7, function() {
			$(".imgbox").fadeIn(500);
			setTimeout("expand_img()", 2000);
		});
		
	});

	$("select#listby").change(function(){
	 var url = window.location.url;
	 $.post(url, { listby : $(this).val() }, function(){
	  window.location = window.location;
	 });
	});

	$("select#displaycount").change(function(){
	 var url = window.location.url;
	 $.post(url, { displaycount : $(this).val() }, function(){
	  window.location = window.location;
	 });
	});
	

});


var newImg = new Image();

function expand_img() {
	var h = newImg.height+120;
	var w = newImg.width+120;
	var imgsrc = newImg.src;
	var imgboxtop = getScrollTop()-(h/2) + getWindowHeight(); //-(h/2);
	var imgboxleft = 0-(w/2);
	
	//alert(getWindowHeight());
	
		//alert(imgboxtop);

		$(".imgbox").animate({
			height: h+"px",
			top: imgboxtop+"px",
			width: w+"px",
			marginLeft: imgboxleft+"px"
		}, 500, function() {	
			$(".imgbox img").fadeIn(500);
		});					 

}

function getScrollTop() {

var ScrollTop = document.body.scrollTop;

if (ScrollTop == 0)
{

    if (window.pageYOffset) {
        ScrollTop = window.pageYOffset;
	}
    else
	{
        ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}

}
else
{
	
}

return ScrollTop;
}

function getWindowHeight() {
 
      var x = 0;

        if (self.innerHeight)
        {
                x = self.innerWidth;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                x = document.documentElement.clientWidth;
        }
        else if (document.body)
        {
                x = document.body.clientWidth;
        }
        return x/4;
}

