$(init_page);

function init_page(){			
  /*
	if($("#no_flash:visible").length > 0)
		show_content();
	else		
		setInterval(show_content, 4500);
		*/
	
	if(location.hash == '#no_intro') {
  	$('#flashobject_background_obj').remove();	
  	$('#intro').remove();
		
		intro_loaded();
    setTimeout(function(){ menu_hover_in($("#main_menu li.current-page-item").find('a'), true);}, 1000);
    
	}

  $("#main_menu li.subpage-item.current-page-parent").addClass('current-page-item'); // this to select sub submenu 
	$('ul#main_menu >li a[href="' + WWW_ROOT + '"]').attr('href', WWW_ROOT + '#no_intro');
		
	setup_menu();
	
	setup_browser();
	
	$('a.lightbox').fancybox();
	
 
	//$("#page").show();
		
	if($("#ipad_loading:visible").length > 0) {
		setTimeout(function() { 	$("#top_banner").stop().animate({ 'opacity':1 }, 300); }, 300);
	} else if(!$.browser.msie)
		$("#top_banner").css({'opacity':1});
		
		
  // remove trailing paragraphs on #top_banner
  cleanup_top_banner();
}

function cleanup_top_banner(){
  
  var main_offset = $('#mask').offset();
  
  //alert(main_offset.top);
  
  var offset = $('#top_banner h1').offset();
  var max =   main_offset.top + 130.75;
  var bottom = (offset.top + $('#top_banner h1').height() );
  //alert(bottom);
  if(bottom > max){
    var diff = bottom - max;    
    $('#top_banner h1').css({'margin-top': '-'+ diff +'px'});
  }
    
}

function get_flash(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
 
        return window[movieName];
    }
    else {
       return document.getElementById(movieName + "_obj");
       // return document[movieName + "_obj"]
    }
}

function intro_reveal(){
  var background = get_flash("flashobject_background");
  background.play();
  menu_hover_in($("#main_menu li.current-page-item").find('a'), true);
  
}

function intro_revealed(){
  //alert("revealed");  
  $('#intro').remove();
}
function intro_loaded(){
  $('#background').show();
  $('#page').show();  
}

function background_loaded(){
 // alert("background_loaded");  
}

function show_content() {
	if ($("#no_flash:visible").length > 0) {
		$("#page").show();
		
	} else {
	  
	  
	  
	  
		$("#flashobject_intro").hide();//(1500, function(){
			//		$("#mask").css('background-image', 'url("' + WWW_TEMPLATE + '/images/background.jpg")');
			//	$("#flashobject_intro").fadeOut(500, function() { $(this).remove(); });
		//});
	}
	menu_hover_in($("#main_menu li.current-page-item").find('a'), true);
}

function setup_browser() {		
	if (navigator.userAgent.match(/Android/i) ||
	  navigator.userAgent.match(/webOS/i) ||
	  navigator.userAgent.match(/iPad/i) ||
	  navigator.userAgent.match(/iPhone/i) ||
	  navigator.userAgent.match(/iPod/i)) {
			
		$("#screens").iPadApp({
			width:'669',
			height:'334',
			autosize: false,
			elastic: false,
			screen_class:'screen',
			main_menu_id:'main_menu',
			main_menu_overlay:false,
			main_menu_hover_in:menu_hover_in,
			main_menu_hover_out:menu_hover_out,
			loading_image:WWW_TEMPLATE + '/images/loading.gif',
			loading_image_width:94,
			loading_image_height:94,
			home_button:false,
			mask_id:'content',
			wrap_id:'wrap',
			easing:'circEaseOut',
			nav_buttons_img:WWW_TEMPLATE + '/images/screen_nav_buttons.png',
			nav_button_size:15,
			nav_align:'center'
		});

  //alert("BLAH");
    $('#mask.home').addClass('ipad_home');
  	$('#intro').remove();
  	$('#page').show();
    setTimeout(function(){ menu_hover_in($("#main_menu li.current-page-item").find('a'), true);}, 1000);
  		
 
	} else {
		
		$('#screens').find("p, li").css({
			'padding-right':'10px'
		});
		
		$('#screens').find("h4, h5").not(":first").css({
			'padding-top':'30px'
		})
		
		$('#screens').find("p").each(function() {
			if($(this).width() > 634)
				$(this).width(634);
		});
		
		$('#screens img').each(function() {			
			if($(this).width() > 634)
				$(this).width(634);
		});
		
		$('#screens').show().find(".screen:first").css({
			'left':'0px',
			'width':'669px'
		});
		
		var new_content = '';
		$('#screens').find(".screen").each(function() {
			new_content += $(this).html();// + "<p>&nbsp;</p>";
		});
		$('#screens').find(".screen:first").html(new_content).css({ 'height':'324px' });
		setTimeout(init_scrollbar, 250);
		
		if (!$.browser.webkit) {
	  	if ($.browser.msie) {
				
				$("#main_menu a").css({
		  		'top': '1px'
		  	});	
	
	  		$("#main_menu em").css({
	  			'top': '2px',
	  			'bottom': '5px'
	  		});	
			}	else {
	  		$("#main_menu em").css({
	  			'top': '1px',
	  			'bottom': '5px'
	  		});
	  	}
		}
  }
}

function init_scrollbar() {
	$('#screens').find(".screen:first").jScrollPane();
}

function setup_menu(){
	// hover animations
	$("#main_menu li").not('.current-page-item').find('a').hover(menu_hover_in, menu_hover_out);
	menu_hover_in($("#main_menu li.current-page-item").find('a'), true);
	
	// click/touch animations
	$("#main_menu li").find('a').click(function() { menu_hover_out($(this), true); menu_hover_out($("#main_menu li.current-page-item").find('a'), true); });
}

function menu_hover_in(obj, quick) {
	if($(this).get(0).tagName != null) {
		obj = $(this);
  }
	
	var speed = 200;
	if(quick != null && quick)
		speed = 150;
	
		
	if ($.browser.msie) {
  	if (jQuery.browser.version < 8.0) {
  		obj.css({
  			'top': '2px'
  		});
  	}
  }
	
	var bar_width = obj.innerWidth();

	if(obj.parent('li.subpage-item').length > 0)
		bar_width += 10;
		
	obj.parent('li').find('em').not('[animating="in"]').stop().css({'display':'block'}).attr('animating', 'in').animate({
		opacity: 1,
  	width: bar_width + 'px'
	}, speed, "sineEaseIn", function(){
		
		if ($.browser.msie) {
	  	if (jQuery.browser.version < 8.0) {
				obj.css({
					'top':'2px'
				});
			}
		}
		
		$(this).attr('animating', '');
		obj.stop().css({
			'color':'#FFFFFF'
		});
	});
	
	if (obj.attr("animating") != 'in') {
  	obj.stop().animate({
  		'color': '#FFFFFF'
  	}, speed, "sineEaseIn", function(){			
			$(this).attr('animating', '');
  		obj.stop().css({
  			'color': '#FFFFFF'
  		});
  	});
  }
}


function menu_hover_out(obj, quick) {
	if($(this).get(0).tagName != null) {
		obj = $(this);
  }
	
	var speed = 350;
	if(quick != null && quick)
		speed = 300;
	
	obj.parent('li').find('em').not('[animating="out"]').stop(true, true).animate({
		opacity: 0,
  	width: '0px'
	}, speed, "expoEaseOut", function(){
		
		
		if ($.browser.msie) {
  		if (jQuery.browser.version < 8.0) {
				obj.css({
					'top': '1px'
				});
			}
		}
	
  	$(this).attr('animating', '');
		$(this).css({ 'display':'none' });
		obj.stop().css({ 'color':'#6d6e70' });
	});
	
	if (obj.attr("animating") != 'out') {
  	obj.stop(true, true).animate({
  		'color': '#6d6e70'
  	}, speed, "expoEaseOut", function(){
  		$(this).attr('animating', '');
  		obj.stop().css({
  			'color': '#6d6e70'
  		});
  	});
  }
}

