//open menu with given ID
function openMenu(menuID, linkObj, googleorchrome) {
    //compatible event
    if (um.s==googleorchrome) {    
    	//if the menu code is ready
    	if(um.ready) {
    		//find co-ordinates of link object
    		var coords = {
    			'x' : um.getRealPosition(linkObj,'x'),
    			'y' : um.getRealPosition(linkObj,'y')
    			};
    
    		//increase y-position to place it below the link
    		coords.y += (linkObj.offsetHeight);    		
    		//activate menu at returned co-ordinates
    		um.activateMenu(menuID, coords.x + 'px', coords.y + 'px');		
    	}
    }
    return false;	
}

//close menu with given ID
function closeMenu(menuID) {
    //safari and google
    if(um.s) {
	//if the menu code is ready
    	if(um.ready) {
    		//deactive menu
    		um.deactivateMenu(menuID);
    	}
    }
}

function hoveritem() {
    $(this).addClass("hoveritem");
}
function unhoveritem() {
    $(this).removeClass("hoveritem");
}
function clickitem() {
    var c=$(this).children("a");                
    if (c) {        
        if (c.attr("href")) {
            if (c.attr("target")!="_blank") {
                document.location=c.attr("href");
            }
        }
    }    
}
function object_sort(url) {        
    var ids=$('#object_order').jcSelected();
    alert(ids);
    if (ids && ids!=0) {
        document.location=url+"?"+ids;
    }
    return false;
}
function object_filter(url, element) { 
    var o=$('#object_order').jcSelected();       
    var s=$('#'+element).jcSelected();         
    var u=url+"?"+o; 
    if (s!=0) u=u+"="+s;
    document.location=u;
    return false;
}
function object_select(el) {        
    var p=$('#'+el).jcSelected();
    document.location=p;
    return false;
}
function boldwhite(el) {
    var s=$(el).text();
    if (s.match(/B:/)) {
        $(el).addClass("boldwhite"); 
        $(el).text(s.replace(/B:/, ''));
    }        
}

jQuery(document).ready( function(){        
    $("#udm").show();    
    $("div.topmenu div.item").hover(hoveritem, unhoveritem);
    $("div.leftnav div.item").hover(hoveritem, unhoveritem);
    $("div.rightnav div.item").hover(hoveritem, unhoveritem);
    $("div.halfleft div.item").hover(hoveritem, unhoveritem);
    $("div.newslist div.item").hover(hoveritem, unhoveritem);
    $("div.hoverable").hover(hoveritem, unhoveritem);
    
    //$("div.topmenu div.item").click(clickitem);    
    $("div.rightnav div.item").click(clickitem);    
    
    $("div.pblock").hover(
        function () {
            $(this).children("div.hoverable").addClass("hoveritem");
        },
        function () {
            $(this).children("div.hoverable").removeClass("hoveritem");
        }
    );

    $(".hovertrigger").hover(
        function () {            
            $(this).parent().parent("div.item").addClass("hoveritem");
        },
        function () {
            $(this).parent().parent("div.item").removeClass("hoveritem");
        }
    );
    
    $(".indexsol div.item").hover(
        function () {
            $(this).addClass("hoveritem");
            if ($(this).children("div.opa"))
                $(this).children("div.opa").hide();
        },  
        function () {
            $(this).removeClass("hoveritem");
            if ($(this).children("div.opa"))
                $(this).children("div.opa").show();
        }
    ); 
    
    $(".tobigpic").click(
        function () {
            var c=$(this).children("div.img").children("a");             
            
            $("div.item")
                .filter(function() {
                    return this.className.match(/currentitem/);
                })
                .removeClass("currentitem");
            ; 
            
            $(this).children("div.item").addClass("currentitem");            
            
            if (c) {
                
                $('#loading').show();
                $("#bigpic").hide();                
                var img = new Image();
                $(img).load(function () {
                    $(this).hide();
                    $('#loading').hide();
                    $('#bigpic').attr("src", c.attr("href"));
                    $("#bigpic").show();                    
                }).error(function () {                    
                }).attr('src', c.attr("href"));
            }
            return false;             
        }
    );

    $("#addcart").click(
        function () {  
            $.ajax({
                url: '/ajax/addcart.php',
                data: {addcart: $("#product_id").val()},
                success: function(data) {
                    //$('#infocart').show(); 
                    $('#addcart').hide(); 
                    $('#cartlink').show(); 
                    //$('#infocart span').html(data);                    
                    $.ajax({
                        url: '/ajax/cartinfo.php',
                        success: function(data) {       
                            $('#cart_counter').html(data);                            
                            if (data==1) $('#cart_counter_text').html("Item");
                            else         $('#cart_counter_text').html("Items");
                        }
                    });
                    jQuery('#hiddenModalInfoText').text(data);
                    jQuery('#InfoTrigger').click();                    
                }
            });
            return false;
        }
    );   

    
    $('.oimage img').preload();
    $("map#m_usa_map area").hover(
        function () {
            var id_image=$(this).attr('data-state');
            $('#state_'+id_image).css('display', 'block');
        },
        function () {
            var id_image=$(this).attr('data-state');
            $('#state_'+id_image).css('display', 'none');
        }
    );
    
    $('#productnav ul li a').click(
        function () {
            var h=$(this).attr('href');            
            var t=h.split('?');                                                                      
            var u=t[0].replace(".html", "");
            u=u.replace("/", "");
            $.ajax({
                url: "/ajax/nav.php?"+t[1],
                data: {navobj: u},
                success: function(data) {
                    $('div.leftnav').html(data);                    
                    $("div.leftnav div.item").hover(hoveritem, unhoveritem);                    
                    var urlto=t[0].replace(".html", "");            
                    if (t[1]=='solution') {
                        $.ajax({
                            url: "/ajax/solutions.php",
                            data: {page_url: urlto},
                            success: function(data) {
                                $('#solution').html(data);
                                um.refresh(false);
                            }
                        });
                    }
                    if (t[1]=='attachment') {
                        $.ajax({
                            url: "/ajax/attachments.php",
                            data: {page_url: urlto},
                            success: function(data) {
                                $('#attachment').html(data);
                                um.refresh(false);
                            }
                        });
                    }
                    if (t[1]=='product') {
                        $.ajax({
                            url: "/ajax/products.php",
                            data: {page_url: urlto},
                            success: function(data) {
                                $('#product').html(data);
                                um.refresh(false);
                            }
                        });
                    } 
                    if (t[1]=='name') {
                        $.ajax({
                            url: "/ajax/"+u+".php",
                            data: {page_url: urlto, "objmode": 1},
                            success: function(data) {
                                $('#object_select').html(data);
                                um.refresh(false);
                            }
                        });
                    }
                }
            });
            $("#productnav ul li")
                .filter(function() {
                    return this.className.match(/selected/);
                }).removeClass("selected");
            ;             
            $(this).parent("li").addClass("selected");
            $(this).parent().parent("ul").hide();
            return false;
        }
    );
                
});

var intervalID=setInterval(fdoloadimg, 100);
var imagesloaded=0;
function fdoloadimg() {
    if (um.ready && !imagesloaded) {
        $("div.doloadimg").each(function() {
            var th=this;
            var src=this.title;                            
            var img = new Image();        
            $(img).load(function() {            
                th.title="";
                th.style.backgroundImage="url("+src+")";            
            }).error(function () {                    
            }).attr('src', src);
        });        
        imagesloaded=1;
        clearInterval(intervalID);
    }
}

function check_cart_form() {
    var required=new Array("firstname", "lastname", "address1", "city", "zipcode", "email", "jobsite");    
    var d=$(":radio[name=delivery]").filter(":checked").val();
    if (d==2) {
        required[required.length]="deliveryaccount";
    }    
    if (d==3) {
        required[required.length]="deliveryaccount2";
    }       
    var ret=1;
    for (i=0; i<required.length; i++) {
        if ($('[name='+required[i]+']').attr('class')!="edited") {
            ret=0;
            $('[name='+required[i]+']').addClass("hoveritem");
        }
    }       
    if ($('#country_hidden').val()==0) {
        $('#country_hidden').prev("div").addClass("hoveritem");
        ret=0;
    }
    if ($('#state_hidden').val()==0) {
        $('#state_hidden').prev("div").addClass("hoveritem");
        ret=0;
    }        
    var email=$('[name=email]').val();
    if (email!="" && email!=defaults.email) {
        if (checkEmail(email)==false) {
            ret=0;
            $('[name=email]').addClass("hoveritem");
        }
    }
    
    if (ret==0) {
        $('#errormessage').show();
        return false;
    }
    else {
        $('#errormessage').hide();
        document.forms['cartform'].submit();
        return true;
    }
}

function showmainpic() {
    $('.mainpic').show();
    $('.ptoductpic').hide();    
    $("#t_mainpic").addClass("currentitem");
    $("#t_ptoductpic").removeClass("currentitem");    
}

function showptoductpic() {
    $('.mainpic').hide();
    $('.ptoductpic').show();
    $("#t_mainpic").removeClass("currentitem");
    $("#t_ptoductpic").addClass("currentitem");        
}

function downloadform() {    
    
    if ($("input[name$='sel[]']").filter(':checked').size()==0) {
        alert('Please select from the list on the right to view and save!')
        return false;
    }else{
        return true;
    }
            
}
