

var AppSite = {
	onReady: function()
	{
		
		//	Initialise behaviours for elements, if they exist
		for (var element_id in this.elementBehaviours)
		{
			var elm = $(element_id);

			if (elm.length > 0)
			{
				
				this.elementBehaviours[element_id](elm);
			}
		}
	},
	
	setV8Viewer: function(skin)
	{
		$('#v8_wrapper').attr('rel', skin);
		this.elementBehaviours['#v8_wrapper']($('#v8_wrapper'));
		$.scrollTo('#v8_wrapper', 800, { offset: -50 });
	},
	
	elementBehaviours: 
	{
		'#countdown_movie': function($elms)
		{
			//	Add swf version of sitenav
			var so = new SWFObject("/countdown_timer_current.swf", "countdown_movie", "195", "92", "8", "#ffffff");
			
			so.addVariable("time_remaining", document.countdown_time_remaining);
			
			so.write($elms.attr('id'));
		},
		'#v8_wrapper': function($elms)
		{
			//	Add swf version of sitenav
			var so = new SWFObject("/v8/v8viewer.swf", "v8_viewer", "508", "331", "9", "#ffffff");
			//alert($elms.html());
			
		  //  so.addVariable("skin", '/docs/car_skin.png');
			  so.addVariable("skin", ($elms.attr('rel')));
			  
			  
			  
			so.write($elms.attr('id'));
		},	
		'#videoplayer_movie': function($elms)
		{
			//	Add swf version of sitenav
			var so = new SWFObject("videoplayer_current.swf", "videoplayer_movie", "370", "280", "8", "#ffffff");
			
			so.write($elms.attr('id'));
		},
		
		'#videoplayer_controltac_current': function($elms)
		{
			//	Add swf version of sitenav
			var so = new SWFObject("/videoplayer_controltac_current.swf", "videoplayer_controltac_current", "370", "280", "8", "#ffffff");
			
			so.write($elms.attr('id'));
		},
		
		
		
		'#did_you_know_movie': function($elms)
		{
			//	Add swf version of sitenav
			var so = new SWFObject("/why_fleet_graphics__current.swf", "videoplayer_movie", "806", "240", "8", "#ffffff");
			
			so.write($elms.attr('id'));
		},
		
		//	Hall of Fame image popups - open popup window
		'.hall_of_fame_panel a[rel=image_popup]': function($elms)
		{
			var popupProfile =
			{
				image_popup:	
				{
					width:800,
					height:600,
					status:0,
					center:1,
					scrollbars:0,
					toolbar:0,
					location:0,
					menubar:0
				}
			};
			
			$elms.popupwindow(popupProfile);
			
			if ($.browser.safari)
			{
				$elms.each(
					function()
					{
						this.onclick = function()
						{
							return false;
						}
					}
				);
			}
		},
		
		// render thumbs
		'.v8thumb': function($elms) {
			$elms.each(
					function(){
						//alert( $(this).html());
						var so = new SWFObject("/v8/v8viewer.swf", "videoplayer_movie", "138", "96", "9", "#ffffff");
						
				        so.addVariable("skin", $(this).html());
						so.addVariable("thumbnail", 1);
						// so.addVariable("url", "/v8_wrap/with/id,"+$(this).attr('id')+"/");
						so.addVariable("url", $(this).html());
						so.addParam('scaleMode', 'noScale');
						so.addParam('wmode', 'opaque');
						so.write($(this).attr('id'));
					
					
					
					//alert($(this).attr('id'));
						// $(this).html()
					}
			);
		},
		
		//	Ajax-behavior on V8 Entries Gallery
		'#v8_entry_images': function($elms) 
		{
			
			//	Pagination links
			$('.pagination a', $elms).click(
				function(e)
				{
					//	Keep href
					var href = $(this).attr('href');
					
					//	Remove current content, replace with loader
					$('#v8_entry_images > div:not(.pagination)').remove();
					$('#ajax_loader').attr('id', 'ajax_loader_enabled');
					
					$('#v8_entry_images').load(
						href + ' #v8_entry_images > div',
						function()
						{							
							//	Re-apply behaviors
							AppSite.elementBehaviours['#v8_entry_images']($('#v8_entry_images'));
							AppSite.elementBehaviours['.v8thumb']($('.v8thumb'));
													
							//	Remove loader
							$('#ajax_loader_enabled').attr('id', 'ajax_loader');
						}
					);
					
					e.stopPropagation();
					e.preventDefault();
					return false;
				}
			);
		},
		
		//	Hall of Fame image popup pages - close window on click
		'#view_image_template a.image': function($elms)
		{
			$elms.click( 
				function(e)
				{
					window.close();
					
					e.stopPropagation();
					e.preventDefault();
					return false;
				}	
			);
		},
		
		//	Blink animation
		'.blink': function($elms)
		{
			var updateFunction = function()
			{
				if ($elms.css('visibility') == 'hidden')
				{
					$elms.css('visibility', 'visible');
				}
				else
				{
					$elms.css('visibility', 'hidden');
				}
				
			};
			
			setInterval(updateFunction, 700);
		},
		
		'#v8 .marker': function($elms)
		{
			$elms.hide();
		},
		'#v8 .show_marker': function($elms)
		{
			$('#v8 .marker').show();
		},
		'#v8 .error_notices': function($elms)
		{
		 $('.marker').show();
		},
		
		'#login_panel input[name=login_name], #login_panel input[name=login_password]': function ($elms)
		{
			$elms.focus(
			function() {
				if (this.has_selected != true)
				{
					this.select();
					this.has_selected = true;
				}
				
				}
			).blur(
				function() {
					this.has_selected = false;
				}
			);
		},
		
		'form input[name=distributor_or_sign_shop]': function($elms)
		{
			var updateFunction = function(e, skip_animation)
			{
				var value = $('input[name=distributor_or_sign_shop]:checked').val();
				
				if ($.browser.safari) skip_animation = true;
				
				var open_anim = (skip_animation) ? 'show' : 'slideDown';
				var close_anim = (skip_animation) ? 'hide' : 'slideUp';
				
				if (value == 'A Sign Shop')
				{
					$('form .distributor_container')[open_anim]();
				}
				else
				{
					$('form .distributor_container:visible')[close_anim]();
				}
				
				if (value == 'An End User')
				{
					$('form .distributor_other_container')[open_anim]();
				}
				else
				{
					$('form .distributor_other_container:visible')[close_anim]();
				}
			}
			 
			
			$elms.click(updateFunction);

			updateFunction(null, true);

		},

		'form input[name=distributor_or_sign_shop-2]': function($elms)
		{
			var updateFunction = function(e, skip_animation)
			{
				var value = $('input[name=distributor_or_sign_shop-2]:checked').val();
				
				if ($.browser.safari) skip_animation = true;
				
				var open_anim = (skip_animation) ? 'show' : 'slideDown';
				var close_anim = (skip_animation) ? 'hide' : 'slideUp';
				
				if (value == 'A Sign Shop')
				{
					$('form .distributor_container-2')[open_anim]();
				}
				else
				{
					$('form .distributor_container-2:visible')[close_anim]();
				}
				
				if (value == 'An End User')
				{
					$('form .distributor_other_container-2')[open_anim]();
				}
				else
				{
					$('form .distributor_other_container-2:visible')[close_anim]();
				}
			}
			 
			
			$elms.click(updateFunction);

			updateFunction(null, true);

		},
		'form input[name=distributor_or_sign_shop-3]': function($elms)
		{
			var updateFunction = function(e, skip_animation)
			{
				var value = $('input[name=distributor_or_sign_shop-3]:checked').val();
				
				if ($.browser.safari) skip_animation = true;
				
				var open_anim = (skip_animation) ? 'show' : 'slideDown';
				var close_anim = (skip_animation) ? 'hide' : 'slideUp';
				
				if (value == 'A Sign Shop')
				{
					$('form .distributor_container-3')[open_anim]();
				}
				else
				{
					$('form .distributor_container-3:visible')[close_anim]();
				}
				
				if (value == 'An End User')
				{
					$('form .distributor_other_container-3')[open_anim]();
				}
				else
				{
					$('form .distributor_other_container-3:visible')[close_anim]();
				}
			}
			 
			
			$elms.click(updateFunction);

			updateFunction(null, true);

		}
	}
};


function addEntry(){
	var count = $("#total_ent").attr('value');
	if(count < 3){
		var next_count = count;
		next_count++;
	    $("#total_ent").attr('value', next_count);
		//find first entry that is not hidden and display
	      $(".comp_entry").each(function (i) {
		        if (this.style.display == "none") {
		         this.style.display = '';
			
				   $(".submit").each(function (j) {
						this.style.display = 'none';
				   });
				   $(".submit:last").show();
				 return false;

				 
				 
				 
		        } 
	      });

	    //    $("#entry_" + next_count).show();
		//$("#submit_ent" + count).hide();
	}else{
		alert('A max of 3 entries can be submitted at one time.');
	}
	
}

$(document).ready(
	function()
	{
		AppSite.onReady();


		//$(".marker").hide();

		//  hide buttons
		var count = $("#total_ent").attr('value');
		if(count == 1 ){
	        	$("#entry_2").hide();
		}
		if(count < 3 ){
	        	$("#entry_3").hide();
		}
		
	}

);

function setV8Viewer(skin)
{
	AppSite.setV8Viewer(skin);
}
