/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 *
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);

var pspanel = {};

$(document).ready(function(){

    $('a[rel="external"], area[rel="external"]').attr('target','_blank');
    $('button').button();

    //top navigation hover / active effects
    $('#top-navigation-links li a').hover(function(){
        $(this).parents('li[class!="active"]:first').addClass('hovered');
    },function(){
        $(this).parents('li[class!="active"]:first').removeClass('hovered');
    });

    var closeNavMenus = function(callback, callee){
        if($('#nav-stores-link').attr('state') == 'open'){
            toggleStoreMenu({'state':'open','callback':function(){
                if($('#nav-addon-link').attr('state') == 'open')
                    toggleAddonMenu({'state':'open','callback':callback});
                else
                    if($('#nav-stores-link').attr('state') != 'open' && callee == 'nav-addons-link') callback();
            }});
        }else{
            callback();
        }
    }

    var toggleStoreMenu = function(data){
        data = data || {};
        data.state = (data.state) || 'closed';
        
        var callback = data.callback || function(){};
        var newState = (data.state == 'closed') ? 'open' : 'closed';
        $('#nav-stores-link').attr('state',newState);

        if(data.state == 'open'){
            $('#store-selection-menu-wrapper').stop().animate({marginLeft:'-215px'},'fast',function(){
                $(this).parents('#store-selection').hide().parents('#top-navigation-links li:first').show().next().show(1,callback);
            });
        }else{
            $('#top-navigation-links li:first').hide().next().hide();
            $('#store-selection').show().find('#store-selection-menu-wrapper').stop().animate({marginLeft:0},'fast',callback);
        }
    };

    var toggleAddonMenu = function(data){
        data = data || {};
        data.state = (data.state) || 'closed';
        
        var callback = data.callback || function(){};
        var newState = (data.state == 'closed') ? 'open' : 'closed';        
        $('#nav-addons-link').attr('state',newState);

        if(data.state == 'open'){
            $('#mini-addon-menu-wrapper').stop().animate({marginLeft:'-317px'},'fast',function(){
                $(this).parents('#mini-addon-menu').hide().parents('#top-navigation-links li:first').show().next().show().next().next().show(1,callback);
            });
        }else{
            $('#top-navigation-links li:first').hide().next().hide().next().next().hide();
            $('#mini-addon-menu').show().find('#mini-addon-menu-wrapper').stop().animate({marginLeft:0},'fast',function(){callback();});
        }
    };

    //store menu popup
    $('#nav-stores-link').click(function(){
        var jqo = $(this);
        closeNavMenus(function(){
            toggleStoreMenu({state:jqo.attr('state')});
        },'nav-stores-link')
    });

    //addon menu popup
    $('#nav-addons-link').click(function(){
        var jqo = $(this);
        closeNavMenus(function(){
            toggleAddonMenu({state:jqo.attr('state')});
        },'nav-addons-link')
    });

    var forceOpenStoreMenu = function(){
        toggleAddonMenu(
            {
             state:'open',
             callback:function(){
                toggleStoreMenu({state:'closed'});
             }
            }
        );
    };

    //toggle store menu open from addon-menu
    $('#switch-store').click(forceOpenStoreMenu);

    //login menu popup
    $('#nav-login-link').toggle(function(){
        $(this)
            .removeAttr('style')
            .addClass('active')
            .append($('<div class="top-nav-rounded-corners"><div class="top-nav-rounded-left"></div><div class="top-nav-rounded-right"></div></div>'))
            .stop()
            .removeClass('hovered');
        $('#login-menu').show();
        $('input[name="username"]').val('').focus();
        $('input[name="password"]').val('');
    },function(){
        $(this).removeAttr('style').removeClass('active').find('.top-nav-rounded-corners').remove();
        $('#login-menu').hide();
    });

    $('#change-addon-selector').change(function(){
        if($(this).find(':selected').attr('id') == 'switch-store'){
            forceOpenStoreMenu();
        }else{
            document.location.href = $(this).val();
        }
    })

    $.ajaxSetup({cache:false});
    $('button').button().css({'cursor':'pointer'}).click(function(){
        $(this).parents('form:first').trigger('submit');
    });

    trace = function(t){
        if("console" in window)console.log(t);
    }

    $('#twitter-icon').hover(function(){
        $(this).fadeTo(0, 1);
    },function(){
        $(this).fadeTo(0, .2);
    }).click(function(){
        $(this).css({'z-index':9999}).fadeTo(0, 1).animate({'margin-left':'800px'},500,function(){
            document.location.href = 'http://www.twitter.com/pspanel';
            $(this).fadeTo(0, 1);
            return false;
        });
    });

})
