$(document).ready(function(){
    /*inizialize quicksearch*/
    
    /* check if platform hasn't brands on load */
    if($("select#brand_rs").children().length <= 2) /* 2 = 1 ("--select all--") + 1 (the only brand)*/
    {
        /* platform hasnt brand, hide all unecessary controlls */
        $("div#brand_rs_select").hide();
        $("div#allOpDiv").hide();
        $("table.loga").hide();
    }

    /* show/hide table of brand_images */
    $("#allOp").click(function(event){
        if($('.tohide').is(':hidden'))
        {
            $('.tohide').show();
            $('#allOp').html(translate['VIEW_LESS_NETWORKS']);
        }
        else
        {
            $('.tohide').hide();
            $('#allOp').html(translate['VIEW_ALL_NETWORKS']);
        }
    });

    /* unhide login table on mouseover */
    $('#loginlink').mouseover(function(event){
        $('#logindiv').show();
    });

    //make sure login div stays shown on mouse over
    $('#logindiv').hover(
        function(event){
            $('#logindiv').show();
        },
        function(event){
            $('#logindiv').hide();
        }
        );

   		/* unhide lang table on mouseover */
	    $('#langlink').mouseover(function(event){
	        $('#langdiv').show();
	    });

	    //make sure lang div stays shown on mouse over
	    $('#langdiv').hover(
	        function(event){
	            $('#langdiv').show();
	        },
	        function(event){
	            $('#langdiv').hide();
	        }
	        );

   /* redirect on brand select */
   $("select#platform_rs").change(function(){
       $.getJSON("/en/ajax/redirect-to-platform-find",{
            platform_rs: $("select#platform_rs").val()
        }, function(url){
            window.location = url;
        })
   });

    //  QUICKSEARCH
    //  platform select
    $("select#qsearch-platform").change(function(){
        $.getJSON("/en/ajax/get-brands-by-platform-rs",{
            platform_rs: $(this).val()
        }, function(j){
            var options = '';

            options += '<option value="">' + translate['SELECT_BRAND'] + '</option>';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
            }
            $("select#qsearch-brand").html(options);

            //if select length is <= 1, hide it (=> platform has no brands)
            //  and fill qsearch-model select directly and enable it
            if(j.length <= 1) {
                $("select#qsearch-brand").hide();
                // fill qsearch-model directly
                $.getJSON("/en/ajax/get-models-by-platform-rs",{
                    platform_rs: $("select#qsearch-platform").val()
                }, function(j){
                    var options = '';

                    options += '<option value="">' + translate['SELECT_PHONE'] + '</option>';
                    for (var i = 0; i < j.length; i++) {
                        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                    }
                    $("select#qsearch-model").html(options);
                    $("select#qsearch-model").removeAttr('disabled'); //.attr('disabled', true);
                })
            } else {
                $("select#qsearch-brand").show();
                $("select#qsearch-brand").removeAttr('disabled');
                $("select#qsearch-model").attr('disabled', true);
            }
        })

    });

    //  brand select
    $("select#qsearch-brand").change(function(){
        $.getJSON("/en/ajax/get-models-by-brand-rs",{
            brand_rs: $(this).val()
        }, function(j){
            var options = '';

            options += '<option value="">' + translate['SELECT_PHONE'] + '</option>';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
            }
            $("select#qsearch-model").html(options);
            $("select#qsearch-model").removeAttr('disabled'); //.attr('disabled', true);
        })
    });

    //  phone select - redirect
    $("select#qsearch-model").change(function(){
        $.getJSON("/en/ajax/redirect-to-detail",{
            platform_rs: $("select#qsearch-platform").val(),
            brand_rs: $("select#qsearch-brand").val(),
            phone_rs: $("select#qsearch-model").val()
        }, function(url){
            window.location = url;
        })
    });
});