/*!
 * 
 */

/**
 * On document load actions
 * 
 * Every time a page is requested in the gogoyoko system
 * these function will be executed. (This does not apply to
 * XHR requests).
 */
$(function() {

	/**
	 * Login modal-dialog.
	 * 
	 * This is the main login window. The "login" button on the top-most menu
	 * will trigger this action and display a login window for user.
	 * 
	 */
	$('#head-login-btn').live('click',function(event) {
		event.preventDefault();
        $('#modal-dialog-loginform').dialog({
            width:580,
            title: "Log in to gogoyoko",
            minWidth:'580px',
            autoOpen: false,
            resizable:false,
            modal: true,
            draggable: false
        });
		$('#modal-dialog-loginform').dialog('open');
		$('#modal-dialog-loginform').dialog('option', 'title',
				$(event.target).attr('title'));
	});


	
	/**
	 * Dirty hack.
	 * 
	 * A-href tags that have it's href value as # will hide the gogo-player
	 * after we moved to the iframe implementation. This is a dirty hack that
	 * just disables these kind of links.
	 */
	$('a[href=#]').live('click', function(event) {
		//event.preventDefault();
	});

	/**
	 * Pagination scroll-top.
	 * 
	 * Everytime there is a pagination switch, the page scrolls to the top.
	 */
	$('.pagination').each(function() {
		$(this).click(function() {
			$('html, body').animate( {
				scrollTop : 0
			}, 'normal');
		});
	});

        /**
         * Render TABS
         * 
         * This is in conjunction with gogotabs library class. This will use the
         * jquery.ui functionalitu to turn the markup returned by the library class
         * into a working ui tab.
         */
        $('.tab').tabs({
                cache : true,
                spinner : 'Loading...'
        });


        /* tab pagination */
        $('.tab .pagination a').live('click', function(event) {
                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();
		window.location = $(this).attr('href');
	});

	/**
	 * Switch Identity.
	 * 
	 * On change in the Identity dropdown the form that contains it will be
	 * submited.
	 */
	$("#browse-as").change(function() {
		$('#identity-change-browsingas-form').submit();
	});
        
        
        //select menus
        $("select").sb();

	/**
	 * 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('/cart/inventory');
	});

	/**
	 * 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").hide(); 
					$(this).parents().find("ul.user-mainnav").removeClass('nav-hover');
					// When the mouse hovers  out of the subnav, move it back up
				});

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

	/**
	 * Search boxes.
	 * 
	 * All input fields that have the css class 'search' will get this
	 * functionality.
	 * 
	 * When a input gets focus, if it contains default value it's value will be
	 * removed else it will get highlighted. When the blur event fires it will
	 * get default value if it's empty, else it will hold the give value.
	 * 
	 */
	$('input[type=text].search').clear_input();

	/**
	 * @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) {
				return;// don't trigger the animation again
			}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) {
				return;// don't trigger the animation again
			} 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
	 * 
	 */
	$('.toggle-like').live('click',function(event) {

		event.preventDefault();
		event.stopImmediatePropagation();
		event.stopPropagation();

		if ($(event.target).hasClass('action-like')) {
                    $(event.target).removeClass('action-like').addClass('action-like-active');
		}else{
                    $(event.target).removeClass('action-like-active').addClass('action-like');
		}

                Gogo.updateHeartedList($(event.target).attr('href'));
		$.getJSON($(event.target).attr('href'), function(data) {
			$(event.target).attr('href', data.url).attr('title',data.label);
		});
                
                
                

		return false;
	});

	/**
	 * Like Unlike when user is not logged in.
	 */
	$('.action-like.unauthorised').live('click', function(event) {
		event.preventDefault();
		Gogo.restrictedAction();
		return false;
	});

	/**
	 * Song context menu
	 * 
	 * These are actions in the song context menu, or cog menu, what ever you
	 * want to call it.
	 * 
	 * They use the 'live' function since we don't know where the markup comes
	 * from. We can assume that it is generated with XmlHttpRequest
	 * 
	 */
	$('.controls-context .action-cog').live('click', function(event) {
		event.preventDefault();
	});

	$('.playlist-add').live('click',function(event) {
		event.preventDefault();
		var self = $(event.target);
		Gogo.addTrackToPlaylist(
				$(event.target).attr('id').split('-').pop(), 
				function() {}, 
				function() {}
		);
                event.stopPropagation();
	});
	$('.controls-context .cart-add').live('click', function(event) {
		event.preventDefault();
		Gogo.addToCart($(event.target).attr('id').split('-').pop());
                event.stopPropagation();
	});
	
	$('.share-song').live('click', function(event) {
		event.preventDefault();
                if( $(this).hasClass('inactive') )
                { /* VOID */ }
                else{
                    $('#modal-dialog-shareform').dialog({
                        width:535,
                        height:200,
                        title: "Share this song",
                        autoOpen: false,
                        resizable:false,
                        modal: true,
                        draggable: false
                    });
                    var idString = $(event.target).attr('id').split('-');
                    $('#modal-embed').val('<object width="460" height="100"><param name="movie" value="http://www.gogoyoko.com/object/widget_player.swf?songId='+$(event.target).attr('id').split('-').pop()+'" /><param name="allowscriptaccess" value="always" /><embed src="http://www.gogoyoko.com/object/widget_player.swf?songId='+$(event.target).attr('id').split('-').pop()+'" type="application/x-shockwave-flash" allowscriptaccess="always" width="460" height="100" /></object>');
                    $('#modal-url').val('http://www.gogoyoko.com/song/'+idString[1]);
                    $('#fb-link').attr('href','http://www.facebook.com/sharer.php?u=http://www.gogoyoko.com/song/'+idString[1]);
                    $('#tw-link').attr('href','http://twitter.com/home?status=listening to ' + $(event.target).attr('ref') + ' via http://www.gogoyoko.com/song/'+ idString[1] + ' @gogoyoko');
                    $('#modal-dialog-shareform').dialog('open');
                }  
                event.stopPropagation();
        });
        
        $('ul.controls-context li ul').live('click', function(event) {
		event.preventDefault();
                event.stopPropagation();
        });
        
        $('.cog-href').live('click', function(event) {
		event.preventDefault();
                $.address.path($(this).attr('href'));
                event.stopPropagation();
        });

	$('.controls-context .toggle-like').live('click',function(event) {
		event.preventDefault();

		if ($(event.target).hasClass('action-like')) {
			$(event.target).removeClass('action-like').addClass('action-like-active');
		}else{
			$(event.target).removeClass('action-like-active').addClass('action-like');
		}

		$.getJSON($(event.target).attr('href'), function(data) {
			$(event.target).attr('href', data.url).attr('title',data.label);
		});

		return false;
	});
	


 /**
 * JS PLAYBACK FUNCTIONALITY
 *
 *  live('click') functions for song / albums / playlist playback.
 *
 *  
 **/


  /* album playback */
  $('.play-album').live('click',function ( event ){
          event.preventDefault();
          var albumId = $(event.target).attr('id').split('-').pop();
          var albumTracks = [];
          var currentTrack = "";
          currentPlaylist = null;
          var playerId = "";

          //AJAX
          //	get album tracks and play first track
          $.ajax({
              type: 'POST',
              url: '/gogoblaster/playalbum/',
              data: {id:albumId},
              success: function(result) {
                  albumTracks = result.trackList;
                  currentTrack = result.firstTrack;
                  playerId = result.playerId;
              },
              async: false,
              dataType: 'json'
          });

          Gogo.playAlbum(playerId, currentTrack, albumTracks );

  });



  /* specific album track playback */
  $('.play-album-track').live('click',function ( event ){
          event.preventDefault();
          var currentTrackId = $(this).attr('id').split('-').pop();
          var albumTracks = [];

        $("#album-tracks .play-album-track").each(function() {
            albumTracks.push( $(this).attr('id').split('-').pop() );
        });

        Gogo.playAlbum('0', currentTrackId, albumTracks );

  });


  /* song playback */
  $('.play-song').live('click',function ( event ){
          event.preventDefault();
          Gogo.playTrack( $(event.target).attr('id').split('-').pop() );
  });



  /* playlist playback */
  $('.play-playlist').live('click',function ( event ){
          event.preventDefault();
          var currentTrackId = $(this).attr('id').split('-').pop();
          var playlistTracks = [];

        $('.songs li.song').each(function() {
            playlistTracks.push( $(this).attr('id').split('-').pop() );
        });

        Gogo.playAlbum('0', currentTrackId, playlistTracks );
  });

  /* playlist opening prohibited */
  $('#no-open-playlist').live('click',function ( event ){
          event.preventDefault();
          Gogo.restrictedAction();
  });

  /* playback prohibited */
  $('.no-play').live('click',function ( event ){
          event.preventDefault();
          Gogo.restrictedAction();
  });


    $('#modal-dialog-shareform').dialog({
        width:535,
        height:200,
        title: "Share this song",
        autoOpen: false,
        resizable:false,
        modal: true,
        draggable: false
    });

	/**
	 * Facebook
	 *
	 * Turn 'share' button into popup window
	*/
	$('#fb-link').live('click',function(event){
		event.preventDefault();
    	u=location.href;
    	t=document.title;
    	window.open( $(event.target).attr('href')+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    	$('#modal-dialog-shareform').dialog('close');
    	return false;
	});

	$('#tw-link').live('click',function(event){
		//window.open($(event.target).attr('href'));
		$('#modal-dialog-shareform').dialog('close');
	});


});

/**
* JS / COOKIE PLAYBACK FUNCTIONALITY
*
* It is (in a way) silly to do this here. That being said, we needed
* to implement a quick and dirty playback limit (five 'play' limits) that
* would trigger signup dialog when exceeded for users that are not logged-in
* This does that. The cookie expires after 24hours so returning visitors that
* are not logged in can start playing when returning nexxt day.
*
*/
function playbackCount()
{
        var count = 0;
        //USER LOGGED IN
        if(is_logged_in){
            return count;
        }
        else
        {//USER NOT LOGGED IN
            //FIRST PLAYBACK COUNT
            if($.cookie("gogo_playback")==null)
            {
                count = 1;
            }//UPDATE COUNT
            else{
                count = parseInt($.cookie("gogo_playback")) + 1;
            }
            //UPDATE COOKIE, COOKIE EXPIRES AFTER ONE DAY
            $.cookie("gogo_playback", count, {path: '/', expires: 1});
            return count;
        }
}

/**
 * 
 * @return void
 */
function restrictedAction() {
    $('#modal-dialog-loginform').dialog({
        width:580,
        title: "Log in to gogoyoko",
        minWidth:'580px',
        autoOpen: false,
        resizable:false,
        modal: true,
        draggable: false
    });
	$('#modal-dialog-loginform').dialog('open');
	$('#modal-dialog-loginform').dialog('option', 'title','You have to log in to do that!');
}



/**
 * Object Gogo.
 * 
 * This is like a class except that it's static. You never create an instance of
 * it. <code>
 * 	Gogo.addToCart();
 * </code>
 * 
 * @static
 */
var Gogo = {

	/**
	 * Flash player taks to the content frame through
	 * these methods.
	 * 
	 * Consider these mathods as private.
	 */
	player: {
		/**
		 * Set selected song.
		 * 
		 * In the profile player, hightlight  which song is playing.
		 * This is callsed by the flash-player, but
		 * could be called by others
		 * @param int id
		 * 
		 */
		hightlightPlaylistItem: function( id ){
			Gogo.higlightPlaylistItem(id);
		},
		/**
		 * Buy Items.
		 * 
		 * This method is called by the player and shouldn't be called bay any
		 * one else. The reason is that the player doesn't know the productID.
		 * Functions in the content frame should call <code>Gogo.addToCart()</code>,
		 * the difference is these two are that <code>buyItem</code> will call an
		 * action in the cart controller that can convert the songId into productId
		 * before adding the product to cart.
		 * 
		 * @param int id 
		 * @param string type (song|album) 
		 */
		buyItem: function(id, type ){
			url = '/cart/itemtocart/';
			$.post(url, {id : id,type : type}, function(data) {
				if (data.success == false) { // Error
						if (data.identity == 0) {
							restrictedAction();
						} else {
							$.jGrowl(data.message);
						}
					} // Everything in order
					else {
						$.jGrowl(data.message);
						$("#cart-badge").text(data.cartcount).removeClass('hidden');
					}
				}, 'json');
		},
		/**
		 * Remove item from playlist.
		 * 
		 * @param int id Song ID.
		 * @param int playlistId Playlist ID.
		 */
		removeFromPlaylist:function(id, playlistId){
			Gogo.removeFromPlaylist(id, playlistId);
		}
		
	},
		
	/**
	 * Set selected song.
	 * 
	 * In the profile player, hightlight  which song is playing.
	 * This is callsed by the flash-player, but
	 * could be called by others
	 * @param int id
	 * 
	 */
	higlightPlaylistItem: function( id ){
		$('.songs li').removeClass('hilite');
		$('#song-'+id).addClass('hilite');
	},
        
	/**
	 * Update the Hearted List int the playlist
	 * 
	 * @param int id
	 * 
	 */
	updateHeartedList: function( href ){
            var item_id = href.split('/')[3];
            var status = href.split('/')[4];
            var type = href.split('/')[2];

            //only applies to songs
            if(type == 'song')
            {
                //add to the list
                if( status == 1)
                {
                    //  get track data and add to list
                    $.ajax({
                      type: 'POST',
                      url: '/song/songMetaData/',
                      data: {id:item_id},
                      success: function(result) {
                            $.get('/js/gogoplayer/templates/loved-track.tpl', function(tmpl) {
                                result.trackData['duration'] = result.duration;
                                $('#tbody-loved').jqoteapp(tmpl, result.trackData);
                            });
                      },
                      async: false,
                      dataType: 'json'
                    });
                }
                else
                {//we need to remove from the list
                    $('#tloved-song_'+item_id).fadeOut(300, function(){$(this).remove();});  
                }         
            }
	},


	/**
	 * Remove item from playlist.
	 * 
	 * @param int id Song ID.
	 * @param int playlistId Playlist ID.
	 */
	removeFromPlaylist: function(id, playlistId) {
		url = '/gogoblaster/removeitem/';
		$.post(url, {id : id,playlistId : playlistId}, function(data) {}, 'json');			
	},
	
	/**
	 * Add item to cart
	 * 
	 * @param int id ID of item
	 * @param Function before Callback function
	 * @param Function after Callback function
	 */
	addToCart : function(id, before, after, failure) {
		var before = before || function() {};
		var after = after || function() {};
		var failure = failure || function() {};
		before.apply(this);
		$.post('/cart/additem/', {id : id}, function(res) {
			if (res.success == true) {
				$.jGrowl(res.message);
				$("#cart-badge").text(res.cartcount).removeClass('hidden');
				after.apply(this);
			} else {
				if (res.identity == 0) {
					Gogo.restrictedAction();
				} else {
					$.jGrowl(res.message);
				}
				failure.apply(this);
			}
		}, 'json');
	},

	/**
	 * Add item to playlist
	 * 
	 * @param int id ID of item
	 * @param Function before Callback function
	 * @param Function after Callback function
	 */
	addAlbumToPlaylist : function(id, before, after) {
		var before = before || function() {};
		var after = after || function() {};
		before.apply(this);
	},

	/**
	 * Add item to playlist
         * 
         * einar: This need refactoring in time, works for now but not clean
	 * 
	 * @param int id ID of item
	 * @param Function before Callback function
	 * @param Function after Callback function
	 */
	addTrackToPlaylist : function(id, before, after) {
		var before = before || function() {};
		var after = after || function() {};
                var jsonSongData = null;
		before.apply(this);
                
                //get info
                $.ajax({
                  type: 'POST',
                  url: '/song/songMetaData/',
                  data: {id:id},
                  success: function(result) {
                          jsonSongData = result;
                      },
                      async: false,
                      dataType: 'json'
                   });                
                
		$.post('/gogoblaster/additem/', {id : id}, function(res) {
			if (res.error == false) {
				$.jGrowl(res.message);
                                //Add to DOM
                                $.get('/js/gogoplayer/templates/library-track.tpl', function(tmpl) {
                                    jsonSongData.trackData['duration'] = jsonSongData.duration;
                                    //check if already e
                                    $('#tbody-library').jqoteapp(tmpl, jsonSongData.trackData);
                                });    
			} else {
				if (res.identity == 0) {
					Gogo.restrictedAction();
				} else {
					$.jGrowl(res.message);
				}
			}
		}, 'json');
    

                //if active list open 
                var playlist_id = $('.active-nav').attr('id').split('-').pop();
                var playlist_safeName = $('.active-nav').attr('rel');
                
                //does playlist already contain 20 songs?
                if( $('#tbody-'+playlist_safeName+' tr').length >= 20 && s_str != 'Premium' && playlist_safeName != 'undefined')
                {
                    //$.jGrowl('You can only have 20 songs per playlist');
                }//song not already in list
                else
                {
                    $.post('/webservice-json/json-rpc-pipe.php', {method: 'addSongToPlaylist', 'params[]': [u_id,b_id,'tok3n',playlist_id,id]}, function(res) { 
                         // the server is happy, we will update the UI 
                        if (res.success) { // the server is happy, the user is not valid 

                            if($('#tbody-'+playlist_safeName+' .empty-playlist-state').length)
                            {   
                                $('#tbody-'+playlist_safeName+' .empty-playlist-state').remove();
                            }

                            $.get('/js/gogoplayer/templates/playlist-track.tpl', function(tmpl) {
                                jsonSongData.trackData['playlistSafeName'] = playlist_safeName;
                                jsonSongData.trackData['duration'] = jsonSongData.duration;
                                $('#tbody-'+playlist_safeName).jqoteapp(tmpl, jsonSongData.trackData);    
                            }); 
                        } else 
                        { } 
                    }, 'json');
                }
		after.apply(this);
	},


	/**
	 * Play one song
	 * 
	 * @param int id ID of item
	 * @param function callback Callback function
	 */
	playTrack : function(id, before, after) {  
		var before = before || function() {};
		var after = after || function() {};
		before.apply(this);
                playTrack(id)
		after.apply(this);
	},
	
	playSong: function(player_id,song_id, playlist_array){
                playAlbumTracks(song_id);
	}, //NOT IN USE
        playAlbum: function(player_id,song_id, playlist_array){
                playQueue(player_id,song_id, playlist_array);
	},
    /**
     * Make sure that numbers are between
     * zero and ten and all have at least one
     * point number
     */
    ratingNumberFormat: function( number ){
        if( parseFloat(number) >= 10 ){
            return 10;
        }else if(parseFloat(number) >= 0){
            return 0.0;
        }else{

        }
    },
	/**
	 * User not logged in, display login dialog.
	 */
	restrictedAction : function() {
        $('#modal-dialog-loginform').dialog({
            width:580,
            title: "Log in to gogoyoko",
            minWidth:'580px',
            autoOpen: false,
            resizable:false,
            modal: true,
            draggable: false
        });
		$('#modal-dialog-loginform').dialog('open');
		$('#modal-dialog-loginform').dialog('option', 'title',
				'You have to log in to do that!');
	},
	/**
	 * User passed playback limit, display login dialog.
	 */
	limitedAction : function() {
                $('#modal-playback-limit').show();
                $('#modal-dialog-loginform').dialog({
                    width:580,
                    title: "Log in to gogoyoko",
                    minWidth:'580px',
                    autoOpen: false,
                    resizable:false,
                    modal: true,
                    draggable: false
                });
                $('#modal-dialog-loginform').dialog('open');
                $('#modal-dialog-loginform').dialog('option', 'title',
				'Sign up or log in to keep listening!');
	}



};


