$(document).ready(function(){
		$(function(){
			var ht = screen.height,
				wd = screen.width,
				cssObj = {
							'height' : ht*3,
							'width' : wd*3 + 5
						};
		$("#background").css(cssObj);
		slideShow(4000);
		$(function() {
			//single book
			$('#mybook').booklet({
				width:  544,
        		height: 370,
				speed: 150,
				pagePadding: 0,
				closed: true,
				autoCenter: true,
				keyboard: true,
				tabs: true,
				manual: false,
				overlays: true,
				cursor: 'crosshair' 
			});
		});
		$.scrollTo({ top:'0px',left:wd + 'px' },0,'easeOutQuad');
		$("#slink0").addClass('active');
		$(function(){
			$('#js-news').ticker();
		});
		$(function() {
							$('#gallery1 a').lightBox();
						})
	    $(function(){
				var $mainMenu         = $("#menu"),
				$mainMenuItems		  = $mainMenu.children('ul').children('li'), 
				$menu1 				  = $('#mb_menu1'),
				$menu2				  = $('#mb_menu2'),
			    $extMenu			  = $('#extLinks'),
				$eveMenu              = $('#events_menu'),
				$menuItems1			  = $menu1.children('.sub_menu').children('li'),
				$menuItems2			  = $menu2.children('.sub_menu').children('li'),
				$extMenuItems         = $extMenu.children('ul').children('li'),
				$eveMenuItems         = $eveMenu.children('li'),
				$mbWrapper1			  = $('#mb_content_wrapper_1'),
				$mbWrapper2			  = $('#mb_content_wrapper_2'),
				$mbWrapper3		      = $('#mb_content_wrapper_3'),
				$mbClose1			  = $mbWrapper1.children('.mb_close'),
				$mbClose2			  = $mbWrapper2.children('.mb_close'),
				$mbClose3			  = $mbWrapper3.children('.mb_close'),
				$mbContentItems1	  = $mbWrapper1.children('.mb_content'),
				$mbContentItems2	  = $mbWrapper2.children('.mb_content'),
				$mbContentItems3	  = $mbWrapper3.children('.mb_content'),
				$mbContentHeadings1   = $mbContentItems1.children('h2'),
				$mbContentHeadings2   = $mbContentItems2.children('h2'),
				$mbContentHeadings3   = $mbContentItems3.children('h2'),
				$mbContentInnerItems1 = $mbContentItems1.children('.mb_content_inner'),
				$mbContentInnerItems2 = $mbContentItems2.children('.mb_content_inner'),
				$mbContentInnerItems3 = $mbContentItems3.children('.mb_content_inner'),
				$mbPattern			  = $('#mb_pattern'),
				$subMenuCur			  = $('#abt_menu'),
				$eveBack              = $('#back'),
				$backVis              = false,
				$codaPresent          = false,
				$curCont,
				$curSec				  = $("#slink0"),
				$abtLink			  = $mbContentInnerItems2.children(".contMenu").children("#abt"),
				$rulLink			  = $mbContentInnerItems2.children(".contMenu").children("#rul"),
				$contLink			  = $mbContentInnerItems2.children(".contMenu").children("#cont"),
				Menu		 		  = (function(){
					var init 		  = function() {
						initPattern();
						initEventsHandler();
					},
					/*draws 16 boxes on a specific area of the page. we randomly calculate the top, left, and rotation angle for each one of them*/
					initPattern		= function() {
						for(var i = 0; i < 45 ; ++i) {
							//random opacity, top, left and angle
							var o		= 0,
							    t		= Math.floor(Math.random()*796) + 5, // between 5 and 200
							    l		= Math.floor(Math.random()*1296) + 5, // between 5 and 700
							    a		= Math.floor(Math.random()*701) + 50; // between -50 and 50	
							    $el		= $('<div>').css({
								      opacity	: o,
								      top		: t + 'px',
								      left		: l + 'px'
							});
							switch(i)
							{
								case 0 : $el.addClass("firstcor"); break;
								case 8 : $el.addClass("seccor"); break;
								case 36: $el.addClass("thrcor"); break;
								case 44: $el.addClass("forcor"); break;
							}	
							if (!$.browser.msie) $el.transform({'rotate': a + 'deg'});
							$el.appendTo($mbPattern);
						}
					},
					/*when the User closes a content item, we move the boxes back to the original place,with new random values for top, left and angle though*/
					disperse 		= function(){
						$mbPattern.children().each(function(i){
							//random opacity, top, left and angle
							var o		= 0,
							t			= Math.floor(Math.random()*796) + 5, // between 5 and 200
							l			= Math.floor(Math.random()*1296) + 5, // between 5 and 700
							a			= Math.floor(Math.random()*701) - 50; // between -50 and 50
							$el			= $(this),
							param		= {
									width	: '25px',
									height	: '25px',
									opacity	: o,
									top		: t + 'px',
									left	: l + 'px'
							};
							if (!$.browser.msie) param.rotate	= a + 'deg';		
							var delay = i*15;		
							$el.delay(delay).animate(param, 800, 'easeInOutQuad');
						});
					},
					initEventsHandler	= function() {
						/*click a link in the menu1*/
							$menuItems1.bind('click', function(e) {
							e.preventDefault();
							$(".mb_overlay1").css({ display : 'block', zIndex : 4 });
							var $this	= $(this),
							pos 	= parseInt($this.attr('id')),
							speed	= $this.data('speed'),
							easing	= $this.data('easing'); //if an item is not yet shown
							if(!$menu1.data('open')){ //if current animating return
								if($menu1.data('moving')) return false;
								$menu1.data('moving', true);
								$.when(openItem(speed, easing)).done(function(){
									$menu1.data({
										open	: true,
										moving	: false
									});
									showContentItem1(pos);
									setTimeout(function(){showContentItem1(pos);$mbPattern.children().fadeOut(400);},633);
								});
							}
							else
							{
								showContentItem1(pos);
								}
							return false;
						});
						/*click a link in the menu2*/
						$menuItems2.bind('click', function(e) {
							e.preventDefault();
							$(".mb_overlay1").css({ display : 'block', zIndex : 4 });
							var $this	= $(this),
							target1 = $this.attr('href') + "/About.html",
							target2 = $this.attr('href') + "/Fact.html",
							title = $this.attr('name'),
							speed	= $this.data('speed'),
							easing	= $this.data('easing');
							$curCont = $this.attr('href');
							document.getElementById("title2").innerHTML = title;
							if(!$menu2.data('open')){ //if an item is not yet shown
								if($menu2.data('moving')) return false;	//if current animating return
								$menu2.data('moving', true);
								$.when(openItem(speed, easing)).done(function(){
									$menu2.data({
										open	: true,
										moving	: false
									});
									setTimeout(function(){showContentItem2(target1,target2);$mbPattern.children().fadeOut(400);},580);
								});
							}
							else
								showContentItem2(target1,target2);
							return false;
						});
						/*clicking a link in the external menu*/
						$extMenuItems.bind('click', function(e) {
							e.preventDefault();
							$(".mb_overlay1").css({ display : 'block', zIndex : 7 });
							var $this	= $(this),
							pos		= $this.index(),
							speed	= $this.data('speed'),
							easing	= $this.data('easing');
							//if an item is not yet shown
							if(!$extMenu.data('open')){
								//if current animating return
								if($extMenu.data('moving')) return false;
								$extMenu.data('moving', true);
								$.when(openItem(speed, easing)).done(function(){
									$extMenu.data({
										open	: true,
										moving	: false
									});
									setTimeout(function(){showExtContentItem(pos);$mbPattern.children().fadeOut(400);},800);
								});
							}
							else
								showExtContentItem(pos);
							return false;
						});
						/*clicking on the events page menu */
						$eveMenuItems.bind('click',function(e) {
							e.preventDefault();
							var $this = $(this),
							pos = $this.index(),
							menu = document.getElementById('eve_menu' + pos);
							if(pos != 6)
							{
							showEventMenu(menu,pos);
							}
							else
							{
								if($subMenuCur != null){
										if($backVis == true){
											$($eveBack).hide('slow','easeOutBack',function(){$backVis = false;});
										}
										$($subMenuCur).hide('slow','easeOutBack');
									}
									slideOutAnim(targetMenu);
								$.scrollTo({top:ht*2 + 'px',left:'0px'},700,'easeOutQuad',{queue:true});
								targetMenu = document.getElementById('spotlights_menu');
								slideInAnim(targetMenu);
									$(targetMenu).show('0',function(){$subMenuCur = targetMenu;});
							}
						});
						/*click close makes the boxes animate to the top of the page (for the first wrapper)*/
						$mbClose1.bind('click', function(e) {
							e.preventDefault();
							$menu1.data('open', false);
							$mbPattern.children().fadeIn(500, function() {
								$mbContentItems1.fadeOut(400);
								$mbWrapper1.fadeOut(400);
								$(".mb_overlay1").css({ display : 'none' });
							});	
							disperse();
							return false;
						});
						/*click close makes the boxes animate to the top of the page (for the second wrapper)*/
						$mbClose2.bind('click', function(e) {
							e.preventDefault();
							$menu2.data('open', false);
							$mbPattern.children().fadeIn(500, function() {
								$mbContentItems2.fadeOut(400);
								$mbWrapper2.fadeOut(400);
								$(".mb_overlay1").css({ display : 'none' });
							});
							disperse();
							return false;
						});
						/*click close makes the boxes animate to the top of the page in external items*/
						$mbClose3.bind('click', function(e) {
							$extMenu.data('open', false);
							$mbPattern.children().fadeIn(500, function() {
								$mbContentItems3.fadeOut(400);
								$mbWrapper3.fadeOut(400);
								$(".mb_overlay1").css({ display : 'none' });
							});
							disperse();
							return false;
						});
						/*clicking on main menu links */
						$mainMenuItems.bind('click',function(){
								$($curSec).removeClass('active');
								$(this).addClass('active');
								$curSec = $(this);
								var targetId = $(this).attr('href');
									targetMenuId = $(this).attr('name') + "_menu",
									targetMenu = document.getElementById(targetMenuId);
									disperse();
									if($subMenuCur != null){
										if($backVis == true){
											$($eveBack).hide('slow','easeOutBack',function(){$backVis = false;});
										}
										$($subMenuCur).hide('slow','easeOutBack');
									}
									slideOutAnim(targetMenu);
									switch(targetId){
										case '#home' : $.scrollTo({top:'0px',left:wd + 'px'},700,'easeOutQuad',{queue:true}); break;
										case '#abt_us' : $.scrollTo({top:'0px',left:'0px'},700,'easeOutQuad',{queue:true}); break;
										case '#sponsors' : $.scrollTo({top:'0px',left:wd*2 + 'px'},700,'easeOutQuad'); break;
										case '#events' : $.scrollTo({top:ht + 'px',left:wd + 'px'},700,'easeOutQuad',{queue:true}); break;
										case '#attractions' : $.scrollTo({top:ht + 'px',left:'0px'},700,'easeOutQuad',{queue:true}); break;
										case '#workshops' : $.scrollTo({top:ht + 'px',left:wd*2 + 'px'},700,'easeOutQuad',{queue:true}); break;
										case '#spotlights' : $.scrollTo({top:ht*2 + 'px',left:'0px'},700,'easeOutQuad',{queue:true}); break;
										case '#pro_nites' : $.scrollTo({top:ht*2 + 'px',left:wd +'px'},700,'easeOutQuad',{queue:true}); break;
										case '#gallery' : $.scrollTo({top:ht*2 + 'px',left:wd*2 + 'px'},700,'easeOutQuad',{queue:true}); break;
									}
									slideInAnim(targetMenu);
									$(targetMenu).show('0',function(){$subMenuCur = targetMenu;});
									if(targetId == "#sponsors"){
										openSponsors();
									}
									return false;			
						});
					/*binding event to back to events button (needs to be changed)*/
					$($eveBack).bind('click',function(e){
							e.preventDefault();
							if($subMenuCur != null){
								$($subMenuCur).hide('slow','easeOutBack');
							}
							$("#events_menu").show('0','easeOutBack',function(){
									$subMenuCur = document.getElementById("events_menu");
							});
							$(this).hide('slow',function(){
											$backVis = false;
									});
					});
					/*binding events to inner content Menu*/
					$abtLink.bind('click',function(e){
						e.preventDefault();
						var targetCont = $curCont + "/About.html";
						ajax_loadContent($mbContentInnerItems2.children('div').attr('id'),targetCont);
					});
					$rulLink.bind('click',function(e){
						e.preventDefault();
						var targetCont = $curCont + "/Details.html";
						ajax_loadContent($mbContentInnerItems2.children('div').attr('id'),targetCont);
					});			
					},
					/*When we are changing in between events submenus (needs to be changed)*/
					showEventMenu = function(menu,pos){
						if($subMenuCur != null){
							$($subMenuCur).hide('slow','easeOutBack');
					}
					$(menu).show('slow','easeOutBack',function(){
							$subMenuCur = menu;
					});
					$($eveBack).show('slow','easeOutBack',function(){$backVis = true;});
					},
					/*function to open sponsors*/
					openSponsors			= function(){
						$(".mb_overlay1").css({ display : 'block', zIndex : 4 });
						if($menu1.data('moving')) return false;
						$menu1.data('moving', true);
						$.when(openItem(800, 'easeInOutQuad')).done(function(){
									$menu1.data({
									open	: true,
									moving	: false
								});
						showContentItem1(4);
						$mbPattern.children().fadeOut(400);
						});			
					}
					/*This shows a content item when there is already one shown: (in the first wrapper)*/
					showContentItem1		= function(pos) {
						$mbWrapper1.fadeIn(500,'swing');
						$mbContentItems1.hide();
						$mbContentItems1.eq(pos).fadeIn(800,'swing').children('.mb_content_inner');
						$($mbContentInnerItems1).children(".mainContent");
					},
					/*This shows a content item when there is already one shown: (in the first wrapper)*/
					showContentItem2		= function(target1,target2) {
						$mbWrapper2.fadeIn(500,'swing');
						$mbContentItems2.hide();
						$mbContentItems2.fadeIn(600,'swing').children('.mb_content_inner');
						ajax_loadContent($mbContentInnerItems2.children('.mainContent').attr('id'),target1);
						ajax_loadContent($mbContentInnerItems2.children('.facts').attr('id'),target2);
					},
					/*This shows a content item when there is already one shown in external links*/
					showExtContentItem		= function(pos) {
						$mbContentItems3.hide();
						$mbWrapper3.fadeIn(500,'swing');
						if(pos == 3)
						{
							ajax_loadBrochure();
							$mbContentItems3.eq(pos).fadeIn(600,'swing').children('.mb_content_inner');
							$($mbContentInnerItems3).children(".mainContent").jScrollPane();

						}
						else
						{	
							$mbContentItems3.eq(pos).fadeIn(600).children('.mb_content_inner');
							$($mbContentInnerItems3).children(".mainContent");
						}
					},
					/*sliding animation during menu link click*/
					slideOutAnim			= function(targetMenu){
						var $header = $('#head'),
							$footer = $('#foot'),
							$social = $('.slideContainer'),
							$menu   = $('.mb_menu');;
							$header.animate({
												top: '-78px'
							},'slow','swing');
							$social.animate({
												left: '-70px'
							},'slow','swing');
							$menu.animate({
												right: '-230px'
							},'slow','swing');
							$footer.animate({
												bottom: '-61px'
							},'slow','swing');
					},
					slideInAnim				= function(targetMenu){
						var $header = $('#head'),
							$footer = $('#foot'),
							$social = $('.slideContainer'),
							$menu   = $('.mb_menu');;
							$header.animate({
												top: '0px'
							},'slow','swing');
							$social.animate({
												left: '-10px'
							},'slow','swing');
							$menu.animate({
												right: '0px'
							},'slow','swing');
							$footer.animate({
												bottom: '0px'
							},'slow','swing');
					},
					/*moves the boxes from the top to the center of the page,and shows the respective content item*/
					openItem			= function(speed, easing) {
						return $.Deferred(
						function(dfd) {
							$mbPattern.children().each(function(i) {
								var $el		= $(this),
								param		= {
									width	: '100px',
									height	: '100px',
									top		: 100 + 100 * Math.floor(i/9),
									left	: 93 + 100 * (i%9),
									opacity	: 0.5
								},
								delay = 660-i*15;				
								if (!$.browser.msie) param.rotate	= '0deg';	
								$el.delay(delay).animate(param, speed, easing, dfd.resolve);
							});
						}
					).promise();
					};
					return {init : init};
				})();
				/*call the init method of Menu*/
				Menu.init();
			});
		});
	});
