/**
 * General actions for the UI
 * 
 *	//TAB
 *		Finds all elements with class tab and converts them to jQueryUI tabs
 *		All links that are inside 
 * 
 * @author einarvalur
 */

$(function(){


    // Scroll to top on pagination click
    $('.pagination').each(function()
    {
        $(this).click(function()
        {
            $('html, body').animate({scrollTop:0}, 'normal');
        });
    });
	
	/**
	 * Render TABS
	 */
	$('.tab').tabs({
		cache: true
//		,fx: { opacity: 'toggle', duration: 'fast' }
		,spinner: 'Retrieving data...'
	});
	/**
	 * TABS pagination
	 */
	$('.tab').click(function(event){
		if( event.target.tagName   == 'A' &&  $(event.target).parents('.pagination').length==1 ){
			event.preventDefault();
			$(event.target).parents('.ui-tabs-panel').load( $(event.target).attr('href') );
		}
	});
	
	/**
	 * LOGOUT-DIALOG
	 * create modal window to ask the user
	 * if he really wants to log out
	 */
	$('#navigation-logout').click(function(event){
		event.preventDefault();
		var self = this;
                window.location = $(self).attr('href');
	});
	
	
	
	
	/**
	 * MENU/NAVIGATION
	 */
	$('#navigation a').focus(function(){$(this).blur()});
	$('#navigation-user-profile').menu('a#navigation-user-profile-btn');
	$('#navigation-cart-list').menu('a#navigation-cart-list-btn');
	
	$('a#navigation-cart-list-btn').click(function(event){
		$('#navigation-cart-list').load( $(event.target).attr('href') );
	})
	
	
	/**
	 * MENU-ACCORDION
	 */
	$('#navigation-info .navigation-panel').menu_accordion();

        //This fixes that bug that shall not be named now. I'll hunt down
        //culprit and give him the evil eye - ebe
        $('ul.user-mainnav li').css('width',$(".nav-username").width()+18);

	$("ul.user-mainnav li span").hover(function() { //When trigger is clicked...
                $(this).parents().find("ul.user-mainnav").addClass('nav-hover');

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.user-subnav").show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
                    $(this).parent().find("ul.user-subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up
                    $(this).parents().find("ul.user-mainnav").removeClass('nav-hover');
                });

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});



    /**
     *@Helper
     */
    $('.pop-right').each(function () {
        var distance = 10;
        var time = 250;
        var hideDelay = 500;

        var hideDelayTimer = null;

        var beingShown = false;
        var shown = false;

        var trigger = $('.trigger', this);
        var info = $('.popup', this).css('opacity', 0);

        $([trigger.get(0), info.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                // don't trigger the animation again
                return;
            } else {
                // reset position of info box
                beingShown = true;

                info.css({
                    left: 25,
                    display: 'block',
                    margin: '-80px 0px'
                }).animate({
                    left: '-=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function() {
                    beingShown = false;
                    shown = true;
                });
            }

            return false;
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    left: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });

            }, hideDelay);

            return false;
        });
    });


$('.pop-down').each(function () {
        var distance = 10;
        var time = 250;
        var hideDelay = 500;

        var hideDelayTimer = null;

        var beingShown = false;
        var shown = false;

        var trigger = $('.trigger', this);
        var info = $('.popup', this).css('opacity', 0);

        $([trigger.get(0), info.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                // don't trigger the animation again
                return;
            } else {
                // reset position of info box
                beingShown = true;

                info.css({
                    top: 20,
                    left: -217,
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function() {
                    beingShown = false;
                    shown = true;
                });
            }

            return false;
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });

            }, hideDelay);

            return false;
        });
    });


    /**
     *=close help bubble
     */
    $('.closeBox').click( function () {
        $('.popup').fadeOut();
        return false; }
    );

    /**
     *@forgot-pass modal
     */
    $("a.forgot-pass").fancybox({
        'frameWidth' : 450,
        'frameHeight' : 200
    });


    /**
     *= gogoyoko video
     */
    $("a.btn-watch-video").fancybox({
        'frameWidth' : 600,
        'frameHeight' : 338
     });
    
    
    /**
     * Like or Unlike
     * 
     * @author einarvalur
     */
    $('.i-like').click(function(event){
    	
    	event.preventDefault();
    	event.stopImmediatePropagation();
    	event.stopPropagation();
    	
    	if( $(event.target).hasClass('unlike') ){
    		$(event.target).removeClass('unlike').addClass('like');
    	}else{
    		$(event.target).removeClass('like').addClass('unlike');
    	}
    	
    	$.getJSON( $(event.target).attr('href'),function(data){
    		$(event.target).attr('href',data.url).attr('title',data.label);
    	});
    	
        return false;
    });

        //FOOTER: REGION RESCRIPT MODAL
        $("a.footer-apply").fancybox({
            'frameWidth' : 480,
            'frameHeight' : 440
        });

});

//SEARCH
function selectSearchOption(evt) {
	var targetId = $(evt.target).attr('id');
	var idParts = targetId.split('_');
	var searchInId = idParts[2];
	var searchValue = eval('labels.search.' + searchInId);
	currentSearchValue = labels.search.valuePrefix + searchValue;
	$('#searchInput').attr('value', currentSearchValue);
	$('#search').slideUp();
	return false;
}
var gyShowNavigation = true;
var currentSearchValue = '';
var labels = {
	search: {
		valuePrefix: 'Search ',
		gogoyoko: 'Gogoyoko',
		store: 'Store',
		storeArtists: 'Store Artists',
		storeSongs: 'Store Songs',
		storeAlbums: 'Store Albums',
		storeLabels: 'Store Labels',
		users: 'Users'
	}
};

function restrictedAction( ){
         $('<div><div class="modal-text">You need to log in to listen or buy.<br />Click here to log in or sign up for an account by following the "Sign Up" link below.</div>\n\
            <div class="clearfix"></div><div class="modal-controls"><a href="/login" class="btn">Login!</a> or <a href="/signup" class="close-modal-control">Sign Up!</a></div>').dialog({
                modal:true,
                title: "You need to be signed in!"
        })
}
         
         
