/**
 * 
 * Common jQuery plugins for the UI
 * 
 * @category gogoyoko
 * @author einarvalur
 */




	/**
	 * 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 = {
			/**
			 * 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{
					
				}
			},
            /**
             * 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(){};
                    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)
                            {
                                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
                 * @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(){};
                        before.apply(this);
                        $.post('/gogoblaster/additem/',{id:id}, function(res){
                            if(res.error == false) { top.frames["Player"].addExternalSong( id ); }
                            $.jGrowl(res.message);
                        }, '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);
                                after.apply(this);
                }
	};

		



    //Identities switch
    $(document).ready(function(){
        $("#browse-as").change(function()
        {
                $('#identity-change-browsingas-form').submit();
        });
    });



/**
 * For IE
 * just so that is I forget to remove a "console.log()"
 * statement from the code, IE will still work
 */
//var console = console || { log:function(){} }

/**
 * 
 */
(function($){
	
	/**
	 * Rating plugin
	 * See {@see http://deepspace.gogoyoko.com/confluence/display/gogonew/How+to+add+star+rates } for more information
	 */
	$.fn.rating = function(){
		this.each(function(){ 
			var count = 0;
			var that = this;
			$('li',this).each(function(){
				var input = $(this).find('input').css('display','none');
				var self = this;
				if( input.attr('checked') ){ count++; }
				var link = $('<a href="" class="'+ ((input.attr('checked'))?'active':'inactive') +'" >&nbsp;</a>').appendTo(this);
					link.mouseover(function(){ 
						$(self).parent('ul').find('li a').removeClass('active').addClass('inactive');
						var alls = $('li',that);
						for( var i=0;i<alls.length;i++ ){
							$('a',alls[i]).removeClass('inactive').addClass('active');
							if( alls[i]==self ){ break; }
						}
	
					});
					link.mouseout(function(){
						$('a',that).removeClass('active').addClass('inactive');
						var alls = $('li',that);
						for( var i=0;i<count;i++ ){
							$('a',alls[i]).removeClass('inactive').addClass('active');
						}
					});
					link.click(function(event){
						event.preventDefault();
						$(this).blur();
						count = 0;
						var alls = $('li',that);
						for( var i=0;i<alls.length;i++ ){
							count++;
							if( alls[i]==self ){ break; }
						}						
						$.post( $(that).attr('action'),{ value: $(this).parent('li').find('input').val() } );
						
					});
			});
			
		});
	};//end of $.fn.rating = function()
	
	
	/**
	 * CONTAINS
	 * a workaround for the lack of the contains() functino in FF
	 * @see http://www.quirksmode.org/blog/archives/2006/01/contains_for_mo.html
	 */
	if (window.Node && Node.prototype && !Node.prototype.contains){
		Node.prototype.contains = function(arg){
			return !!(this.compareDocumentPosition(arg) & 16);
		};
	}
	
	/**
	 * When a user selects an input files,
	 * it's content is removed. If the user
	 * changes, the new value will stay, else
	 * the old value is returned
	 */
	$.fn.clear_input = function(){
		return this.each(function(){
			var _key = $(this).val();
			$(this).focus(function(event){
				if( $(this).val() == _key ){
					$(this).val('');
				}else{
					this.select();
				}
			});
			$(this).blur(function(){
				if( $(this).val() == '' ){
					$(this).val(_key);
				}
			});
		});
	}
	
	/**
	 * Create load animation.
	 * 
	 * this will append an animation into the container
	 * 
	 * $('container').load_animation();
	 * $('container').load_animation('destroy');
	 */
	$.fn.load_animation = function(){
		var arg = arguments[0] || null;
		return this.each(function(){	
			var _changer = function(){
				$(cont).css("backgroundPosition", "0px "+pos+"px"); 
				setTimeout(_changer,30);
				pos -= 50;
			}
			if( arg==null ){
				var cont = $('<div class="gogoyoko-load-animation"></div>').appendTo(this);
				var pos = -200;
				_changer();
			}else if(arg=='destroy'){
				$('.gogoyoko-load-animation',this).remove();
			}
		});
		
	}
	
	
	/**
	 * Accordion menu
	 * @todo finish
	 */
	$.fn.menu_accordion = function(){
		return this.each(function(){
			$('> li a',this).click(function(){
				$(' > ul',$(this).parent()).toggle();
			});
		});
	}
	
	/**
	 * MENU
	 * 
	 * A menu is like a dialog that has no header or footer
	 * and therefor no close/cancel/ok... buttons
	 */
	$.fn.menu = function(trigger){
		var d = this[0];
		//REMOVE
		//	remove menu from sight (hide)
		var clean = function(event){
			if(!d.contains(event.target)){
				$(d).stop().animate({
					top: '-=60px',
					opacity: 'toggle'
				},'middle');
				$(document).unbind('mouseup',clean);				
			}
		}
		//TRIGGER EVENT
		//	on trigger... display menu or hide
		$(trigger).click(function(event){
			event.preventDefault();
			event.stopPropagation();
			if( $(d).css('display') == 'none' ){
				$(d).stop().animate({
					top: '+=60px',
					opacity: 'toggle'
				},'middle');
				$(document).bind('mouseup',clean);
			}else{
				clean(event);
			}
		});
		//RETURN
		//	return the complete menu
		//	ie. return the wrapper DIV element
		return $(d);
	}
	 
	 
	 $.fn.menu_show = function(anchor){
		 
		 return this.each(function(){
				//REMOVE
				//	remove menu from sight (hide)
				var clean = function(event){
					if(!d.contains(event.target)){
						$(d).stop().animate({
							top: '-=25px',
							opacity: 'toggle'
						},'middle');
						$(document).unbind('mouseup',clean);				
					}
				}
				//WRAPPER
				//	wrapp element in menu markup
				var d = $(this).wrap([
			        '<div style="display:none; position:absolute;" class="ui-dialog ui-widget-content ui-corner-all undefined">',
			        '<div class="ui-dialog-content ui-widget-content"></div>',
			        '</div>'
			        ].join('')).parents('.ui-dialog')[0];
				var pos = $(anchor).offset();
				$(d).css({
//					top: pos.top,
//					left: pos.left
				});
				
				$(d).stop().animate({
					top: '+=5px',
					opacity: 'toggle'
				},'middle');
				$(document).bind('mouseup',clean);
			 
		 });
	 }	
	
})(jQuery);


/**
 * Confirm navigate away from form
 * 
 * If a form has been changed, and is not
 * beeing submitted when navigated away from a
 * page... "R U sure" modal will be displayed
 */
(function($){
	$.fn.changeconfirm = function(message){
		var msg = message || '';
		this.each(function(){
			$(this).change( function(){
				window.onbeforeunload = function(){
					return msg;
				}
			});
			$(this).submit(function(){
				window.onbeforeunload = function(){}
			});
		});
	}
})(jQuery);

