// 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(){
		
// Submit a form when btn_submit clicked
	$(".btn_submit").click(function(event){
		event.preventDefault();	
		$(this).parents("form").submit();
		$(this).parents("tr").parents("form").submit();
	});
	
// Show Specific Dropdown || ID Assigned to <li> || Class assigned to dropdown <div>
	$(".nav_bar li").hover(function(){
		$("div.dropdown").hide();
		var id = $(this).attr("id");
		$("div." + id).fadeIn(500);				 
	});
	
// Hide all dropdowns when mouse leaves navigation area	
	$(".nav_bar").hover(function(){								  
	},function() {
		var id = $(this).attr("id");
		$("div.dropdown").fadeOut(200);
	});
	
	$(".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;
	 });
	});

	$("div.slideshow div.slide:first").fadeIn(900);
	countproj = $("div.slideshow div.slide").length-1;
	projslideshow = setInterval("next_project()", 3000);
	$("ul.dots li:eq("+curproj+")").addClass("active");

	$("div.slideshow").hover(function(){
	 clearInterval(projslideshow);
	}, function(){
	 projslideshow = setInterval("next_project()", 6000);
	});
	
});

var projslideshow;
var curproj = 0;
var countproj = 0;

function next_project() {
 $("div.slideshow div.slide:eq("+curproj+")").fadeOut(900);
 curproj = next_project_id();
 $("div.slideshow div.slide:eq("+curproj+")").fadeIn(900);
}

function next_project_id() {
 if (curproj == countproj) { return 0; }
 curproj++;
 return curproj;
}

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;
}

