﻿(function($) { // start our namespace 
  /*
   * Function for all the sIFR on the site
   */
  function sIFR(){
    $('body #landing-hero-box h1,'+
        'body #discovery-landing .detail-tabs .content h3,'+
        'body #home-page .detail-tabs .content h3,' +
        'body #context-links h3,' +
        /*'body ul.teaser-words li,'+*/
        'body #content-page h1,'+
        'body #news-landing h1,' +
        'body #sponsor-root h1,' +
        'body #error-page h1,' +
        'body #members-page h1').flashText({
      src: BASE_URL + '/swf/CenturyGothic_Regular.swf' //Century Gothic
    });
  }

  /*
   * Setup page + attach event handlers
   */
  $(document).ready(function() {
    sIFR();
  });

})(jQuery);  // end namespace



/****************************************
*** AJAX Function Handlers
****************************************/

function loadVehicleModels(make, dropDownID, hiddenFieldID, type, defaultDropDownValue) {

    // If the defaultDropDown is null, set it to empty string
    if (defaultDropDownValue == null) {
        defaultDropDownValue = "";
    }

    if (make != null &&
        make != "") {
        // Set the loading animation
        $(".loading-anim").html("<img src=\"/i/ajax_loading.gif\" alt=\"\" />");

        // Load the response from the ajax handler.
        $.get("/ajax_handler.aspx?f=advert_select_model&p="+type+"," + make, function(data) {

            // add list items to the drop list
            addListItems(dropDownID, hiddenFieldID, data, 'default');

            // Set the value of the dropdown list
            setDropDownListValue(dropDownID, defaultDropDownValue);

            // Clear the loading animation
            $(".loading-anim").html("");

        });
    }
    else {
        addListItems(dropDownID, hiddenFieldID, '', 'default');
    }
}

function loadStatesFromCountry(country, dropDownID, hiddenFieldID, defaultDropDownValue) {

    // If the defaultDropDown is null, set it to empty string
    if (defaultDropDownValue == null) {
        defaultDropDownValue = "";
    }

    // Set the loading animation
    $(".loading-anim").html("<img src=\"/i/ajax_loading.gif\" alt=\"\" />");

    // Load the repsonse from the ajax handler
    $.get("/ajax_handler.aspx?f=select_country_states&p=" + country, function(data) {

        // Split the Response Data into an array
        var states = data.split(",");

        // add list items to the drop list
        addListItems(dropDownID, hiddenFieldID, data, 'default');

        // Set the value of the dropdown list
        setDropDownListValue(dropDownID, defaultDropDownValue);

        // Clear the loading animation
        $(".loading-anim").html("");

    });

}

// adds list items to a drop down list based on data and format provided
function addListItems(dropDownID, hiddenFieldID, data, format) {

    // Split the response data into an array.
    var splitData = data.split("+,+");

    // Remove any previous dropdown items
    document.getElementById(dropDownID).options.length = 0;

    // Loop through each element in the array
    for (i = 0; i < splitData.length; i++) {

        // now we want to split on pipe
        var itemData = splitData[i].split("+|+");

        // make sure we have data
        if (itemData.length < 2) {
            continue;
        }

        // Create a new option object, set the name and value, and add it to the auto models dropdown list.
        var listItem = document.createElement("option");
        listItem.value = itemData[0]; // ID
        listItem.text = itemData[1]; // name
        
        // add to drop list
        document.getElementById(dropDownID).options.add(listItem);

        // If its the first Item, add it to the hiddenField, so that a user does not have to select the same option (IE: Default Value)
        if (i == 0) {
            $("#" + hiddenFieldID).val(itemData[0]);
        }

    }
}

// Sets the value of a hidden control
// objectValue: The value to set
// hdnControl: The ID of the control to set the value of
function setHiddenValue(objectValue, hdnControl) {
    // Set the value of the hidden control to the value specified
    $("#" + hdnControl).val(objectValue);

}

// After a post back, we need to re-select the selected country and state values
function reselectStates(country, dropDownID, hiddenFieldID, hiddenFieldValue) {

    // Ensure the country has been selected.
    if (country != "-1") 
    {

        // Reload the country
        loadStatesFromCountry(country, dropDownID, hiddenFieldID, hiddenFieldValue);
        
        // select the drop down item
        setHiddenValue(hiddenFieldValue, hiddenFieldID);
        
    }

}

// After a post back, we need to re-select the selected country and state values
function reselectModels(make, dropDownID, hiddenFieldID, hiddenFieldValue, type) {
    // Ensure the country has been selected.
    if (make != "-1") {

        // Reload the country
        loadVehicleModels(make, dropDownID, hiddenFieldID, type, hiddenFieldValue);

        // select the drop down item
        setHiddenValue(hiddenFieldValue, hiddenFieldID);

    }

}

// Sets the value of the given Drop Down List
function setDropDownListValue(dropDownID, value) {

    // Get the drop down list object
    var dropDownList = document.getElementById(dropDownID);

    // Ensure the options property is not null
    if (dropDownList.options != null) {

        // Select the specific state in the dropdown.
        for (i = 0; i < dropDownList.options.length; i++) {

            // If the current option matches the hiddenFieldValue
            if (dropDownList.options[i].value == value) {

                // Change the selected index to the current option
                dropDownList.selectedIndex = i;

                // no need to continue the loop
                break;

            }

        }

    }

}


function recordItemPhoneClick(itemID) {
    if (itemID != null &&
        itemID != "") {

        // Load the response from the ajax handler.
        $.get("/ajax_handler.aspx?f=record_item_phone_click&id=" + itemID);
    }
}

function recordItemEmailClick(itemID) {
    if (itemID != null &&
        itemID != "") {

        // Load the response from the ajax handler.
        $.get("/ajax_handler.aspx?f=record_item_email_click&id=" + itemID);
    }
}