﻿$(function(){
    if($("#slider").length==1){
        $("#slider").show();
        ai_slider.init();
    }
    $(".handset-detail-tab").hide();
    $("#handset-tab li a:lt(4)").addClass("selected").removeClass("selected");
    $("#handset-tab li a:lt(4)").each(function(i){
        $(this).click(function(){
            $("#handset-tab li a:lt(4)").removeClass("selected");
            $(this).addClass("selected");
            $(".handset-detail-tab").removeClass("tab-selected").hide();
            $("#handset-detail-tab-data-"+(i+1)).addClass("tab-selected").show();
        });
    });
    var hash = window.location.hash;
    if(typeof(hash)=="string"&&$.trim(hash)!=""){
        $("#handset-tab li a:eq("+({
            "#Specifications":0,
            "#Connectivity":1,
            "#Feature":2,
            "#Other":3
        }[hash]||0)+")").click();
    }else{
        $("#handset-tab li a:eq(0)").click();
    }
    
    $("#handset-container input").click(function(){
        var c=$(this);
        if(c.attr("checked")){
            if($("#handset-container input:checked").length>3){
                $("#handset-container input:checked").each(function(){
                    if($(this).val()!=c.val()){
                        $(this).attr("checked",false);
                        return false;
                    }
                });
            }
        }
    });
    
    /*$("#handset-subfooter a").click(function(){
        if($("#handset-container input:checked").length>1){
            var ids=[];
            $("#handset-container input:checked").each(function(i){
                ids.push("id"+(i+1)+"="+$(this).val());
            });
            hs.htmlExpand($(this)[0], {
		        objectType : 'iframe',
		        src : "handsets-compare.rails?"+ids.join("&"),
		        width : 1000,
		        objectHeight : 500,
		        hieght : 500
	        });
        }
        return false;
    });*/
    var getSelectedPhones = function(ref){
        var ids=[];
        $("div.handset-compare-header select").each(function(){
            if(!ref){
                if($(this).val()!="") ids.push("id"+(ids.length+1)+"="+$(this).val());
            }else{
                if($(ref).attr("id")!=$(this).attr("id")&&$(ref).val()==$(this).val())
                    $(this).val("");
            }
        });
        return ids;
    };
    $("div.handset-compare-header select").change(function(){
        getSelectedPhones(this);
    });
    $("#btn-compare").click(function(){
        var ids = getSelectedPhones();
        if(ids.length>1)
            hs.htmlExpand($(this)[0], {
		        objectType : 'iframe',
		        src : "handsets-compare.rails?"+ids.join("&"),
		        width : 1000,
		        objectHeight : 500,
		        hieght : 500
	        });
    });
    
    
});
