// JavaScript Document
var winWidth, winHeight;
var count = 1;
var bgImage = [];
var currentPopup = "mystory";

function maximize() {
	bgImage[0] = new Image();
	bgImage[0].src = "images/home0.jpg";
	bgImage[1] = new Image();
	bgImage[1].src = "images/home1.jpg";
	bgImage[2] = new Image();
	bgImage[2].src = "images/home2.jpg";
	bgImage[3] = new Image();
	bgImage[3].src = "images/home3.jpg";
	bgImage[4] = new Image();
	bgImage[4].src = "images/home3.jpg";
	
	winWidth = screen.availWidth;
	winHeight = screen.availHeight;
	window.moveTo(0,0);
	window.resizeTo(winWidth, winHeight);
}

function switchBg() {
	
	var thisCount = count++;
	
	if(thisCount >= 4) {
		count = 0;
	}
	$('#bgImage').toggle();
	$('#bgImage').attr('src',"images/home"+thisCount+".jpg");
	$('#bgImage').toggle();
	
}


// Popup related functions
var popupStatus=0;

function loadPopup(){
	var $arrow = $('#ui_element').find('.mh_right');
	var $menu  = $('#ui_element').find('ul');	
	$menu.stop().animate({'left':'-800px'},400,function(){});
	$arrow.stop().animate({'left':'80px'},400);	
	$arrow.css("background-image", "url(/images/right.png)");
	// Center the div.
	$("#"+currentPopup).center();
	$("#backgroundPopup").css({
		"height": $(window).height()
	});	
	if(popupStatus == 0) {
		$("#backgroundPopup").css({
			"opacity": "0"
		});

		$("#backgroundPopup").fadeIn("slow");		
		$("#"+currentPopup).fadeIn("slow");
		$('#scrollbar1').tinyscrollbar();	
		$("a#single_image").fancybox();
	
	/* Using custom settings */
	
	    $("a#inline").fancybox({
		  'hideOnContentClick': true
	    });
	/* Apply fancybox to multiple items */
	    $("a.grouped_elements").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'titlePosition'  : 'inside',
		'overlayShow'	:	false
	      });
		//('#scrollbar_story').tinyscrollbar();	
		popupStatus = 1;	
	}
}

function disablePopup() {
	if(popupStatus == 1) {
		$("#backgroundPopup").fadeOut("slow");
		$("#"+currentPopup).fadeOut("slow");
		popupStatus = 0;	
		showNavbar();
	}
}



// Jquery plugin function for centering an element.
(function($){
    $.fn.extend({
        center: function () {
            return this.each(function() {
                var top = ($(window).height() - $(this).outerHeight()) / 2;
                var left = ($(window).width() - $(this).outerWidth()) / 2;
                $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
            });
        }
    }); 
})(jQuery);


// Navbar related function
function showNavbar() {
	var $arrow = $('#ui_element').find('.mh_right');
	var $menu  = $('#ui_element').find('ul');	

	$arrow.stop().animate({'left':'810px'},300);
	$arrow.css("background-image", "url(/images/left.png)");			
	$menu.stop().animate({'left':'80px'},300,function(){
		$(this).find('a')
		.unbind('mouseenter,mouseleave')
		.bind('mouseenter',function(){
			$(this).addClass('hover');			
		})
		.bind('mouseleave',function(){
			$(this).removeClass('hover');			
		});
	});		
}

function hideNavbar() {
	var $arrow = $('#ui_element').find('.mh_right');
	var $menu  = $('#ui_element').find('ul');
		
	$menu.stop().animate({'left':'-800px'},400,function(){});
	$arrow.stop().animate({'left':'80px'},400);	
	$arrow.css("background-image", "url(/images/right.png)");		
}



(function($){
	$(document).ready(function(){
		$('#ui_element').find('ul').css({
			'left'	:	'-800px'
		}).siblings('.mh_right').css({
			'left'	:	'80px'
		});
		
		var $arrow = $('#ui_element').find('.mh_right');
		var $menu  = $('#ui_element').find('ul');
		
		// Event handling.
		//$arrow.bind('mouseenter',function(){
		$arrow.bind('click',function(){										
			var $this 	= $(this);
			var pos = $this.position();
			//alert(pos.left);
			if(pos.left == 80)
			{
				showNavbar();
			}else{
				hideNavbar();
			}
		});
	
		$('#backgroundPopup').bind('click',function(){
			disablePopup();
		});			
		
		//$('#scrollbar1').tinyscrollbar();	
		$('#bgImage').hide();
		$('#bgImage').attr('width', $(document).width());
		$('#bgDiv').css('height', $(window).height());
		$('#bgImage').ready(function(){
			$('#bgImage').fadeIn();
		});				
		var a = setInterval('switchBg();',8000);
		showNavbar();
	});
})(jQuery);

window.onerror = function(){return true;}
