﻿/// <reference path="jquery.intellisense.js" />

//-- VARIABLES ----
var area_amount;
var tbx_amount;
var tbx_data;
var results_div;
//-- end VARIABLES ----


jQuery(document).ready(function() {

    //-- VARIABLES ----
    area_amount = $("DIV#input_amount_area");
    tbx_amount = $("INPUT#tbx_input_amount");
    tbx_data = $("#tbx_selected_data");
    results_div = $("DIV#results_area");
    //-- end VARIABLES ----

    //startup actions
    process_checked_items();
    calculate();
    load_adds();


    //alert("test");

    //    $("DIV#divimg_bookmark").click(function() {
    //        bookmark();
    //    });
    //    $("DIV#divimg_recommend").click(function() {
    //        alert("priporoči prijatelju klik");
    //    });

    //--- checkbox, radio CLICKS ----
    //from currency change

    //    $("DIV#valutes_from INPUT:radio").click(function() {
    //        clear_all_rbtns();
    //        process_checked_items();
    //        calculate();
    //    });
    //to currency change
    //    $("DIV#valutes_to INPUT:checkbox").click(function(e) {
    //        e.preventDefault();
    //        //$(this).parent().removeClass("selected");
    //        //process_checked_items();
    //        //calculate();
    //    });

    //preracunaj iz click
    $("DIV#valutes_from DIV").click(function() {
        var chk = $(this).children("INPUT:radio");
        //UNCHECK
        // if (chk.attr("checked") == true)
        //     chk.removeAttr("checked");
        // else

        chk.attr("checked", "checked");

        $(this).removeClass("selected");
        clear_all_rbtns();
        process_checked_items();
        calculate();
    });

    //preracunaj v click
    $("DIV#valutes_to DIV").click(function() {
        var chk = $(this).children("INPUT:checkbox");
        if (chk.attr("checked") == true)
            chk.removeAttr("checked");
        else
            chk.attr("checked", "checked");

        $(this).removeClass("selected");
        process_checked_items();
        calculate();
    });


    //--- END checkbox, radio CLICKS ----

    //CALCULATE CLICK
    $("INPUT#btn_calculate").click(function() {
        //calculate and display results
        calculate();

    });

    //ENTER pressed
    $('INPUT#tbx_input_amount_text').keyup(function(e) {
        //alert(e.keyCode);
        if (e.keyCode == 13) {
            calculate();
            $(this).blur();
        }
    });

    //SELECT ALL, CLEAR
    $("SPAN.select_links A").click(function(e) {
        e.preventDefault();
        var href = $(this);
        if (href.attr("href") == "check_all") {

            check_clear_all("check");

        }
        else if (href.attr("href") == "clear") {

            check_clear_all("clear");
        }
        process_checked_items();
        calculate();

    });


});                                               //ENd $(document).ready(function(){


function process_checked_items() {
   
    var from_rb =$("DIV#valutes_from INPUT:radio:checked");
    var to_chks = $("DIV#valutes_to INPUT:checkbox:checked");

    from_rb.parent().addClass("selected");
    to_chks.parent().addClass("selected");
    
    var str_data = "";
    str_data += from_rb.val();

    to_chks.each(function() {
        str_data += "|" + $(this).val();

    });

    //save to textbox
    tbx_data.val(str_data);

    //set from flag (by the input amount field)
    var flag_from = $("DIV#input_amount_area SPAN.flag_from");
    flag_from.removeClass();
    flag_from.addClass("flag_from");

    var currency_name = from_rb.val().split(';')[0].toLowerCase();
    flag_from.addClass(currency_name);

    flag_from.html(currency_name.toUpperCase());
}

//selects or clears all checkboxes in currencies list
function check_clear_all(check_or_clear) {
    var to_chks = $("DIV#valutes_to INPUT:checkbox");

    to_chks.each(function() {
        if (check_or_clear == "check") {
            $(this).attr("checked", "checked");
        }
        else if (check_or_clear == "clear") {
            $(this).removeAttr("checked");
            $(this).parent().removeClass("selected");
        }
    });
}

function clear_all_rbtns() {
    var to_chks = $("DIV#valutes_from INPUT:radio");

    to_chks.each(function() {
        
       //$(this).removeAttr("checked");
       $(this).parent().removeClass("selected");
        
    });
}


//function calculates and DISPLAYs results
function calculate() {
    var loading_gif = $("IMG.loading_results");

    loading_gif.show();
    var params = "data=" + tbx_data.val() + "&amount=" + tbx_amount.val();

    $.ajax({
        type: "post", url: "inc/ajax_calls.aspx?action=get_results",
        data: params,
        success: function(response) {
            if (response == "Napaka") {
                //error message
                alert("Prišlo je do napake.");
                // reset_textboxes();
            }
            else {
                results_div.html(response);

            }
            loading_gif.fadeOut();
        }
    }); //end ajax
}

function load_adds() {
//    $.ajax({
//        type: "get", url: "inc/oglasi.html",

//        success: function(response) {
//            $("DIV#adds_area").html(response);
//        }
//    });  //end ajax



}

//opens "bookark" dialog window
function bookmark() {
    var title = "Pretvornik valut - VALUTE.SI";
    var url = document.location; //windowdow.location.href ;

    if (window.sidebar) window.sidebar.addPanel(title, url, "");

    if (window.opera && window.print) {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel', 'sidebar');
        mbm.setAttribute('href', url);
        mbm.setAttribute('title', title);
        mbm.click();
    }

    else if (document.all) window.external.AddFavorite(url, title);

}

