$(document).ready(function()
{
	/*= UI Effects
	**********************************************/
	
	// Apply shine to h1 tags
	if ($("#rightcolumn h1").length > 0)
		$("#rightcolumn h1").prepend("<span></span>");
	
	// Apply sliding door buttons
	if ($("a.button").length > 0)
	{
		$("a.button").append("<span></span>");
		$("a.button").addClass("fancybutton");
	}
	

	// Set hover for drop down menu
	if ($("#header ul li").length > 0)
	{
		$("#header ul li").hover(
			function(){ $("ul", this).fadeIn("fast"); }, 
			function() { $("ul", this).fadeOut("fast");} 
		);
		$("#header ul li").hoverClass("sfHover");
	}

	// Set hover for subnav drop down menu
	if ($(".subnav ul li").length > 0)
	{
		$(".subnav ul li").hover(
			function(){ $("ul", this).fadeIn("fast"); }, 
			function() { $("ul", this).fadeOut("fast"); } 
		);
		$(".subnav ul li").hoverClass("sfHover");
	}
	
	
	// Create print button
	$(".panel .subnav ul").append('<li class="print"><span><a href="#">Print This Page</a></span></li>');
	// Attach Print Event
	$(".print a").bind("click", function() {
		window.print();
	});
	
	// Set Accordion
	if ($("#left.homecolumn").length > 0)
	{
		$("#left.homecolumn").accordion({
			alwaysOpen: true,
			autoheight: false,
			header: 'a.span',
			clearStyle: true
		});
	}
	
	// Set Global Navigation
	if ($("#rightcolumn #breadcrumb li a").length > 0)
		$("#rightcolumn #breadcrumb li a").append(' &nbsp;&rsaquo;');
	
	// Set any quote boxes
	if ($("#rightcolumn blockquote div").length > 0)
	{
		$("#rightcolumn blockquote div").append('<span class="topleft"></span>');
		$("#rightcolumn blockquote div").append('<span class="topright"></span>');
		$("#rightcolumn blockquote div").append('<span class="bottomleft"></span>');
		$("#rightcolumn blockquote div").append('<span class="bottomright"></span>');
	}
	
	// Set Share Page button
	if ($(".share span a").length > 0)
	{
		$(".share span a").removeAttr("href");
		$(".share span a").click(function() {
			window.open("../forms/share.php?url=" + window.location, "share", "width=500,height=470");
		});
	}
	
	
	/*= Load Home Page Story Animation Settings
	**********************************************/
	if ($("#storypanel #slider").length > 0)
	{
		// Add rounded corners
		$("#storypanel").append('<span class="corners"></span>');
		
		var $panels = $('#slider .scrollContainer > div');
		var $container = $('#slider .scrollContainer');
		
		// if false, we'll float all the panels left and fix the width 
		// of the container
		var horizontal = true;
		
		// float the panels left if we're going horizontal
		if (horizontal) {
		  $panels.css({
		    'float' : 'left',
		    'position' : 'relative' // IE fix to ensure overflow is hidden
		  });
		  
		  // calculate a new width for the container (so it holds all panels)
		  $container.css('width', $panels[0].offsetWidth * $panels.length);
		}
		
		// collect the scroll object, at the same time apply the hidden overflow
		// to remove the default scrollbars that will appear
		var $scroll = $('#slider .scroll').css('overflow', 'hidden');
		
		// apply our left + right buttons
		//$scroll
		  //.before('<img class="scrollButtons left" src="../images/ui-scroll-left.png" />')
		  //.after('<img class="scrollButtons right" src="../images/ui-scroll-right.png" />');
		// Show next button
		$(".scrollButtons").css("display", "block");
		
		// handle nav selection
		function selectNav() {
		  $(this)
		    .parents('ul:first')
		      .find('a')
		        .removeClass('selected')
		      .end()
		    .end()
		    .addClass('selected');
		}
		
		$('#slider .navigation').find('a').click(selectNav);
		
		// go find the navigation link that has this target and select the nav
		function trigger(data) {
		  var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
		  selectNav.call(el);
		}
		
		if (window.location.hash) {
		  trigger({ id : window.location.hash.substr(1) });
		} else {
		  $('ul.navigation a:first').click();
		}
		
		// offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset.  Try removing this to get a good idea of the effect
		var offset = parseInt((horizontal ? 
		  $container.css('paddingTop') : 
		  $container.css('paddingLeft')) 
		  || 0) * -1;
		
		var scrollOptions = {
		  target: $scroll, // the element that has the overflow
		  
		  // can be a selector which will be relative to the target
		  items: $panels,
		  
		  //navigation: '.navigation a',
		  navigation: '',
		  
		  // selectors are NOT relative to document, i.e. make sure they're unique
		  prev: 'img.left', 
		  next: 'img.right',
		  
		  // allow the scroll effect to run both directions
		  axis: 'xy',
		  
		  onAfter: trigger, // our final callback
		  
		  offset: offset,
		  
		  // duration of the sliding effect
		  duration: 300,
		  
		  // easing - can be used with the easing plugin: 
		  // http://gsgd.co.uk/sandbox/jquery/easing/
		  easing: 'swing'
		};
		
		// apply serialScroll to the slider - we chose this plugin because it 
		// supports// the indexed next and previous scroll along with hooking 
		// in to our navigation.
		$('#slider').serialScroll(scrollOptions);
		
		// now apply localScroll to hook any other arbitrary links to trigger 
		// the effect
		$.localScroll(scrollOptions);
		
		// finally, if the URL has a hash, move the slider in to position, 
		// setting the duration to 1 because I don't want it to scroll in the
		// very first page load.  We don't always need this, but it ensures
		// the positioning is absolutely spot on when the pages loads.
		scrollOptions.duration = 1;
		$.localScroll.hash(scrollOptions);
		
	 }
	
	
	if ($("#gs").length > 0)
	{
		$("#gs").validate({
			rules: {
				gs_name: "required",
				gs_title: "required",
				gs_dealership: "required",
				gs_zip: "required",
				gs_phone: "required",
				gs_email: {
					required: true,
					email: true
				}
			},
			messages: {
				gs_name: "Please enter your name",
				gs_title: "Please enter your title",
				gs_dealership: "Please enter your dealership",
				gs_zip: "Please enter your zip code",
				gs_phone: "Please enter your phone number",
				gs_email: "Please enter a valid email address"
			}
		});
	}
	
	
	/*= Fancy Zoom for Image Galleries
	**********************************************/
	if ($("a.zoom").length > 0)
		$("a.zoom").fancyZoom({scaleImg: true, closeOnClick: true});
	if ($("a.zoomswf").length > 0)
		$("a.zoomswf").fancyZoom({scaleImg: true, closeOnClick: false});
		
	
	
	/*= Dealer Code Form Effects
	**********************************************/
	
	// Hide Dealer Code expansion
	if ($("#getcode").length > 0)
	{
		$(".openDealerCode").removeClass("hide");
		$("#getcode").addClass("hide");
	}
		
	// Set Click handler for Dealer Code expansion
	if ($(".openDealerCode a").length > 0)
	{
		$(".openDealerCode a").bind("click", function(){
			$("#getcode").removeClass("hide");
			$(".openDealerCode").addClass("hide");
		});
	}
	
	
	/*= Load Video Gallery XML
	**********************************************/
	if ($("#videopanel #slider").length > 0)
	{
		// Create Array for video links
		var videoArray = new Array();
		
		// Remove links
	    $(".scrollContainer li a").removeAttr("href");
	    
	   	// Process XML
	   	$.get("videofeed.xml", {}, function(xml) {
	   		$('item', xml).each(function(i){
	   			tempObj = new Object();
	   			tempObj.video = "../video/" + $(this).find("id").text();
	   			tempObj.photo = "../video/" + $(this).find("id").text();
	   			tempObj.runtime = $(this).find("runtime").text();
	   			tempObj.title = $(this).find("title").text();
	   			tempObj.pdf = $(this).find("pdf").text();
	   			videoArray.push(tempObj);
	   		});
	   	});
	    
	    // Temporary code for the new video player
	    if ($(".scrollContainerNew li a").length > 0)
	    {
	    	// Add new click handlers
		    $(".scrollContainerNew li a").each(function(i){
		    	$(this).click(function(){
		    		// Clear selected state from buttons
		    		$(".scrollContainerNew li a").removeClass("selected");
		    		// Add selected class to this button
		    		$(this).addClass("selected");
		    		// Run video function
		    		selectVideo(videoArray[i].video + ".mov", videoArray[i].photo + ".jpg", "player", "320", "240", true);
		    		// Add the pdf link to the video section
		    		if (videoArray[i].pdf.length > 1)
		    			$("#videopdf").html('<a href="../pdf/' + videoArray[i].pdf + '" target="_blank" class="document"> ' + videoArray[i].title + '</a>');
		    		else
		    			$("#videopdf").html("");
		    	});
		    });
		}
		else
		{
			$(".scrollContainer li a").each(function(i){
		    	$(this).click(function(){
		    		// Clear selected state from buttons
		    		$(".scrollContainer li a").removeClass("selected");
		    		// Add selected class to this button
		    		$(this).addClass("selected");
		    		// Run video function
		    		thisMovie("videofeed").playvid(videoArray[i].video + ".flv", videoArray[i].photo + ".jpg", videoArray[i].runtime);
		    		// Add the pdf link to the video section
		    		if (videoArray[i].pdf.length > 1)
		    			$("#videopdf").html('<a href="../pdf/' + videoArray[i].pdf + '" target="_blank" class="document"> ' + videoArray[i].title + '</a>');
		    		else
		    			$("#videopdf").html("");
		    	});
		    });
		}
	 }
	 
	
	/*= Scroll Pane Effects
	**********************************************/
	
	if ($("#videopanel #slider").length > 0)
	{

		var $panels = $('#slider .scrollContainer > div');
		var $container = $('#slider .scrollContainer');
		
		
		
		// if false, we'll float all the panels left and fix the width 
		// of the container
		var horizontal = true;
		
		// float the panels left if we're going horizontal
		if (horizontal) {
		  $panels.css({
		    'float' : 'left',
		    'position' : 'relative' // IE fix to ensure overflow is hidden
		  });
		  
		  // calculate a new width for the container (so it holds all panels)
		  $container.css('width', $panels[0].offsetWidth * $panels.length);
		}
		
		// collect the scroll object, at the same time apply the hidden overflow
		// to remove the default scrollbars that will appear
		var $scroll = $('#slider .scroll').css('overflow', 'hidden');
		
		// apply our left + right buttons
		$scroll
		  .before('<img class="scrollButtons left" src="../images/ui-scroll-left.png" />')
		  .after('<img class="scrollButtons right" src="../images/ui-scroll-right.png" />');
		
		// handle nav selection
		function selectNav() {
		  $(this)
		    .parents('ul:first')
		      .find('a')
		        .removeClass('selected')
		      .end()
		    .end()
		    .addClass('selected');
		}
		
		$('#slider .navigation').find('a').click(selectNav);
		
		// go find the navigation link that has this target and select the nav
		function trigger(data) {
		  var el = $('#slider .navigation').find('a[href$="' + data.id + '"]').get(0);
		  selectNav.call(el);
		}
		
		if (window.location.hash) {
		  trigger({ id : window.location.hash.substr(1) });
		} else {
		  $('ul.navigation a:first').click();
		}
		
		// offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset.  Try removing this to get a good idea of the effect
		var offset = parseInt((horizontal ? 
		  $container.css('paddingTop') : 
		  $container.css('paddingLeft')) 
		  || 0) * -1;
		
		
		var scrollOptions = {
		  target: $scroll, // the element that has the overflow
		  
		  // can be a selector which will be relative to the target
		  items: $panels,
		  
		  navigation: '.navigation a',
		  
		  // selectors are NOT relative to document, i.e. make sure they're unique
		  prev: 'img.left', 
		  next: 'img.right',
		  
		  // allow the scroll effect to run both directions
		  axis: 'xy',
		  
		  onAfter: trigger, // our final callback
		  
		  offset: offset,
		  
		  // duration of the sliding effect
		  duration: 300,
		  
		  // easing - can be used with the easing plugin: 
		  // http://gsgd.co.uk/sandbox/jquery/easing/
		  easing: 'swing'
		};
		
		// apply serialScroll to the slider - we chose this plugin because it 
		// supports// the indexed next and previous scroll along with hooking 
		// in to our navigation.
		$('#slider').serialScroll(scrollOptions);
		
		// now apply localScroll to hook any other arbitrary links to trigger 
		// the effect
//		$.localScroll(scrollOptions);
		
		// finally, if the URL has a hash, move the slider in to position, 
		// setting the duration to 1 because I don't want it to scroll in the
		// very first page load.  We don't always need this, but it ensures
		// the positioning is absolutely spot on when the pages loads.
//		scrollOptions.duration = 1;
//		$.localScroll.hash(scrollOptions);
	}
	
});


$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


// Return correct path to Flash Movie
function thisMovie(movieName) 
{
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	}
	else {
		return document[movieName]
	}
}