String.prototype.contains = function(it) { return this.indexOf(it) != -1; };


$(function () {
	$('#other-areas-info').hide();
	$('#staff-link').parent().hover(function(){
		$(this).children('ul').toggle();
	});
	
    $.fn.highlightNavigation(); // This method should come before imageRollover method because it remove 'rollover' class used in imageRollover method.

    $('#banners').cycle({ timeout: 9000, slideExpr: 'img', pause: true });

    path = $.url.attr("path");
    $('#left-menu ul li a[href=' + path + ']').addClass('selected');

});

$.fn.highlightNavigation = function () {
    section = $.url.segment(0);

    if (section === null) {
        section = 'Home';
    }

    if ($.url.segment(1) != null) {
        switch ($.url.segment(1).replace('.aspx', '')) {
            case 'car-financing-faq':
                section = 'Questions';
                break;
            case 'car-finance-rates-fees-and-charges':
                section = 'Questions';
                break;
            case 'contact':
                section = 'Contact';
                $.fn.contact();
                break;
            case 'Terms':
                section = 'none';
            case 'car-loan-finance-enquiry':
                section = 'Contact';
                break;
            case 'dealer-search':
                section = 'Contact';
                break;                
            case 'calculator':
                section = 'Loans';
                break;                
            case 'promotions':
                section = 'Promotions';
                break; 
        }
    }


    $('#' + section).addClass('selected');

};

$.fn.contact = function()
{
    var ctx = $('#contacts')[0];
    var map = $('#map');

    $('#google-map').dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        top: 100,
        height: 550,
        width: 445,
        buttons: { "Close": function () { $(this).dialog('close'); } }
    });

    $('#map p').mouseover(function () {

        $(this).toggleClass('map-p-hover');
        map.find('div').fadeOut("fast");
        $(this).next('div').toggle();

    });

    $('#map').delegate('p, li', "click", function () {

		try
		{
		
			var Id = $(this).children().first().attr('id');
			//var Id = $(this).children()[0].id;
			if (Id.toString() != '')
			{
				var Url = '/Home/Contact.aspx?id=' + Id + '&location=dummy #lblDealerContent';
				$('h1').text($(this).text());
				$('#contacts').load(Url).show();
				//$('#other-areas p:first').trigger('click', ["region"]);
				map.toggle();
				$('#other-areas p:last').text('Search in another location');
				var href = $('#enquiry-button').attr('href');
				if (href.contains("?")) { href = href.substring(0,href.indexOf("?")) };
				$('#enquiry-button').attr('href', (href + '?id=' + Id));
				var address = $('.street-map').attr('title');
				$('#street-map').data('map-address', address);
			
				if (Id == '2180')
				{
					$('#351579').remove();
				}
			}
		}
		catch (ex)
		{
			console.error(ex.message + ' ' + ex.number);
		}
    });


    $('#other-areas p:last').click(function (event, e) {
		
		map.toggle();
		if(e != "region")
		{
			$(this).next().toggle();
			$(this).parent(".float-right").removeClass('float-right');
		}
        if(map.is(':visible'))
        {
			$('#other-areas-info').hide();
			$(this).text('All other areas click here').not(".float-right").addClass("float-right");
			$('h1').text('Click your nearest MTF location');
			$('#lblDealerContent').hide();
        }
        else
        {
			$(this).text('All other areas click here').not(".float-right").addClass("float-right");
			$('#other-areas-info').show();
			$('h1').text('MTF head office');
			$(this).text('Search in another location');
        }
    });


    $('#article').delegate(".street-map", "click", function () {
        $('#google-map').dialog({ title: 'MTF ' + $('#cityname').text() }).dialog('open');

		var postal = $('#street-map').attr('title').replace(/( )+/, '+') + '+New+Zealand';
		var alt = $('#street-map').attr('name');
        var mapRef = '';
                
        if (alt.toString() == "") {
			mapRef = "f=q&source=s_q&hlen&geocode=&q=" + postal;
		}
		else {
			mapRef = alt;
		}
		
        var srcUrl = 'http://maps.google.co.nz/maps?' + mapRef + '&output=embed';

        $('#map-region').empty().append('<iframe id="map-iframe" src="' + srcUrl + '" height="350" width="425" frameborder="0"><iframe>');
        var iframes = $('#map-region iframe');
        if(iframes.length > 1)
        {
			$(iframes).last().remove();
        }
    });

}
