// used for custom events
JLI.events = {};

JLI.components.twoTabPanel = {
    
    hashKey: "open",
    
    init : function() {
        // if not edit mode, create tabs
        if(!JLI.editMode) {
            $('div.twoTabContainer').tabs({
                selected: -1,
                collapsible: true
            });
            
            // unselect and hide other tabs
            $('div.twoTabContainer ul li a').click(function(e) {
                var selected = $(this).parents("div.twoTabContainer:first");
                $('div.twoTabContainer').not($(selected)).tabs('option', 'selected', -1);
                JLI.urlHash.updateState(JLI.components.twoTabPanel.hashKey, this.id);
                JLI.urlHash.pushState();
            });
        }
        
        var open = JLI.urlHash.state[JLI.components.twoTabPanel.hashKey];
        if(open) {
            $('#' + open).click();
            sc_trackBookmark(open);
        }
        
    }
};


JLI.ie = {
    isIE: $.browser.msie && $.browser.version < 8,
    fixButtons: function() {
        if(this.isIE) {
            $(".button > span").each(function(){
                var me = $(this), anchor = me.parent(), w;

                if( anchor.hasClass('button80') ) {
                    w = 43;
                } else if( anchor.hasClass('button125') ) {
                    w = 88;
                } else if( anchor.hasClass('button145') ) {
                    w = 114;
                }
                if(me.attr('scrollWidth') < w) {
                    me.css('width', w + 'px');
                }
            });
        }
    }
};


JLI.anchors = function() {
    // anchors with new window size
    $('a.windowNew').click(function(e){
        var a = $(this), classes = a.attr("class").split(' '), c;
        for(var i = 0; i < classes.length; i++) {
            if(classes[i].indexOf('w-') != -1) {
                c = classes[i];
                break;
            }
        }
        if(c) {
            var size = c.split('-'), width = size[1], height = size[2];
            if(width && height) {
                e.preventDefault();
                window.open(a.attr('href'), 'w', 'width=' + width + ',height=' + height + ',scrollbars=yes');
            }
        }
    });
    // styled link list anchors
    $('div.styledLinkList li a').click(function(e){
        var me = $(this), target = me.attr('target');
        if(target) {
            e.preventDefault();
            window.open(me.attr('href'), target, 'width=1024,height=768,scrollbars=yes');
        }
    });
};

JLI.urlHash = {
    
    // holds name value pairs
    state: {},

    init: function() {
        JLI.urlHash.state = $.deparam.fragment();
        $(JLI.events).trigger("hashdone", [JLI.urlHash.state]);
    },
    
    updateState: function(name, value) {
        JLI.urlHash.state[name] = value;
    },
    
    pushState: function() {
        $.bbq.pushState(JLI.urlHash.state);
    },
    
    getState: function(name) {
        return JLI.urlHash.state[name];
    }
};

$(document).ready(function (){
    JLI.urlHash.init();
    JLI.components.twoTabPanel.init();
    JLI.anchors();
    JLI.ie.fixButtons();
});

$(window).load(function() {
    $('img.pngfix').ifixpng("/etc/designs/pscd/images/blank.gif");
    if(navigator.userAgent.toLowerCase().indexOf('chrome')== -1){
        if (/^Win/.test(navigator.platform) && $.browser.safari) {
	    $('.funnelForm button[name="submit"][type="submit"] span').css('margin-top', -1);
        }
    }	    
});
