// javascript

var Petfundango = {
	
	billboard_current:0,
	billboard_duration:5000,
	billboard_int:Number,
	
	btab_current:Number = 0,
	btab_duration:Number = 10000,
	btab_int:Number,
	btab_group:Array,
	
	dropdown_current:Object,
	dropdown_duration:Number = 500,
	dropdown_int:Number,
	dropdown_native:Object,
	
	init : function()
	{
		// cufon replace
		// Cufon('h1:not(.nocufon)', { color: '-linear-gradient(#1883c0, #0c317d)', fontWeight:'bold' })('h2:not(.nocufon)').now();
		
		Petfundango.btab_group = $(".btabs a");
		$(".btabs:not('.homepage') a").bind('click', Petfundango.select_btab);
		$(".btabs.homepage a").bind('mouseover', Petfundango.select_btab);
		
		if (Petfundango.btab_group.length>0) 
		{
			Petfundango.btab_int = setInterval(Petfundango.next_btab,Petfundango.btab_duration);
		}
		
		$('.dropdown,a.has_submenu').bind('mouseover',Petfundango.dropdown_over)
							.bind('mouseout',Petfundango.dropdown_out);
		Petfundango.dropdown_current = 
		Petfundango.dropdown_native = $('.dropdown:visible');
		
		
		// billboard actions
		$('.billboard .menu a').each(function(index) {
			$(this).bind('click', Petfundango.transition_billboard);
			$(this).bind('click', function(){ clearInterval(Petfundango.billboard_int) });
		});
		
		
		// center the menu
		var w = ($('.kids')) ? 0 : 20;
		$('li.menu').each(function(index) {
			w += $(this).width();
		});
		var pLeft = Math.floor((800-w)/2) - 4 +'px';
		$('#mainmenu').css({ left: pLeft });
		
		// submenu rollover actions
		$('li.menu').bind('mouseover', Petfundango.show_submenu).bind('mouseout', Petfundango.hide_submenu);
		
		var flashvars = {};
		var params = {};
		var attributes = {};
		
		if($('#landingflash').length)
		{
			flashvars.area = AREA;
			params.wmode = 'transparent';
		}
		
		if($('#visaad').length)
		{
			params.wmode = 'transparent';
			swfobject.embedSWF("/swf/visa_ad/visa_ad.swf", "visaad", "300", "70", "9.0.0", "/js/swfobject/expressInstall.swf", flashvars, params);
		}
		
		if($('#quiz_ad').length)
		{
			params.wmode = 'transparent';
			swfobject.embedSWF("/swf/quiz_ad/kidquiz_ad.swf", "quiz_ad", "221", "157", "9.0.0", "/js/swfobject/expressInstall.swf", flashvars, params);
		}
		
		if($('#register_ad').length)
		{
			params.wmode = 'transparent';
			swfobject.embedSWF("/swf/register_ad/registerpet_ad.swf", "register_ad", "221", "157", "9.0.0", "/js/swfobject/expressInstall.swf", flashvars, params);
		}
		
		if($('#ccp_parents').length)
		{
			params.wmode = 'transparent';
			swfobject.embedSWF("/swf/continuous_care_ads/parents.swf", "ccp_parents", "221", "259", "9.0.0", "/js/swfobject/expressInstall.swf", {linkpath:'/parents/continuous_care_for_parents'}, params);
		}
		
		if($('#ccp_teachers').length)
		{
			params.wmode = 'transparent';
			swfobject.embedSWF("/swf/continuous_care_ads/teachers.swf", "ccp_teachers", "221", "259", "9.0.0", "/js/swfobject/expressInstall.swf", {linkpath:'/teachers/continuous_care_for_teachers'}, params);
		}
		
		if($('#critters_banner_teachers').length)
		{
			params.wmode = 'transparent';
			swfobject.embedSWF("/swf/continuous_care_ads/waitinglist.swf", "critters_banner_teachers", "221", "157", "9.0.0", "/js/swfobject/expressInstall.swf", {linkpath:'/teachers/critters_in_the_classroom'}, params);
		}
	
		
		$('.virtualpet.swf').each(function(index) {
			var flashvars = { area:AREA };
			var params = { base:'/swf/virtualpet/' };
			var attributes = {};
			swfobject.embedSWF("/swf/virtualpet/virtualpet.swf", 'hamster_happiness', "670", "560", "9.0.0", "/js/swfobject/expressInstall.swf", flashvars, params);
		});
		
		$('.activity.swf').each(function(index) {
			var swfname = $(this).attr('id');
			var flashvars = { area:AREA };
			var params = { wmode:'transparent', base:'/swf/'+swfname, allowScriptAccess:'always' };
			var attributes = { id:swfname, name:swfname };
			swfobject.embedSWF("/swf/"+swfname+"/"+swfname+".swf", swfname, "664", "505", "9.0.0", "/js/swfobject/expressInstall.swf", flashvars, params, attributes);
		});
		
		$('#restart_game').bind('click', Petfundango.restart_game);
		
		if($('#ecard').length)
		{
			$('#overlay').css({
				width:$(document).width(),
				height:$(document).height(),
				opacity:0.5
			}).bind('click', Petfundango.hide_ecard);
			var ecard = $('#ecard');
			ecard.css({
				top: ($(window).height()/2) - (ecard.height()/2),
				left: ($(window).width()/2) - (ecard.width()/2)
			}).bind('click', Petfundango.hide_ecard);
			
			$('#show_ecard').bind('click', Petfundango.show_ecard);
		}
		
		$('#quicklinks a.close').bind('click', Petfundango.close_quicklinks);
		$('#showquicklinks').bind('click', Petfundango.open_quicklinks);
		if($('#billboardmenu').length) Petfundango.billboard_int = setInterval(Petfundango.change_billboard,Petfundango.billboard_duration);
		
		var flashversion = swfobject.getFlashPlayerVersion();
		if(flashversion.major<9)
		{
			$('#warning,#flashplayer').show();
		}
	},
	
	dropdown_over : function()
	{
		if(Petfundango.dropdown_int) clearTimeout(Petfundango.dropdown_int)
		var id = $(this).attr('rel');
		if(id)
		{
			$('.dropdown').hide();
			var dd = $('#'+id);
			Petfundango.dropdown_current = dd;
			Petfundango.dropdown_show(dd);
		}
	},
	
	dropdown_out : function()
	{
		var id = $(this).attr('rel');
		Petfundango.dropdown_int = setTimeout(Petfundango.dropdown_hide,Petfundango.dropdown_duration);
	},
	
	dropdown_show : function(dd)
	{
		var top = (dd.hasClass('parents') || dd.hasClass('teachers')) ? '36px' : '45px';
		dd.css({ top:'0' }).show().animate({ top:top },250,'easeInOutCubic');
	},
	
	dropdown_hide : function(dd)
	{
		if( Petfundango.dropdown_current!=Petfundango.dropdown_native )
		{
			$('.dropdown').hide();
			if( Petfundango.dropdown_native )
			{
				Petfundango.dropdown_show(Petfundango.dropdown_native);
			}
		}
		
	},
	
	next_btab : function()
	{
		Petfundango.btab_current++;
		var tab = Petfundango.btab_group[Petfundango.btab_current%Petfundango.btab_group.length];
		Petfundango.select_btab(tab);
	},
	
	select_btab : function(t)
	{
		// alert(t.tagName);
		if(t.tagName)
		{
			var tab = $(t);
		}
		else
		{
			var tab = $(this);
			clearInterval(Petfundango.btab_int);
		}
		
		tab.addClass('active').siblings().removeClass('active');
		$('.bboard').children().each(function(index) {
			var child = $(this);
			if(tab!=child) 
			{
				child.hide();
			}
		});
		$('#'+tab.attr('rel')).hide().fadeIn(500);
		return false;
	},
	
	change_billboard : function()
	{
		Petfundango.transition_billboard(++Petfundango.billboard_current%4);
	},
	
	transition_billboard : function(num)
	{
		//clearInterval(Petfundango.billboard_int);
		
		var areas = ['home','teachers','parents','kids','critters'];
		var type = ($(this).attr('rel')) ? $(this).attr('rel') : areas[num];
		
		var billboard_holder = $('.billboard .photo');
		var active_photo = $('#'+type+'_billboard');
		var active_tab = $('#'+type+'_tab');
		active_photo.siblings().each(function(index) {
			$(this).stop();
		});
		billboard_holder.append(active_photo);
		active_photo.fadeIn(400,function(){ active_photo.siblings().each(function(index) {
			$(this).hide();
		}); });
		
		var copy_holder = $('.billboard .copy');
		copy_holder.children('div').each(function(index) {
			$(this).hide();
			$('#'+type+'_copy').show();
		});
		
		active_tab.siblings('a').each(function(index) {
			var inactive = $(this).children('img').attr('src').replace(/_active/,'_inactive');
			$(this).children('img').attr('src',inactive);
		});
		if(active_tab)
		{
			var active = active_tab.children('img').attr('src').replace(/_inactive/,'_active');
			active_tab.children('img').attr('src', active);
		}
		return false;
	},
	
	close_quicklinks : function()
	{
		$('#quicklinks').animate({"height": 'toggle'}, 300, 'easeInOutCubic');
		$.cookie('quicklinks_closed', 1, { expires: 7, path: '/' });
		return false;
	},
	
	open_quicklinks : function()
	{
		$('#quicklinks').animate({"height": 'toggle'}, 300, 'easeInOutCubic');
		$.cookie('quicklinks_closed', 0, { expires: 7, path: '/' });
		return false;
	},
	
	restart_game : function()
	{
		var swfname = $(this).attr('rel');
		Petfundango.get_flash_movie(swfname).restartGame();
		return false;
	},
	
	get_flash_movie : function(movieName)
	{
	    if (navigator.appName.indexOf("Microsoft") != -1) {
	        return window[movieName]
	    }
	    else {
	        return document[movieName]
	    }
	},
	
	show_ecard : function()
	{
		$('#ecard,#overlay').fadeIn(150);
	},
	
	hide_ecard : function()
	{
		$('#ecard,#overlay').fadeOut(150);
	},
	
	show_submenu : function()
	{
		var id = $(this).attr('id').split('_')[1];
		$('#submenu_'+id).show();
	},
	
	hide_submenu : function()
	{
		var id = $(this).attr('id').split('_')[1];
		$('#submenu_'+id).hide();
	},
	
	validate_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		$(form).find(".REQUIRED").each(function(req)
		{
			if(($(this).attr('type')!='checkbox' && this.value == '') || 
				($(this).attr('type')=='checkbox' && !this.checked))
			{
				$(this).parents("div.formfield").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.formfield").removeClass('error');
			}			
		});
		if( !submit_it ) $('#error_message').fadeIn(250); $.scrollTo('#error_message', 800, {easing:'easeInOutCubic'});
		return submit_it;
	}
	
}

$(document).ready(function() {
	Petfundango.init();
});

