// JavaScript Document
var lastmenu;
var ttTimer;
$(document).ready(function() {
    //homepage element shuffle
    if ($('#content').hasClass('home')) {
        $('#manufacturers-block').insertAfter('#content');
    }
    if ($('#content').hasClass('home')) {
        $('#scrappage').insertAfter('#content');
    }

    //font-replacement
    Cufon.replace('h1', { fontSize: '17px' });
    Cufon.replace('div.usedCars div#secondary-content h1', { fontSize: '19px' });
    Cufon.replace('div#secondary-content h2.specOfferHeader', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div.smsSignUp h2', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div.loyaltyCard h2', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div.bestDeal h2', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div#see-more-block h2', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div#franchise-offers-block h2', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div#call-block h2', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div#side-contact-block h2', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div#side-contact-block h3', { fontSize: '18px' });
    Cufon.replace('div#info-block h2#ctl00_ContentPlaceHolder1_h2Make1', { fontSize: '18px' });
    Cufon.replace('div#info-block h3#ctl00_ContentPlaceHolder1_h3Model', { fontSize: '18px' });
    Cufon.replace('div#secondary-content div#side-contact-block h3', { fontSize: '18px' });
    Cufon.replace('div#primary-content .contact dt.detail', { fontSize: '18px' });
    Cufon.replace('div.usedCars div.dealer-details div#dealer-logo h2', { fontSize: '16px' });
    Cufon.replace('div#secondary-content div.rentalContact dl dt', { fontSize: '16px' });
    Cufon.replace('div#secondary-content div.rentalWidget h3', { fontSize: '16px' });
    Cufon.replace('div.introReview p.modelReviewIntro', { fontSize: '18px' });
    Cufon.replace('div.intro p.modelReviewIntro', { fontSize: '18px' });
    Cufon.replace('#scrappage p', { fontSize: '24px' });
    Cufon.replace('#widgetviewport h2', { fontSize: '16px' });
    Cufon.replace('.widgetviewport h2', { fontSize: '16px' });
    Cufon.replace('body.financeSolutions h1', { fontSize: '16px' });
    Cufon.replace('div.bikes dl.bikeSections dt h2', { fontSize: '20px' });
    Cufon.replace('div#secondary-content div#dvQuestionnaire h2', { fontSize: '18px' });

    //homepage tooltips
    $('#manufacturer-logos li a').hover(function() {
        clearTimeout(ttTimer);
        var make = $(this).parent('li').attr('class').toUpperCase();
        $('.tooltipContainer').hide();
        $('.ttc' + make).show();
    }, function() {
        var make = $(this).parent('li').attr('class').toUpperCase();
        ttTimer = setTimeout(function() {
            $('.ttc' + make).hide();
        }, 250);
    });
    $('.tooltipContainer').hover(function() {
        clearTimeout(ttTimer);
        $(this).show();
    }, function() {
        $(this).hide();
    });
    // Script to make z-index behave in IE 6 and 7
    if ((jQuery.browser.msie && jQuery.browser.version < 8)) {
        $('a.enlargeImage').live('mouseover', function() {
            $('div.used-car-block, div.colRowBreak, a.enlargeImage').css({ 'z-index': 18 });
            $(this).css({ 'z-index': 25 }).parent('div.used-car-block').css('z-index', 20);
        });
        $('.compare-table tr:first-child').children().css('border-top-style', 'none');
    }
    //hack to override default css hover behaviour
    $('img.thumbnail').live("mouseout", function() {
        var $enlarge = $(this).parent('a').find('.enlarge');
        $enlarge.css('display', 'none');
        setTimeout(function() {
            $enlarge.removeAttr('style');
        }, 50);
    });
    // row/col views
    $('.rowviewicon').live("click", function(e) {
        $('#carResultSet').removeClass('col').addClass('row');
        $('.rowviewicon img').attr('src', '_includes/images/global/rowView1.gif');
        $('.colviewicon img').attr('src', '_includes/images/global/colView0.gif');
        e.preventDefault();
    });
    $('.colviewicon').live("click", function(e) {
        $('#carResultSet').removeClass('row').addClass('col');
        $('.rowviewicon img').attr('src', '_includes/images/global/rowView0.gif');
        $('.colviewicon img').attr('src', '_includes/images/global/colView1.gif');
        e.preventDefault();
    });
    $('.dynamic-widget-block ul:first li a').bind("click", function(e) {
        var $target = $($(this).attr('href'));
        $target.siblings('div').slideUp('fast', function() {
            $target.slideDown('fast');
        });
        $(this).parent('li').addClass('current').siblings().removeClass('current');
        e.preventDefault();
    });
    $('.dynamic-widget-viewport div:first').siblings('div').hide();

    $('a.review-read', 'div').click(function() {

        var reviewId = $(this).attr("title");
        var reviewText = $('div.review-body-' + reviewId).html();

        $('div.holder').html(reviewText);

        return false;
    });
});
function showHide(x,y)
{
    document.getElementById(x).style.display='inline'
    document.getElementById(y).style.display='none'
}
function showMenu(dv)
{
    if(lastmenu)
    {
        if($('#dv' + lastmenu))
        {
            $('#dv' + lastmenu).hide();
            $('#dv' + lastmenu).attr('class','secondary-navigation under');
        }
        if($('#liMenu' + lastmenu).hasClass('selected'))
		{
            $('#liMenu' + lastmenu).attr('class','');
		}else{
            $('#liMenu' + lastmenu).attr('class','last');
		}
    }
    $('#dv' + dv).show();
    $('#dv' + dv).attr('class','secondary-navigation over');
    $('#liMenu' + dv).attr('class','selected');
    lastmenu=dv;
}
function clearInput(inputItem) {
/*     if (inputItem.value == inputItem.title) {
         inputItem.value = ""
     }*/
 }
function resetit(inputItem) {
/*     if (inputItem.value == "") {
         inputItem.value = inputItem.title
     }*/
 }
 //input masks
 // add
 // defaultvalue="something" onFocus="maskFocus(this)" onBlur="maskBlur(this)"
function maskFocus(el){
	var me = $(el);
	if(me.val()==me.attr('title')){
		me.val('');
	}
 }
 function maskBlur(el){
	var me = $(el);
	if(me.val()==''){
		me.val(me.attr('title'));
	}
 }

var aSelectedLink,sCross
function getCalender(move,txtDate)
{	
    	tb=document.getElementById("calenderTable")
		if(txtDate!='')
		{
		    if($get(txtDate).value!="")
		    {
		        aDate=$get(txtDate).value.split('/')
    			today=new Date(aDate[2],aDate[1]-1,aDate[0])
		    }
		    else
			    today=new Date()
			txtDateCtrl=txtDate
		}
		var cal_days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		cal_months_labels = ['January', 'February', 'March', 'April','May', 'June', 'July', 'August', 'September','October', 'November', 'December'];
	    
		var month=today.getMonth();
		var iYear=today.getFullYear();
		
		if(move) 
		{
			month+=move
			if(month==-1)
			{
				iYear-=1;month=11
			}
			if(month==12)
			{
				iYear+=1;month=0
			}
			today.setMonth(month)
			today.setFullYear(iYear)
		}

		var iFirstDayOfWeek=(new Date(iYear,month,1)).getDay()+5;
		if(iFirstDayOfWeek>5)iFirstDayOfWeek-=7
		var iLastDay = cal_days_in_month[month];
		if (month == 1) {if(iYear % 4==0){iLastDay = 29;}}

		tdMY=document.getElementById("tdMonthYear")
		tdMY.innerHTML=cal_months_labels[today.getMonth()]+' '+today.getFullYear()
		var x='';
		for(var i=0;i<6;i++)
		{
			for(var j=0;j<7;j++)
			{
				var iDay=i*7+j-iFirstDayOfWeek;
				if(iDay>0 && iDay<=iLastDay)
				{
					var sDay=iDay.toString();
					if(iDay<10)sDay="0"+sDay;
					var sMonth=(month+1).toString();
					if(month<9)sMonth="0"+sMonth;
                    if(sCross!="")
                    {
                        if((new Date(iYear,month,iDay+1))>(new Date()))
                        {
						    tb.rows[i+2].cells[j].innerHTML= "<a id='a"+i+j+"' href=\"javascript:setDate('"+sDay+"/"+sMonth+"/"+iYear+"','a"+i+j+"')\">"+iDay+"</a>"; //class="selected"
                            tb.rows[i+2].cells[j].className=""
						}
                        else
                        {
                            tb.rows[i+2].cells[j].className=sCross
						    tb.rows[i+2].cells[j].innerHTML= iDay
                        }
                    }
                    else
                    {
					    if(today.getDate()==iDay)
					    {
						    tb.rows[i+2].cells[j].innerHTML= "<a id='a"+i+j+"' class='selected' href=\"javascript:setDate('"+sDay+"/"+sMonth+"/"+iYear+"','a"+i+j+"')\">"+iDay+"</a>"; //class="selected"
					        aSelectedLink=document.getElementById('a'+i+j)
					    }
					    else
						    tb.rows[i+2].cells[j].innerHTML= "<a id='a"+i+j+"' href=\"javascript:setDate('"+sDay+"/"+sMonth+"/"+iYear+"','a"+i+j+"')\">"+iDay+"</a>"; //class="selected"
                    }					
				}
				else
				{
					tb.rows[i+2].cells[j].innerHTML= "";
                    tb.rows[i+2].cells[j].className=""
                 }
			}
		}
	
}
function setDate(sDate,me)
{   
    if(aSelectedLink!=undefined)aSelectedLink.className=''
    aSelectedLink=document.getElementById(me)
    aSelectedLink.className='selected'
	document.getElementById(txtDateCtrl).value=sDate
}
function showNewCarReview(dv)
{
    document.getElementById('dvOption').style.display='none';
    document.getElementById('dvReview').removeAttribute("style")
}
function checkEmailOrTelephone(source, args){
  if (document.getElementById(email_ClientID).value=="" && document.getElementById(telephone_ClientID).value==""){
   args.IsValid = false;
  }   else {
  args.IsValid = true;
  }
 }
function calcTotal()
{
var sOpt=''
var total=0
opt=document.getElementsByTagName('input')
for(i=0;i<(opt.length);i++)
	if(opt[i].checked)
	if(!isNaN(parseFloat(opt[i].value)))
	{
	//total+= parseFloat(opt[i].Price)
	total+= parseFloat(opt[i].value)
	sOpt+=opt[i].getAttribute('optionname')+','
	}
var Price=parseFloat(document.getElementById("ctl00_ContentPlaceHolder1_ltPrice").innerHTML.substring(1).replace(",",""))
document.getElementById("ctl00_ContentPlaceHolder1_txtOpt").value=sOpt	
document.getElementById("ctl00_ContentPlaceHolder1_aEnquiry").href= document.getElementById("ctl00_ContentPlaceHolder1_aCompare").href.replace("NewCarCompare.aspx","new-car-enquiry.aspx")+ "&Option="+sOpt+"&OptionCost="+total.toString()
document.getElementById("ctl00_ContentPlaceHolder1_txtOptPrice").value='&pound;'+CommaFormatted(total.toString())
document.getElementById("ctl00_ContentPlaceHolder1_ltOptionPrice").innerHTML=document.getElementById("ctl00_ContentPlaceHolder1_txtOptPrice").value
document.getElementById("ctl00_ContentPlaceHolder1_ltTotalPrice").innerHTML='&pound;'+CommaFormatted((total+Price).toString())
document.getElementById("ctl00_ContentPlaceHolder1_txtTotalPrice").value = document.getElementById("ctl00_ContentPlaceHolder1_ltTotalPrice").innerHTML

var exchRate = document.getElementById("ctl00_ContentPlaceHolder1_txtExchRate").value;
var priceInEuros = '&euro;' + CommaFormatted((Math.floor(((total + Price) * exchRate))).toString()) + '**';
var totalInEuros = '&euro;' + CommaFormatted((Math.floor((total.toString() * exchRate))).toString()) + '**';
//Euros
if (document.getElementById("ctl00_ContentPlaceHolder1_ltOptionPriceInEuros")) {
	document.getElementById("ctl00_ContentPlaceHolder1_ltOptionPriceInEuros").innerHTML = totalInEuros; //document.getElementById("ctl00_ContentPlaceHolder1_txtOptPrice").value * exchRate
	document.getElementById("ctl00_ContentPlaceHolder1_ltTotalPriceInEuros").innerHTML = priceInEuros
	document.getElementById("ctl00_ContentPlaceHolder1_ltTotalPrice1InEuros").innerHTML = priceInEuros
}
} 
function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d!='undefined') { amount = n; }
	else { amount = n + '.' + d; }
	amount = amount;
	return amount;
}
var dvNewsContent
function showNewsContent(dv)
{
if(dvNewsContent) dvNewsContent.style.display="none"
dvNewsContent=document.getElementById(dv)
dvNewsContent.style.display="block"
//dvNewsContent.removeAttribute("style")
}
function hideNewsContent()
{
dvNewsContent.style.display="none"
}
 function switchBanners(banner1, banner2){
	var b1=$('#banner1');
	var b2=$('#banner2');
	
	var b1style=b1.css('display');
	var b2style=b2.css('display');

	if (b1style == b2style) {
		if (Math.floor(Math.random() * 2) == 0) {
			b1.show();
			b2.hide();
		} else {
			b1.hide();
			b2.show();
		}
	} else {
		b1.css('display', b2style);
		b2.css('display', b1style);
	}

	setTimeout("switchBanners('" + banner1 + "', '" + banner2 + "')", 5000);
	
}