$(document).ready(function(){
    $('#bottomtabs').tabs();
}) 

var timeout         = 500;
var closetimer      = 0;
var ddmenuitem      = 0;

function jsddm_open()
{
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close()
{
    if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer()
{
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
    if(closetimer)

    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

function removeByElement(arrayName,arrayElement)
{
    for(var i=0; i<arrayName.length;i++ )
    {
        if(arrayName[i]==arrayElement)
            arrayName.splice(i,1);
    }
}

$(document).ready(function()
{
    $('#jsddm > li').bind('mouseover', jsddm_open);
    $('#jsddm > li').bind('mouseout',  jsddm_timer);
    
    //var productIds = new Array();
    var products = new Array();
    var productType = "";
	
	/*chechkboxLimit = function() {
	
		$(".compare_prod").click(function(){
			products = new Array();
			
			
			if(!productType){
				productType = $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().attr("id");
			}
	//        alert(productType);return false;
			
			if ($(".compare_prod:checked").length==0) {
				productType = false;
			}
			
			$(".compare_prod:checked").each(function(){
				var mystr = $(this).attr("id");
				products[products.length] = mystr.slice(8);
				prtpIds[prtpIds.length] = $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().attr("id");
				
			});
			
			if (products.length > 4) {
				products.pop();
				alert("Please select 4 products or less to compare");
				return false;
			}
			
			if((productType !== $(this).parent().parent().parent().parent().parent().parent().parent().parent().parent().attr("id")) && productType!=false){
				products.pop();
				alert(" You can only compare products of the same type");
				return false;
			}
		});
	};*/

	$(".compare_prod").not($(".compare_prod:first")).click(function(){
			products = new Array();
                        
                        var not_current_checks = $(".compare_prod:checked").not($(".compare_prod:first"));
                        
                        if (not_current_checks.length == 0) {
                            $(".compare_prod").not($(".compare_prod:first")).each(function(){
                                $(this).removeAttr("disabled");
                                $(this).parent().parent().parent().parent().parent().children(":button").removeAttr("disabled");
                                productType = "";
                            });
                        }

                        if($(this).attr("checked")) {
                            if(productType == ""){
                                productType = $(this).parent().parent().parent().parent().attr("id");

                                $(".compare_prod").not($(".compare_prod:first")).each(function(){
                                   if($(this).parent().parent().parent().parent().attr("id") != productType) {
                                       $(this).attr("disabled", "disabled");
                                       $(this).parent().parent().parent().parent().parent().children(":button").attr("disabled", "disabled");
                                   }
                                });
                            }
                        }
	//        alert(productType);return false;
			/*
			if ($(".compare_prod:checked").length==0) {
				productType = false;
			}*/
			
			$(".compare_prod:checked").each(function(){
				var mystr = $(this).attr("id");
				products[products.length] = mystr.slice(8);
			});
			
			if (products.length > 4) {
				products.pop();
				alert("Please select 4 products or less to compare");
				return false;
			}
			
			/*if((productType !== $(this).parent().parent().parent().parent().attr("id")) && productType!=false){
				products.pop();
				alert(" You can only compare products of the same type");
				//return false;
			}*/
		});
    
    $(".compare_button").click(function(){
        var url = "";
        for(var i = 0 ; i < products.length ; i++){
            url += "/productId/" + products[i];
        }
        window.location.href = window.baseUrl + "/compare/view" + url;
        return false;
    });
});

document.onclick = jsddm_close;
