$j = jQuery;
$j.fn.hcenter = function () {
    this.css("position","absolute");
   // this.css("top", (($j(window).height() - this.outerHeight()) / 2) + $j(window).scrollTop() + "px");
    this.css("left", (($j(window).width() - this.outerWidth()) / 2) + $j(window).scrollLeft() + "px");
    return this;
}
$j.fn.vcenter = function () {
    this.css("position","absolute");
   this.css("top", (($j(window).height() - this.outerHeight()) / 2) + $j(window).scrollTop() + "px");
    //this.css("left", (($j(window).width() - this.outerWidth()) / 2) + $j(window).scrollLeft() + "px");
    return this;
}
/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}


$j(document).ready(function() {
	$j("#body-background").ezBgResize();
	$j(".arrownavigation").appendTo( "body" );
	screenAdjustment();
	sleekMenuInit();
});	
$j(window).resize(function() {
	screenAdjustment();
	sleekMenuInit();
});
function screenAdjustment(){
	$j("#footer").hcenter();
	$j("#container").hcenter();
	// $j(".arrownavigation").appendTo( "body" );	
	$j(".arrownavigation").vcenter();
	$j(".arrownavigation").hcenter();
}
function sleekMenuInit(){
	// default position when page is reloaded  for main menu
	//alert( 'testing' );
	offset=$j("#mainmenu li.active").offset();
	if( null != offset ){
		offset=$j("#mainmenu li.active").offset();
		var thiswidth =$j("#mainmenu li.active").width()+5;
		$j('#mainmenu li.bg').show().css({left:offset.left - 5+"px",width:thiswidth+"px"});    
	} 
	// default position when page is reloaded for gallery menu
	offset=$j("#gallery-menu li.active").offset();
	if( null != offset ){
		thiswidth =$j("#gallery-menu li.active").width()+5;
		$j('#gallery-menu li.bg').show().css({left:offset.left - 5+"px",width:thiswidth+"px"},600);    
	}
// sleek menu
	$j('#mainmenu li a').hover(function()
		{
			var offset=$j(this).offset();
			var thiswidth =$j(this).width()+10;
			$j('#mainmenu li.bg').show().stop().animate({left:offset.left - 20+"px",width:thiswidth+"px"},600); 
			$j('#gallery-menu li.bg').hide();
		});
		$j('#gallery-menu li a').hover(function()
		{
			var offset=$j(this).offset();
			var thiswidth =$j(this).width()+10;
			$j('#gallery-menu li.bg').show().stop().animate({left:offset.left - 20+"px",width:thiswidth+"px"},600);    
			$j('#mainmenu li.bg').hide();
		});
		/*function()
		{
			if( undefined == currentHeaderMenuId){
				var offset = $j("#first_sleek_menu1").offset();
				var thiswidth = $j("#first_sleek_menu1").width()+15;
				$j('#topsleekmenu1 li.bg').stop().animate({left:offset.left+"px",width:thiswidth+"px"},600);   
			} else {
				var offset= $j(currentHeaderMenuId).offset();
				var thiswidth = $j(currentHeaderMenuId).width()+15;
				$j('#topsleekmenu1 li.bg').stop().animate({left:offset.left+"px",width:thiswidth+"px"},600);   
			}
		});*/
}


// resize bg image 
var center = false;
    function resizeBgImage() {
        $("body").css({
            "overflow-x":"auto"
        });
		 containerObj = $j("#body-background");

        containerObj.css("visibility","hidden");
        containerObj.css({
            "position":"fixed",
            "top":"0px",
            "left":"0px",
            "z-index":"-1",
            "overflow":"hidden",
            "width":getWindowWidth() + "px",
            "height":getWindowHeight() + "px"
        });

        // Resize the img object to the proper ratio of the window.
        var iw = containerObj.children('img').width();
        var ih = containerObj.children('img').height();
        
        if ($(window).width() > $(window).height()) {
            //console.log(iw, ih);
            if (iw > ih) {
                var fRatio = iw/ih;
                containerObj.children('img').css("width",$(window).width() + "px");
                containerObj.children('img').css("height",Math.round($(window).width() * (1/fRatio)));

                var newIh = Math.round($(window).width() * (1/fRatio));

                if(newIh < $(window).height()) {
                    var fRatio = ih/iw;
                    containerObj.children('img').css("height",$(window).height());
                    containerObj.children('img').css("width",Math.round($(window).height() * (1/fRatio)));
                }
            } else {
                var fRatio = ih/iw;
                containerObj.children('img').css("height",$(window).height());
                containerObj.children('img').css("width",Math.round($(window).height() * (1/fRatio)));
            }
        } else {
            var fRatio = ih/iw;
            containerObj.children('img').css("height",$(window).height());
            containerObj.children('img').css("width",Math.round($(window).height() * (1/fRatio)));
        }

        containerObj.css("visibility","visible");

        // Center BG Image
        if (center) {
            containerObj.children('img').css("position","relative");

            if (containerObj.children('img').width() > containerObj.width()) {
                var wDiff = (containerObj.children('img').width() - containerObj.width()) / 2;
                containerObj.children('img').css("left", "-" + wDiff + "px");
            }
        }
    }

    // Dependable function to get Window Height
    function getWindowHeight() {
        var windowHeight = 0;
        if (typeof(window.innerHeight) == 'number') {
            windowHeight = window.innerHeight;
        }
        else {
            if (document.documentElement && document.documentElement.clientHeight) {
                windowHeight = document.documentElement.clientHeight;
            }
            else {
                if (document.body && document.body.clientHeight) {
                    windowHeight = document.body.clientHeight;
                }
            }
        }
        return windowHeight;
    };

    // Dependable function to get Window Width
    function getWindowWidth() {
        var windowWidth = 0;
        if (typeof(window.innerWidth) == 'number') {
            windowWidth = window.innerWidth;
        }
        else {
            if (document.documentElement && document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth;
            }
            else {
                if (document.body && document.body.clientWidth) {
                    windowWidth = document.body.clientWidth;
                }
            }
        }
        return windowWidth;
    };
