$(document).ready(function(){

    showRandomOfferPods();

	$("h2 + h3.white").css({"padding-top": "0"});
	
	$("a.video").prettyPhoto({
		padding: 20,
		showTitle: false
	});
	
	$("a[href^='http']").attr('target','_blank');
	$("a.brochure").attr('target','_blank');
	
	fixThirdOfferPod();
	
	
});

function showRandomOfferPods(){
	
	
	
	//Find all the offer pods
	var offerPods = $("div.traffic-driver-col > div.driver");
	
	if(offerPods.length > 0) {
	    //Hide all pods
	    $(offerPods).hide();
    	
	    //Get height of the main content div and use to calculate number of offers to show
	    var the_correct_height = $("#content").height();
	    var podHeight = 290;
	    var numberPods = Math.round(the_correct_height/290);
    	//Make sure don't try to show more than the number of existing pods
    	if(numberPods > offerPods.length){
    	    numberPods = offerPods.length;
    	}
    	
	    var i = 0;
	    for( i; i < numberPods; i++) {
		    var ranPos = Math.floor((Math.random() * offerPods.length));
		    //Show the randomly found pod
		    $(offerPods[ranPos]).show();
		    //Knock that pod out of the array
		    offerPods.splice(ranPos, 1);
	    }
	    
	    //Remove the unused offers from the DOM to avoid IE6 display bugs...
	    var j = 0;
	    for(j; j < offerPods.length; j++) {
	        $(offerPods[j]).remove();
	    }
	    
	}

}

function fixThirdOfferPod() {

    var pods = $("ul.special-offers-listing > li");
    
    if(pods.length > 0) {
        var podcount = pods.length;
        
        for( var i = 1; i <= podcount; i++ )
        {
            var mod = i % 3;
            if(mod == 0){
                $(pods[i - 1]).addClass("last-item");
           }
        }
    }
    
}

function inArray (obj, arr) {
	for(var i = 0; i < arr.length; i++) {
		if(arr[i] == obj){
			return true;
		}
	}
	return false;
}

function correctRightColHeight(){
	var $the_correct_height = $("#content").height();
	var $rightcol_height = $("div.traffic-driver-col").height();
	if($the_correct_height > $rightcol_height){
		$("div.traffic-driver-col").css({height:$the_correct_height+"px"});
	}
}
