var tempDivId="";
var isIE6 = $.browser.msie && $.browser.version.substr(0,1)<7;
var isSafari = $.browser.safari;

$(document).ready(function(){
var config = {    
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
     interval: 5, // number = milliseconds for onMouseOver polling interval    
     over: showMenu, // function = onMouseOver callback (REQUIRED)    
     timeout: 0, // number = milliseconds delay before onMouseOut    
     out: hideMenu // function = onMouseOut callback (REQUIRED)       
};

if (isIE6) {
//	$(".models_navi_container ul.topnav li h3").mouseenter( function(){showMenu1($(this).attr('id'));});
//	$(".services_navi_container ul.topnav li h3").mouseenter( function(){showMenu1($(this).attr('id'));});
} else {
//	$(".models_navi_container ul.topnav li h3").hoverIntent( config );
//	$(".services_navi_container ul.topnav li h3").hoverIntent( config );
}


$(".models_navi_container ul.topnav li h3").bind('click', function(){showMenu1($(this).attr('id'));});
$(".services_navi_container ul.topnav li h3").bind('click', function(){showMenu1($(this).attr('id'));});


function showMenu(){
	showMenu1($(this).attr("id"));
}

function showMenu1(_id){
	var qtID =  _id;
	var elementID = ".dropdown_wrapper .dropdown ." + qtID;	
	
	if (tempDivId != '') {
		$('div').find(tempDivId).hide();
	}

	if($('div').find("div .qtvr_box").length > 0){
		$('div').find("div .qtvr_box").hide();
	}
	tempDivId = elementID;

	if (isIE6 && $('select#year').length > 0) {	
		$('div').find("div .year select#year").css("visibility", "hidden");
		$('div').find("div .cate select#category").css("visibility", "hidden");
	}
	if (isIE6 && $('select#selectedState').length > 0) {	
		$('div').find("div .field select#selectedState").css("visibility", "hidden");
		$('div').find("div .field select#selectedCity").css("visibility", "hidden");
	}
	
	if (isSafari){ 
		$('div').find("div .dropdown").height("387px");  //set height for div dropdown, so that hover event works fine for safari
	}
	
	if (isIE6) {
		$('div').find(elementID).show();
	} else {
		$('div').find(elementID).fadeIn("fast");
	}
	
}

function hideMenu(){
    if (isSafari){ 
		$('div').find("div .dropdown").height("0px");  //set height for div dropdown, so that hover event works fine for safari
	}
}

function hideCarMenuDiv(id) {
	hideMenu();
	$('div').find(id).fadeOut("fast");
	
	if($('div').find("div .qtvr_box").length > 0){
		$('div').find("div .qtvr_box").show();
	}
	
	if (isIE6 && $('select#year').length > 0) {		
        $('div').find("div .year select#year").css("visibility", "visible");
        $('div').find("div .cate select#category").css("visibility", "visible");
	}
	
	if (isIE6 && $('select#selectedState').length > 0) {	
		$('div').find("div .field select#selectedState").css("visibility", "visible");
		$('div').find("div .field select#selectedCity").css("visibility", "visible");
	}
}

$('div').find(".dropdown_wrapper .dropdown .sedancompact").mouseleave( function() {
	hideCarMenuDiv(".dropdown_wrapper .dropdown .sedancompact");
});

$('div').find(".dropdown_wrapper .dropdown .hybrid").mouseleave( function() {
	hideCarMenuDiv(".dropdown_wrapper .dropdown .hybrid");
});

$('div').find(".dropdown_wrapper .dropdown .van").mouseleave( function() {
	hideCarMenuDiv(".dropdown_wrapper .dropdown .van");
});

$('div').find(".dropdown_wrapper .dropdown .suv4wd").mouseleave( function() {
	hideCarMenuDiv(".dropdown_wrapper .dropdown .suv4wd");
});

$('div').find(".dropdown_wrapper .dropdown .mpv").mouseleave( function() {
	hideCarMenuDiv(".dropdown_wrapper .dropdown .mpv");
});

$('div').find(".dropdown_wrapper .dropdown .toyota-careplus").mouseleave( function() {
	hideCarMenuDiv(".dropdown_wrapper .dropdown .toyota-careplus");
});

$('div').find(".dropdown_wrapper .dropdown .sales-service-locator").mouseleave( function() {
	hideCarMenuDiv(".dropdown_wrapper .dropdown .sales-service-locator");
});

$('div').find(".dropdown_wrapper .dropdown .toyota-owners").mouseleave( function() {
	hideCarMenuDiv(".dropdown_wrapper .dropdown .toyota-owners");
});

if (jQuery.browser.msie) {
	// thankyou_msg for Altis taking order
	//if ($("div[id^='thankyou_msg']")){
	//	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'altis_taking_order'});
	//}
	if(document.getElementById('thankyou_msg')){
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'altis_taking_order'});
	}
	else{
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded_noclose'});
	}
} else {
	// thankyou_msg for Altis taking order
	if(document.getElementById('thankyou_msg')){
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'altis_taking_order'});
	}
	else{
		$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded_noclose'});
	}
}

$("#hidePanel").click(function(){
	collapseSelectionPanel();
});
 
 
$("#showPanel").click(function(){
	openSelectionPanel();
});
});

function hideSelectionPanel(){ // meant to hide panel upon loading
	$("#panel").animate({marginLeft:"-300px"}, 500 );
	$("#colleft").animate({width:"0px", opacity:0}, 400 );
	$("#showPanel").hide("normal");
	$("#colright").animate({marginLeft:"0px"}, 300);
}

function collapseSelectionPanel(){    // meant for user interaction, to show or to collapse
	$("#panel").animate({marginLeft:"-300px"}, 500 );
	$("#colleft").animate({width:"0px", opacity:0}, 400 );
	$("#showPanel").show("normal").animate({width:"43px"}, 200);
	$("#colright").animate({marginLeft:"0px"}, 300);
}

function openSelectionPanel(){
	
	$("#panel").animate({marginLeft:"0px"}, 400 );
	$("#colleft").animate({width:"300px", opacity:1}, 400 );
	$("#showPanel").animate({width:"0px"}, 600).hide("slow");
	$("#colright").animate({marginLeft:"300px"}, 200);
}

function closeLightbox(){
	$.prettyPhoto.close();
}

function openLightbox(url, title, description){
	$.prettyPhoto.open(url, title, description);
}

function tellAFriend(link){
	openPopupwindow('/tell-a-friend/index.dot?link='+link, 'width=700,height=550,scrollbars=yes');
}

function openPopupwindow(link, params){
	if (jQuery.browser.msie) {
		params = params.replace('height=', 'dialogHeight:');
		params = params.replace('width=', 'dialogWidth:');
		if (params.indexOf('scrollbars=yes') > 0) {
		params = params.replace(',scrollbars=yes', '');
		} else {
		params = params.replace(',scrollbars', '');
		}
		params = params.replace(',', ';');
		params = params.replace(';', 'px;');
		params += 'px';
		window.showModalDialog(link,'','center:1;'+params);
	} else {
	var newwin = window.open(link, 'popupWin', params);
	if (window.focus) {newwin.focus()}
	}
}

function getURLParam(strURL, strParamName){
  var strReturn = "";
  var strHref = strURL;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

/* this function calls openPopupwindow after extracting the height and width from the parameter passed in*/
function openPopupwindow2(strURL) {
	var strParam = 'width=';
	strParam += getURLParam(strURL, 'width');
	strParam += ',height=';
	strParam += getURLParam(strURL, 'height');
	strParam += ',scrollbars=yes';
	openPopupwindow(strURL, strParam);
}

function resizeFlash(width, height){
	var flashObj = document.getElementById("flashId");
	var flashEmbed = document.getElementsByName("flashId").item(0);
	
	if(flashObj != null)
	{
		flashObj.width = width;
		flashObj.height = height;
	}
	
	flashEmbed.width = width;
	flashEmbed.height = height;
	
	alert(height);
}