<!--
function updateImageCount( id, image_index, path )
{
		$.get( path+"index.php?module=scraper&class=Favorites&event=image_update&artist_id="+id+"&image="+image_index, 
			function( data ) 
			{
				// jAlert( data );
			}
		);	
}

function check_image_logged( path )
{
	if( logged_in )
	{
		return true;
	}
	else
	{
		function callback()
		{
			logged_in = true;
		};
		login_signup_success_callback = callback;
		loadLogin( "#basic-modal-content", path, "Login to view larger image" );
	}
		
	return false;
}

function sendFavorite( id, path, type ) 
{
	sendFavoriteVariable( id, path, type, "add_favorite", "" );
}

function sendNotFavorite( id, path, type )
{
	sendNotFavoriteVariable( id, path, type, "add_favorite", "" );
}

function sendFavoriteVariable( id, path, type, variable, params )
{
	if( logged_in )
	{
		if( mpmetrics !== undefined )
		{
			mpmetrics.track('Favorite', { 'type': type });		
		}
		
		var search_for = "#"+variable+"_"+id;
		
		$( search_for ).html( "<img src=\"" + path + "images/loader.gif\"/>" );
		
		$.get( path+"index.php?module=scraper&class=Favorites&event=add_favorite&id="+id+"&type="+type+params, 
			function( data ) 
			{
				if( type == "picture" || type == "wallpaper" )
				{
					$( search_for ).html( "<img src=\""+path+"images/fav.png\" title=\"You like this picture\"> You like this picture <a href=\"javascript:sendNotFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"', '"+params+"' );\">(Remove)</a>" );
				}
				else if( type == "artist" )
				{
					$( search_for ).html( "<img src=\""+path+"images/fav.png\" title=\"You are a fan of this artist.\"> You are a fan of this artist <a href=\"javascript:sendNotFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"' );\">(Remove)</a>" );
				}
				else
				{
					$( search_for ).html( "<img src=\""+path+"images/fav.png\" title=\"This song is a favorite.\"> This is a favorite <a href=\"javascript:sendNotFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"' );\">(Remove)</a>" );
				}
			});			
	}
	else
	{
		// show the login dialog and let it be sorted out there.
		function callback()
		{
			logged_in = true;
			var search_for = "#"+variable+"_"+id;

			$( search_for ).html( "<img src=\"" + path + "images/loader.gif\"/>" );
			
			$.get( path+"index.php?module=scraper&class=Favorites&event=add_favorite&id="+id+"&type="+type+params, 
				function( data ) 
				{
					if( type == "picture" || type == "wallpaper" )
					{
						$( search_for ).html( "<img src=\""+path+"images/fav.png\" title=\"You like this picture\"> You like this picture <a href=\"javascript:sendNotFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"', '"+params+"' );\">(Remove)</a>" );
					}
					else if( type == "artist" )
					{
						$( search_for ).html( "<img src=\""+path+"images/fav.png\" title=\"You are a fan of this artist\"> You are a fan of this artist <a href=\"javascript:sendNotFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"' );\">(Remove)</a>" );
					}
					else
					{
						$( search_for ).html( "<img src=\""+path+"images/fav.png\" title=\"This song is a favorite.\"> This is a favorite <a href=\"javascript:sendNotFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"' );\">(Remove)</a>" );
					}
					// jAlert( data );
				});			
		};
		login_signup_success_callback = callback;
		loadLogin( "#basic-modal-content", path, "Login to continue" );
	}		
}


function sendNotFavoriteVariable( id, path, type, variable, params )
{
	if( params == "undefined" )
	{
		params = "";
	}	
	
	if( logged_in )
	{
		var search_for = "#"+variable+"_"+id;
		
		$( search_for ).html( "<img src=\"" + path + "images/loader.gif\">" );
		
		$.get( path+"index.php?module=scraper&class=Favorites&event=remove_fav&id="+id+"&type="+type+params, 
			function( data ) 
			{
				if( type == "picture" || type == "wallpaper" )
				{
					$( search_for ).html( "<a href=\"javascript:sendFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"', '"+params+"' )\" title='Add to favorites' class=\"button\"> Add to favorites </a>" );
				}			
				else if( type == "artist" )
				{
					$( search_for ).html( "<a href=\"javascript:sendFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"' )\" title='Become a fan of this artist' class=\"button\"> Become a fan </a>" );
				}
				else
				{
					$( search_for ).html( "<a href=\"javascript:sendFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"' )\" title='Add to favorites' class=\"button\"> Add to favorites </a>" );
				}
			});		
	}
	else
	{
		// show the login dialog and let it be sorted out there.
		function callback()
		{ 
			logged_in = true;
			var search_for = "#"+variable+"_"+id;
			
			$( search_for ).html( "<img src=\"" + path + "images/loader.gif\"/>" );
			
			$.get( path+"index.php?module=scraper&class=Favorites&event=remove_fav&id="+id+"&type="+type+params, 
				function( data ) 
				{
					if( type == "picture" || type == "wallpaper" )
					{
						$( search_for ).html( "<a href=\"javascript:sendFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"', '"+params+"' )\" title='Add to favorite pictures' class=\"button\"> Add to favorite pictures </a>" );
					}					
					else if( type == "artist" )
					{
						$( search_for ).html( "<a href=\"javascript:sendFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"' )\" title='Become a follower of this artist' class=\"button\"> Follow this artist </a>" );
					}
					else
					{
						$( search_for ).html( "<a href=\"javascript:sendFavoriteVariable( '"+id+"', '"+path+"', '"+type+"', '"+variable+"' )\" title='Add to favorites' class=\"button\"> Add to favorites </a>" );
					}
				});	
		};
		login_signup_success_callback = callback;
		loadLogin( "#basic-modal-content", path, "Session expired, please login" );
	}		
}

function deletePlaylist( id, path )
{
	function callbackDeletePlaylist( input_value )
	{
		if( input_value == true )
		{
			window.location = path+"index.php?module=scraper&class=Playlist&event=index&delete_playlist="+id;
		}
	}
	
	jConfirm( "Are you sure?", "Deleting playlist", callbackDeletePlaylist );
}

function emptyPlaylist( path )
{
	function confirmedCallback( input_value )
	{
		if( input_value == true )
		{
			window.location = path+"index.php?module=scraper&class=Playlist&event=index&empty_playlist=true";
		}
	}

	jConfirm( "Are you sure?", "Emptying playlist", confirmedCallback ); 
}

function savePlaylist( path )
{
	function savePlaylistCallback( input_value )
	{
		if( input_value == null )
		{
			jAlert( "Empty playlist name" );
		}
		else
		{
			window.location = path+"index.php?module=scraper&class=Playlist&event=index&playlist_name="+escape(input_value); 
		}		
	}
	
	jPrompt( "Enter playlist name:", "", "Save playlist", savePlaylistCallback );
}


function reportLink( id, path ) 
{
	function reportLinkCallback( input_value )
	{
		$.get( path+"index.php?module=scraper&class=MyAdmin&event=fix_video&song="+id+"&youtube="+escape(input_value),
			function( data )
			{
				if( input_value != null )
				{
					jAlert( "Youtube video " + input_value + " submitted." );
				}
				else
				{
					jAlert( "Submitted broken video alert." );
				}
			});				
	}
	
	function login_callback()
	{
		jPrompt( "Suggest a better video:", "", "Report incorrect video", reportLinkCallback );
	}
	
	if( logged_in )
	{
		jPrompt( "Suggest a better video:", "", "Report incorrect video", reportLinkCallback );
	}
	else
	{
		login_signup_success_callback = login_callback;
		loadLogin( "#basic-modal-content", path, "Login to report video" );
	}
}

function autofind( id, path ) 
{
	var original_text = " Autofind better video. ";
	
	if( $( "#autofind" ).html() == original_text )
	{
		$( "#autofind" ).html( " Autofind better video. <img src=\"" + path + "scripts/images/spinner.gif\" alt=\"Processing\"/>" );

		$.get( path+"index.php?module=scraper&class=Search&event=search_youtube&song_id="+id,
		function( data )
		{
			$( "#autofind" ).html( " Autofind better video. " );
			
			if( data == "OK" )
			{
				// reload the page
				window.location.reload( true );
			}
			else if( data == "ERROR" )
			{
				$( "#autofind" ).html( original_text );
				jAlert( "Error unable to find better video." );
			}
		});
	}		
}

function getSecondary( id, path )
{
	var result = "";
	var submit_url = path+"index.php?module=scraper&class=Youtube&event=get_youtube&song_id="+id+"";
	
 	$.ajax({ 
         url: submit_url, 
         success: function(data) 
         		  { 
					result = data;
                  }, 
         async:  false 
			});
	return result;		
}

function reportLinkA( id, path )
{
		var submit_url = path+"index.php?module=scraper&class=MyAdmin&event=fix_video&song="+id+"&automated=true&youtube=";
		$.get( submit_url,
			function( data )
			{
			});	
}

var timer = false; 

function resetImg(name, id) { 
    jQuery(name).attr('src', 'http://img.youtube.com/vi/' + id + '/1.jpg'); 
    // jQuery(name).css('border', '1px solid #fff'); 
 
    if (timer) { 
        clearTimeout(timer); 
    } 
} 
 
function nextImg(name, id, nr) { 
    var myId = jQuery(name).attr('id'); 
    jQuery(name).attr('src', 'http://img.youtube.com/vi/' + id + '/' + nr + '.jpg'); 
    //jQuery(name).css('border', '1px solid #333'); 
 
    nr++; 
    if (nr > 3) { 
        nr = 1; 
    } 
 
    timer = setTimeout("nextImg('#" + myId + "', '" + id + "', " + nr + ");", 1000); 
} 
 
jQuery(document).ready(function() 
{ 
	jQuery(".youtube_image").hover(function() { 
        nextImg('#' + jQuery(this).attr('id'), jQuery(this).attr('rel'), 1); 
    }, function() { 
        resetImg('#' + jQuery(this).attr('id'), jQuery(this).attr('rel')); 
    }); 
});

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function externalLinks() {   
 if (!document.getElementsByTagName) return;   
 var anchors = document.getElementsByTagName("a");   
 for (var i=0; i<anchors.length; i++) {   
   var anchor = anchors[i];   
   if (anchor.getAttribute("href") &&   
       anchor.getAttribute("rel") == "external")   
     anchor.target = "_blank";   
 }   
}   

//  by Mredkj (Add commas)
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function loadSignup( elementSelector, path ) 
{
	$( elementSelector ).load( path+"index.php?module=scraper&class=MyLogin&event=signup_menu", 
	function() {
		$('#simplemodal-container').css( {"width": "320px", "height": "490px"});
		$(window).resize();
		
		attach_facebook_click_event();
		
		$("#join > form").submit(function(){
		
		// hide the signup 'Submit' button
		$("#submit2").hide();

		// Show gif spinning rotator
		$("#ajax_signup_loading").show();

		// 'this' refers to the current submitted form
		var str = $(this).serialize();

		// Start AJAX Call
		$.ajax({
			type: "POST", 
			url: path+"index.php?module=scraper&class=MyLogin&event=ajax_signup",   // Sent the login info to this page
			data: str,
			success: function(msg){
				
				// Show submit2 button
				$("#submit2").show();
				
				// Hide gif spinning rotator
				$("#ajax_signup_loading").hide();
				
				if( msg == "OK" ) // signed up!
				{
					// mpmetrics defined.
					if( mpmetrics !== undefined )
					{
						mpmetrics.track("signup_complete");
					}
					
					if( _gaq !== undefined  )
					{
						_gaq.push(['_trackEvent', 'signup', 'signup_complete' ]);
						_gaq.push(['_trackPageview', '/thankyou.html']);
					}

					// google goal tracking. Successful signup
					// $('#google_goal').html( "<IFRAME NAME='google_signup' SRC='/thankyou.html' WIDTH='0' HIEGHT='0' FRAMEBORDER='0'></IFRAME>" );
					
				 	var login_response = "<div id='logged_in'>" +
					 "<div style='width: 350px; float: left; margin-left: 70px;'>" + 
					 "<div style='width: 40px; float: left;'>" +
					 "<img style='margin: 10px 0px 10px 0px;' align='absmiddle' src='/scripts/images/ajax-loader.gif' width='32' height='32'>" +
					 "</div>" +
					 "<div style='margin: 10px 0px 0px 10px; float: right; width: 300px;'>" + 
					 "You've been signed up and are being logged in! <br /> Please wait...</div></div>";  

					$('#simplemodal-container').css("width","500px");
					$('#simplemodal-container').css("height","120px");
					$('#simplemodal-container').html( login_response );
					$(window).resize();
				 
					setTimeout('success_login_signup()', 3000); 		
				}
				else
				{
					// jAlert( msg );
					$('#simplemodal-container').css("width","640px");
					$(window).resize();
					$("#signup_errors").html( msg );
				}
			 
		} // end success function

		}); // end ajax call
			
		return false;
			
	});
	}		
	);

	
}

function success_login_signup()
{
	$.modal.close();
	
	if( login_signup_success_callback != null )
	{
		login_signup_success_callback();
	}

	// $("#login_link").html( "<li><a href='/logout' class='linka'>LOGOUT</a></li>" );
}


function loadLogin( elementSelector, path, title ) 
{
	$( elementSelector ).load( path+"index.php?module=scraper&class=MyLogin&event=login_menu", 
	function() {
		$('#simplemodal-container').css( {"width": "320px", "height": "320px"});
		$(window).resize();
		$('#basic-modal-content').modal();
		$('#signup-message').html( title );
		
		// attach login form submit event
		$("#status > form").submit(function(){  

		// Hide 'Submit' Button
		$('#submit').hide();

		// Show Gif Spinning Rotator
		$('#ajax_loading').show();

		// 'this' refers to the current submitted form  
		var str = $(this).serialize();  

		// Start AJAX Call

		$.ajax({  
		    type: "POST",
		    url: path + "index.php", 
		    data: str,  
		    success: function(msg){  
		   
		$("#status").ajaxComplete(function(event, request, settings){  
		 
		 // Show 'Submit' Button
		$('#submit').show();

		// Hide Gif Spinning Rotator
		$('#ajax_loading').hide();  

		 if(msg == 'OK') // LOGIN OK?
		 {  
			if( mpmetrics !== undefined )
			{
				mpmetrics.track( "login_complete" );
			}
			
			if( _gaq !== undefined )
			{
				_gaq.push(['_trackEvent', 'login', 'login_complete' ]);
			}
			
		 	var login_response = '<div id="logged_in">' +
			 '<div style="width: 350px; float: left; margin-left: 70px;">' + 
			 '<div style="width: 40px; float: left;">' +
			 '<img style="margin: 10px 0px 10px 0px;" src="'+path+'scripts/images/ajax-loader.gif">' +
			 '</div>' +
			 '<div style="margin: 10px 0px 0px 10px; float: right; width: 300px;">'+ 
			 "You are successfully logged in! <br /> Please wait while you're redirected...</div></div>";  

			$('#simplemodal-container').css("width","500px");
			$('#simplemodal-container').css("height","120px");
			$('#simplemodal-container').html( login_response );
		 
			setTimeout('success_login_signup()', 3000); 
		 }  
		 else // ERROR?
		 {  
		 	var login_response = msg;
		 	$('#login_response').html(login_response);
		 }  
		 });  
		 }  
		  });  
		// End AJAX Call
		return false;
		}); // end submit event
		return false;
		}); // end load
} // end function


