/*jslint strict:true, evil: true, onevar: true, nomen: false  */ /*global $, jQuery, console, hbx, window, ndm, document, alert, setInterval, clearInterval */

var CG_activeHover,
    CG_cur_hover = 1,
    CG_animateCarousel;

CG_animateCarousel = function(){
    var curHvrId = CG_activeHover;
    if(CG_cur_hover >= 5) CG_cur_hover = 1;
    var nxtHvrId = "CG_homepage_hovers_"+CG_cur_hover;

    /* Fade out active - fade in new */
    $("#"+curHvrId+" span.CG_hover_main img").fadeOut("slow");
    $("#"+curHvrId).removeClass("CG_hover_active CG_hover_active_js");
    $("#"+nxtHvrId).addClass('CG_hover_active CG_hover_active_js');
    $("#"+nxtHvrId+" span.CG_hover_main img").fadeIn("slow");
    
    CG_activeHover = nxtHvrId;
    CG_cur_hover++;
};

$(document).ready(function(){
    var CG_animateTime;

    CG_activeHover = $(".CG_hover_active").eq(0).attr("id");

    $("#CG_homepage_cabaret li.CG_homepage_hover").hover(
        function(){
            clearInterval(CG_animateTime);
            if($(this).attr("id") !== CG_activeHover){
                /* Fade out */
                $(".CG_hover_active_js span.CG_hover_main img").fadeOut("fast");
                $(".CG_hover_active_js").removeClass("CG_hover_active_js");
                $(".CG_hover_active").removeClass("CG_hover_active");
                /* Fade in */
                $(this).addClass("CG_hover_active CG_hover_active_js");
                $(".CG_hover_active_js span.CG_hover_main img").fadeIn("fast");
                CG_activeHover = $(this).attr("id");
            }
        }
    );
    
    CG_activeHover = "CG_homepage_hovers_"+CG_cur_hover;
    
    // start - clear styles for images
    $(".CG_homepage_hover span.CG_hover_main img").fadeOut("fast");
    $(".CG_homepage_hover span.CG_hover_main img").removeClass("CG_hover_active CG_hover_active_js");
    // end - clear styles for images
    
    $("#"+CG_activeHover).addClass('CG_hover_active CG_hover_active_js');
    $("#"+CG_activeHover+" span.CG_hover_main img").fadeIn(800);
    CG_cur_hover++;

    CG_animateTime = setInterval('CG_animateCarousel()',4000);

    // Petrol prices search box
    if ($("#CG_petrol_search_form").length > 0) {
        $.ajax({
            url:"/javascript/screenspecific/CG_postcodes.js",
            dataType: "script",
            success: function(){ postCodesLoaded = true; },
            cache: true
        });

      $("#CG_petrol_search_form").submit(function(){
        $(this).CG_getCurrentPostcode();
        return false;
      });
    }
});

/* START - PETROL PRICES WIDGET FUNCTIONS */
var urlLocation = "/tools-and-advice/petrol-prices/",
    curURL = urlLocation,
    curQuery = document.location.search,
    curPostcode;

jQuery.fn.CG_getCurrentPostcode = function(formPost){
    var curPCodeInt,
        curLocation = $("#location").val().toString(),
        validShortPostcode = false,
        curFtype = $("select#fuelType option:selected").val();
        
    $(document).CG_formStationValue();

    curPostcode = "";
    if(curLocation === ""){
        // The field is blank
        alert("Please supply a postcode to search for petrol.");
        $("#CG_postcode").val();
    }else{
        if(curLocation.length === 3 && curLocation.substring(0,1) === "8"){
            validShortPostcode = true;
        }

        if(curLocation.length < 4 && !validShortPostcode){
            alert("Please enter a valid postcode.");
            $("#CG_postcode").val();
        }else{
            curPCodeInt = parseInt(curLocation,10);
            // If the current location is an number set it as the current postcode
            if(curPCodeInt.toString() !== "NaN"){
                if(curLocation.length > 4){
                    alert("Location not found. Please try another suburb or postcode.");
                }else{
                    curPostcode = curLocation;
                    urlQuery = "?stations="+stationsVal+"&fuelType="+curFtype+"&postcode="+curPostcode;
                    document.location.href = urlLocation + urlQuery;
                }
            }else{
                // They have typed in a suburb name. Do a postcode lookup and then perform the query
                $.getScript("http://www.carsguide.com.au/tools-and-advice/petrol-prices-lookup/"+curLocation.toLowerCase());
            }
        }
    }
};

/* This function is used to find a postcode if the user enters a suburb name */
lookupJSON = function(args){
    var text = "",
        pcodeSearch,
        i=0;

    switch(args.ResultData.ResultCode){
        case "0":
            alert("Location not found. Please try another suburb or postcode.");
            break;
        case "1":
            curPostcode = jQuery.trim(args.ResultData.Data[0].postcode);
            curLocation = $("#location").val().toString(),
	    curFtype = $("select#fuelType option:selected").val();
	    urlQuery = "?stations="+stationsVal+"&fuelType="+curFtype+"&suburb="+curLocation+"&postcode="+curPostcode;
            document.location.href = urlLocation + urlQuery;
            break;
        case "2":
            text = "Did you mean: <br />";
            for (i=0 ; i<args.ResultData.Data.length ; i++){
                pcodeSearch = "CG_selectPostcode('"+args.ResultData.Data[i].postcode+"')";
                text = text + '<a href="javascript:'+pcodeSearch+'">';
                text = text + args.ResultData.Data[i].name + " " + args.ResultData.Data[i].state + " " + args.ResultData.Data[i].postcode;
                text = text + "</a>; ";
            }
            $("#did_you_mean").html(text);
            $("#did_you_mean").css("display","block");
            break;
    }
};

jQuery.fn.CG_formStationValue = function(){
    var allStations = true;
    stationsVal = "111111";
    stationsString = "All stations";
};

function CG_selectPostcode(pCde){
    var curFtype = $("select#fuelType option:selected").val(), urlQuery, urlLocation;

    curPostcode = pCde;

    $(document).CG_formStationValue();
    urlQuery = "?stations="+stationsVal+"&fuelType="+curFtype+"&suburb="+$("#location").val().toString()+"&postcode="+pCde;

    if(curQuery.length > 0){
        urlLocation = curURL.substring(0,curURL.indexOf("?"));
    }else{
        urlLocation = curURL;
    }
    document.location.href = urlLocation + urlQuery;
}
/* END - PETROL PRICES WIDGET FUNCTIONS */

