﻿//Main Entry Point for Website
$(function() {

    check_browsers();

    search_init();


    //all hover and click logic for buttons
    var buttons = $(".ui-state-default:not(.ui-state-disabled)");

    buttons.live("mouseover", function() { $(this).addClass("ui-state-hover"); })
    buttons.live("mouseout", function() { $(this).removeClass("ui-state-hover"); })

    //Search: Go to Url
    init_SearchGoToAd();

    //History
    HistoryCarAdClass.createHTML();


});

//jquery : Custom 

//Clear Form
$.fn.clearForm = function() {
    // iterate each matching form
    return this.each(function() {
        // iterate the elements within the form
        $(':input', this).each(function() {
            var type = this.type, tag = this.tagName.toLowerCase();
            if (type == 'text' || type == 'password' || tag == 'textarea')
                this.value = '';
            else if (type == 'checkbox' || type == 'radio')
                this.checked = false;
            else if (tag == 'select')
                this.selectedIndex = -1;
        });
    });
};



function search_init() {

    var ddlcom = $("#ddlCompany");
    var btnS = $("#btnSearch");

    $("#ddlCar").attr("disabled", "disabled");
    btnS.attr("disabled", "disabled");
    btnS.click(search_byCar);
    ddlcom.change(search_onChangeCompany);


    var queryCompanyCookie = getCookie_QueryCompany();

    //load selected car if available
    if (queryCompanyCookie) {

        ddlcom.val(queryCompanyCookie);
        ddlcom.change();
    }
    else {
        //else : reset to Defaults
        ddlcom.attr("selectedIndex", 0);

    }

}

function search_onChangeCompany() {

    var ddlC = $("#ddlCar");
    var comId = $(this).val();
    if (comId.length === 0) {

        ddlC.html('<option value="">----السيارة-----</option>');
        ddlC.attr("disabled", "disabled");
        $("#btnSearch").attr("disabled", "disabled");
        return false;

    }
    //get cars for selected company
    $.getJSON(getUrl_Root() + 'Tools/GetCarsList', { carCompanyId: comId }, function(results) {


        var oHtml = "";

        $.each(results, function(code, name) {

            oHtml += '<option value="' + code + '">' + name + '</option>';
            return true;
        });

        ddlC.html(oHtml);
        ddlC.attr("disabled", "");

        $("#btnSearch").attr("disabled", "");



        //save selected company
        setCookie_QueryCompany(comId);

        var queryCarCookie = getCookie_QueryCar();

        //load selected car if available
        if (queryCarCookie) {

            ddlC.val(queryCarCookie);
            ddlC.change();
        }
        else {
            //else : reset to Defaults
            ddlC.attr("selectedIndex", 0);

        }

    });
    return false;
}
function search_byCar() {

    //create url for search and redirect
    var cId = $("#ddlCar").val();

    var url = getUrl_Root() + 'search/car/' + cId;

    //save selected car
    setCookie_QueryCar(cId);


    window.location = url;

}

function init_SearchGoToAd() {

    $("#txtGoToAd").format({ precision: 0, allow_negative: false, autofix: true });
    $("#btnGoToAd").click(function() {

        var adId = $("#txtGoToAd").val();

        if (isNaN(parseInt(adId))) return false;

        var adUrl = getUrl_Root() + 'ad/' + adId;

        window.location = adUrl;


    });
}

function showMessage(isError, msg) {

    var $msgBox = $('div#dialogMessage');

    //setup dialog box
    $('div#dialogMessage').dialog({
        autoOpen: false,
        title: 'رسالة',
        closeOnEscape: false,
        width: 400,
        hide: 'slide',
        resizable: false,
        modal: true,
        close: function(event, ui) { $(this).dialog("destroy"); },
        buttons: { "- إغلاق -": function() { $(this).dialog("destroy"); } }
    });

    $msgBox.html(msg);

    if (isError) {
        $msgBox.dialog('option', 'dialogClass', 'alert');
    }

    $msgBox.dialog('open');
}

//------------------
//Root URL
//------------------
function getUrl_Root() {

    return 'http://www.sell11.com/';
   // return $("#hf_Global_Url").val();
}

//------------------
//Cookies
//------------------
//Cookie : Search > Company 
function getCookie_QueryCompany() {
    return $.cookieJar('q').get('com');
}
function setCookie_QueryCompany(theValue) {

    if (getCookie_QueryCompany() != theValue) {
        return $.cookieJar('q').set('com', theValue);
    }

}
//Cookie : Search > Car 
function getCookie_QueryCar() {

    return $.cookieJar('q').get('car');
}
function setCookie_QueryCar(theValue) {
    if (getCookie_QueryCar() != theValue) {
        return $.cookieJar('q').set('car', theValue);
    }
}


function check_browsers() {


    $.reject({
        reject: { all: false, msie5: true, msie6: true }, //reject IE5,6
        display: ['msie', 'firefox', 'chrome', 'opera'],
        header: 'هنالك مشكلة بمتصفحك', // Header Text
        paragraph1: 'متصفحك لن يظهر الموقع بالطريقة الصحيحة', // Paragraph 1
        paragraph2: 'للمتابعة يرجي تطوير متصفحك الى الاصدار الجديد', // Paragraph 2
        closeCookie: true,
        closeMessage: 'عند إغلاق النافذة لن يظهر الموقع بالطريقة الصحيحة', 
        closeLink: 'إضغط هنا لإغلاق النافذة و الدخول',
        imagePath: 'http://www.sell11.com/images/browsers/'
    }); // Customized Text 

}


//-----------------
//--Prototypes--------
//---------------------

