﻿// JavaScript Document
var $ov = jQuery.noConflict();

$ov(document).ready(function(){

	/* this code is here to auto select all text box content on focus */
	$ov("input[type=text]").focus(function() { $ov(this).select(); });
	/* this code is here because Chrome decides it doesn't want to behave the same as IE/Firefox */
	$ov("input[type=text]").mouseup(function(e){ e.preventDefault(); }); 	

	model_nav_bind_events();
	model_nav_dom_inserts();
});

function model_nav_bind_events(){
	$ov('.model_nav_tier1_li').mouseenter(model_nav_tier1_li_MOUSE_ENTER);
	$ov('.model_nav_tier1_li').mouseleave(model_nav_tier1_li_MOUSE_LEAVE);
	$ov('.model_nav_tier2_li').mouseenter(model_nav_tier2_li_MOUSE_ENTER);
	$ov('.model_nav_tier2_li').mouseleave(model_nav_tier2_li_MOUSE_LEAVE);
	$ov('.model_nav_tier2_a').mouseenter(model_nav_tier2_a_MOUSE_ENTER);
	$ov('.model_nav_tier1_a').mouseenter(model_nav_tier1_a_MOUSE_ENTER);
	$ov('.model_nav_tier3_a').mouseenter(model_nav_tier3_a_MOUSE_ENTER);
	$ov('.model_nav_tier3_a').mouseleave(model_nav_tier3_a_MOUSE_LEAVE);
}

function model_nav_dom_inserts(){
	if(browserCheck(8)){
		var model_nav_tier1_li_cap = '<div class="model_nav_tier1_li_cap">';
    	model_nav_tier1_li_cap += '<img src="/StyleSite/ORV/2012/images/model_nav_tier1_li_cap_img.png" class="model_nav_tier1_li_cap_img" height="5" width="5" />';
        model_nav_tier1_li_cap += '</div>';
		$ov('.model_nav_tier1_a').before(model_nav_tier1_li_cap);
		
		/*var model_nav_tier1_li_corner_img = '<img src="/StyleSite/ORV/2012/images/model_nav_tier1_li_corner_img.png" class="model_nav_tier1_li_corner_img" height="9" width="9" />';
		$ov('.model_nav_tier2_ul').after(model_nav_tier1_li_corner_img);
		*/
	}else{
		$ov('.model_nav_tier2_a').width(150);
	}
	
	//var model_nav_tier1_li_divider = '<img src="/Style%20Library/Polaris/ORV/2011/model_nav_tier1_li_divider.gif" height="8" width="4" class="model_nav_tier1_li_divider" />';
	//$ov('.model_nav_tier1_li:not(:last-child)').after(model_nav_tier1_li_divider);
	
	//var model_nav_tier1_ul_cap_left = '<img src="/Style%20Library/Polaris/ORV/2011/model_nav_tier1_ul_cap_left.png" height="32" width="32" class="model_nav_tier1_ul_cap_left" />';
	//$ov('.model_nav_tier1_li:first-child').before(model_nav_tier1_ul_cap_left);
	
	//var model_nav_tier1_ul_cap_right = '<img src="/Style%20Library/Polaris/ORV/2011/model_nav_tier1_ul_cap_right.png" height="32" width="32" class="model_nav_tier1_ul_cap_right" />';
	//$ov('.model_nav_tier1_li:last-child').before(model_nav_tier1_ul_cap_right);
}

function model_nav_tier2_li_MOUSE_ENTER(){
	var myWidth = $ov(this).parent().width();
	$ov('.model_nav_tier3_ul', this).css("left", myWidth);

	/* if last item in ul list, set top of flyout so bottoms line up */
	if ($ov(this).is(':last-child') && $ov(this).index() > 1 ){
		$ov('.model_nav_tier3_ul', this).css("top", '-137px');
	}

	var numChildren = $ov('.model_nav_tier3_ul', this).children().length;
	var colWidth = 3;
	var colHeight = 1;
	switch(numChildren){
		case 0:
			colWidth = 0;
			return;
		case 1:
			colWidth = 1;
			break;
		case 2:
			colWidth = 2;
			break;
		case 3:
			colWidth = 3;
			break;
		case 4:
			colWidth = 2;
			break;
		case 5:
			colWidth = 3;
			break;
		case 6:
			colWidth = 3;
			break;
		case 7:
			colWidth = 3;
			break;
		case 8:
			colWidth = 3;
			break;
		case 9:
			colWidth = 3;
			break;
		default:
			colWidth = 3;
			break;
	}
	var childWidth = parseInt($ov(".model_nav_tier3_a").css("width"));
	childWidth += parseInt($ov(".model_nav_tier3_li").css("padding-right"));
	childWidth += 2; //borders must be entered manually
	var finalWidth = childWidth * colWidth;
	//var offsetHeight = $ov(this).index() * 10;
	$ov('.model_nav_tier3_ul', this).css({width: finalWidth});
	$ov('.model_nav_tier3_ul', this).stop(true, true);
	$ov('.model_nav_tier3_ul', this).fadeIn("fast");
	
	// Added below
	$ov(".model_nav_tier3_img", this).each(function(index){
		var newSource = $ov(this).attr("imageSrc");
		//alert(newSource);
		$ov(this).attr("src", newSource);
	});
	//
	
}

function model_nav_tier2_li_MOUSE_LEAVE(){
	$ov('.model_nav_tier3_ul', this).stop(true, true);
	$ov('.model_nav_tier3_ul', this).fadeOut("fast");
}

function model_nav_tier1_li_MOUSE_ENTER(){
	$ov('#topNavDiv').css({zIndex: 1000});
	$ov('.model_nav_tier1_a', this).addClass('model_nav_tier1_a_hover');
	$ov('.model_nav_tier1_li_cap', this).show();
	$ov('.model_nav_tier1_li_corner_img', this).show();
	$ov('.model_nav_tier2_ul', this).stop(true, true);
	if($ov('.model_nav_tier2_ul', this).children().length){
		$ov('.model_nav_tier2_ul', this).fadeIn("fast");
	}
}

function model_nav_tier1_li_MOUSE_LEAVE(){
	$ov('#topNavDiv').css({zIndex: 5});
	$ov('.model_nav_tier1_a', this).removeClass('model_nav_tier1_a_hover');
	$ov('.model_nav_tier1_li_cap', this).hide();
	$ov('.model_nav_tier1_li_corner_img', this).hide();
	$ov('.model_nav_tier2_ul', this).stop(true, true);
	$ov('.model_nav_tier2_ul', this).fadeOut("fast");
}

function model_nav_tier2_a_MOUSE_ENTER(){
	$ov(".model_nav_tier2_a").removeClass("model_nav_tier2_a_hover");
	$ov(this).addClass('model_nav_tier2_a_hover');
}

function model_nav_tier1_a_MOUSE_ENTER(){
	$ov(".model_nav_tier2_a").removeClass("model_nav_tier2_a_hover");
}

function model_nav_tier3_a_MOUSE_ENTER(){
	$ov(this).addClass('model_nav_tier3_a_hover');
}

function model_nav_tier3_a_MOUSE_LEAVE(){
	$ov(this).removeClass('model_nav_tier3_a_hover');
}

function browserCheck(ieVersion){
	if($ov.browser.msie && $ov.browser.version < ieVersion){
		return false;
	}else{
		return true;
	}
}
