// Generic jQuery plugins for Renault Dealer Sites

var root = 'http://renaultdealers.webfactory.ie';

/****************************************************
* Share this function in order to ensure valid html	*
*													*
*													*
*****************************************************/

function initShareThis(){
	
	stLight.options({publisher:'ac658fc2-4705-4e7c-b7a1-336f8d9b58ce'});	
				
	stWidget.addEntry({
		"service":"sharethis",
		"element":document.getElementById('shareThisButton'),
		"url":"http://sharethis.com",
		"title":"sharethis",
		"type":"chicklet",
		"text":"ShareThis"    
		});

		
}

/****************************************************
* set up cufon trigger elements					*
*													*
*													*
*****************************************************/

function initCufon(){
	Cufon.replace('#header .dealer-title h1');
	Cufon.replace('#page-title h1'); 
	Cufon.replace('ul#menu-primary-navigation li a', { hover: true });
	Cufon.replace('#homepage label');	
	Cufon.replace('.cufonize');
	Cufon.now();	
}	

/****************************************************
* Remove right border of last li in primary nav		*
*													*
*													*
*****************************************************/

function initRemoveLastLiBorders(){
	if($('#primary-navigation').length > 0){
		$('#primary-navigation li').last().css('border-right', 'none');
	}
	if($('#footer-navigation').length > 0){
		$('#footer-navigation li').last().css('border-right', 'none');
	}
}

/****************************************************
* Homepage banner fade transition					*
*													*
*													*
*****************************************************/

function initHomepageSlides(){
	setInterval( "slideSwitch()", 5000 );
}

function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


/****************************************************
* Set up the New Cars page							*
*													*
*****************************************************/

function initNewCars(){

	var rangePage = $("#rangepage");
	
	$("#rangepage li.car").live('mouseenter', function(){
		$(this).addClass('liHover');
		var layer = $(this).find('div.rangePageLayer');
		$(layer).removeClass("hidden");		
		$(layer).hide();
		$(layer).css({ "visibility":"visible" });
		$(layer).fadeIn("slow");		
	}).live('mouseleave', function(){
		$(this).removeClass('liHover');
		var layer = $(this).find('div.rangePageLayer');
		$(layer).hide();
	});	;	
	
	
	
	
	/*
	$("#primary-navigation li a, #callback a, .read-more a").mouseleave(function(){			
		$(this).css("background-position","left top");
	});
	*/


}

/****************************************************
* Set up the links for the homepage tiles			*
*													*
*****************************************************/

function initHomepageTileLinks(){

	
	$(".homepage-tile").click(function() {
		url = $(this).find('a.tile-link').attr("href");
		if(url){
			window.location.replace(url);
		}
	});
	

}

/****************************************************
* Return Current Browser Window Size				*
*													*
*****************************************************/

function getBrowserSizeX(){
	var w=0, h=0;
	if(!window.innerWidth){
		if(!(document.documentElement.clientWidth === 0)){
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		} else {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	} else {
		w = window.innerWidth;
		h = window.innerHeight;
	}
	
	return {x:w, y:h};
}

/****************************************************
* Submit Button Rollovers							*
*													*
*****************************************************/

function initFancySubmitHovers(){

	$('.submit input').hover(
		function(){ 
			$(this).attr({ src : root+'/wp-content/themes/renault-dealer-sites/assets/images/forms/btn_submit_on.gif'});
		},
		function(){ 
			$(this).attr({ src : root+'/wp-content/themes/renault-dealer-sites/assets/images/forms/btn_submit.gif'});
		}
	);

}

/****************************************************
* Set up Used Car image selection					*
*													*
*****************************************************/

function initUsedCarImageSelect(){

	$("#used-cars-listing .thumbs a").click(function() {
		url = $(this).attr("href");
		//alert(url);
		$("#mainImg").attr("src",url);
		return false;
	});

}



/****************************************************
* On Page Load Functions							*
*													*
*****************************************************/

function init(){	
	initRemoveLastLiBorders();
	initCufon();
	initShareThis();
	initHomepageSlides();
	initHomepageTileLinks();
	initNewCars();
	initFancySubmitHovers();
	initUsedCarImageSelect();
}





$(document).ready(function(){ init(); });
$(window).load(function() {  });
$(window).unload(function() {  });

