/* global.... */

jQuery.noConflict();

jQuery(function($){
    
    $('.selectChange').change(function(){
        window.location = $(this).val();
    });
    
    $('#storeNav a').hover(function(){
        $(this).stop().animate({ 
            width: "110px"
        }, 300, "swing", function(){
            $(this).children('.text').fadeIn('fast');
        });
    }, function(){
        $(this).children('.text').fadeOut('fast');
        $(this).stop().animate({ 
            width: "10px"
        }, 300, "swing", function(){
            $(this).children('.text').hide();
        });
    });
    
    $('.searchGear a').toggle(function(){
        openSearchForm();
        $('.searchGearForm').bind('mouseleave', function(){
            closeSearchForm();
        });
    }, function(){
        closeSearchForm();
    });
    
    

    $('a.popup').click(function(){
        var rel = $(this).attr('rel');
        var href = $(this).attr('href');
        if(!rel){
            window.open(href,'newWindow');
        } else {
            var rel_split = rel.split('|');
            var w = rel_split[0];
            var h = rel_split[1];
            var scrollbars = rel_split[2];
            var leftPos=(screen.width-w)/2;
            var topPos=(screen.height-h)/2;
            window.open(href,'newWindow','width='+w+',height='+h+',left='+leftPos+',top='+topPos+',scrollbars='+scrollbars+',resizable=no,statusbar=no,menubar=no,toolbar=no');
        }
        return false;       
    });

    
    function openSearchForm()
    {
        $('.searchGearForm').animate({ 
            width: "240px",
            height: "40px"
        }, 500, "swing", function(){
            $(this).find('form').fadeIn();
        });
    }
    
    function closeSearchForm()
    {
        $('.searchGearForm form').fadeOut(function(){
            $('.searchGearForm').animate({ 
                width: "0"
            }, 500, "swing", function(){
                $(this).hide();
            });
        });
    }
    
    $('#subNav li').each(function(i){
        var link = $(this).children('a').attr('href');
        var uri = parseUri(window.location);
        var segments = uri.directory.split('/');
        var section = segments[1] + '/' + segments[2];
        if(link == '/' + section){
            $(this).addClass('active');
        }
    });
    
});


// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
var parseUri = function(str) {
    var o   = parseUri.options,
        m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
        uri = {},
        i   = 14;

    while (i--) uri[o.key[i]] = m[i] || "";

    uri[o.q.name] = {};
    uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
        if ($1) uri[o.q.name][$1] = $2;
    });

    return uri;
};

parseUri.options = {
    strictMode: false,
    key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
    q:   {
        name:   "queryKey",
        parser: /(?:^|&)([^&=]*)=?([^&]*)/g
    },
    parser: {
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
    }
};
