var unique=0;
var activeClip;
var ex = new Image();
var s1;
function getAmazonClip(theurl)
{
	//alert(theurl);
	s1.stop();
	$(activeClip).html('<a href="'+ theurl+ '" target="_tjsks">'+ $('.sample').html()+'</a>');
	
        $.ajax({url:"/profiles/getAmazon.php?amazon="+theurl,async:false,success:function(data){
		//alert(data);
		
		$(activeClip).html('<a href="'+ theurl+ '" target="_tjsks">'+ data +'</a>');

        }});
	s1.start();

}

function loadExt(list)
{ 
	titles = "";
	for(i=0;i <list.files.length; i++)
        {
        $.ajax({url:list.files[i],dataType:"xml",async:false,success:function(data){
                xx= 0;
                lastTitle = "";
		genre = list.files[i].substring(list.files[i].lastIndexOf("/")+1,list.files[i].indexOf(".xml"));

		$('<div class="subsection" id="_'+genre +'"><label>'+genre+'</label></div>').appendTo('.pallete');
		$('<div class="fieldset" id="_fieldset'+genre+'"/>').appendTo('#_'+genre);

            $("item",data).each(function(){
                title = $("title",this).text();
                if(titles.indexOf(title) ==  -1)
                {
                titles += " " + title;
                //if(++xx <= 10){
                {
                url = $("url",this).text();
                img = $("img",this).attr('src');
                price = $("price",this).text();

 		if(genre == "johnamaral" || genre == "topsellers")
                {
                        genre = "top sellers";
                }


                $('<div  id="div_'+ (++unique) + '" class="resize"><a border="0" target="_tup" title="['+ genre + '] ' + unescape(title)+':'+price+'" href="'+url+'"><img border="0" src="'+img+'"></a></div>\n').appendTo("#_fieldset"+genre);
                }
                }
            });
        }});
        }
}


 $(document).ready(function(){
			$('.addbutton').css('cursor','pointer').bind('click',function(){i
				slots =$('.clip').length;
				if(prompt("Enter the number of additional image locations and click 'ok'\nNote:you currently have " + slots + " slots.\n")){
				try{
					s1.pause();
					$('.clip:last').clone(false).appendTo('#customscroller');
				}catch(err){}
}
			});

			$('.subtractbutton').css('cursor','pointer').bind('click',function(){i
				slots =$('.clip').length;
				if(confirm("Do you wish to remove an image slot from the end of your scroller?\n")){
				try{
					$('.clip:last').remove();
				}catch(err){}
}
			});

			$('.close').live('click',function(){$('#CustomWebPage').css('visibility','hidden');s1.start();});
			$('#customscroller').hide();
                      

	//loadExt({files:['/xml/students/johnamaral.xml','/xml/topsellers.xml','/xml/rap.xml','/xml/jazz.xml','/xml/country.xml','/xml/rock.xml','/xml/opera.xml']});
	loadExt({files:['/xml/jazz.xml','/xml/country.xml','/xml/rock.xml','/xml/opera.xml']});
			$('#customscroller').show();

	$(".resize").draggable({revert:true,start:function(){
			$('.clip').droppable({ hoverClass:'drophover',accept: '.resize',drop:
				function(evt,ui){
							if($(ui.draggable).hasClass('custom'))
							{
								activeClip = $(this);		
								s1.stop();
								$('.amazonEntry').hide();
								$('.other').show();
								$('#CustomWebPage').css('visibility','visible');

							} else if($(ui.draggable).hasClass('amazon')){ 
								activeClip = $(this);		
								s1.stop();
								$('.amazonEntry').show();
								$('.other').hide();
								$('#CustomWebPage').css('visibility','visible');
							}else{
								$(this).html($(ui.draggable).html());
							}

				}
		});

		}
	});


	memO = false
	if($('.clip').length > 10)
	{
		memO = true;
	}

	 s1 = new EasyScroller('customscroller',{animationDelay:120,clipSpeed:2,memoryOptimized:memO});
		$('.pause').css('cursor','pointer').toggle(function(){s1.pause();},function(){s1.pause();});

	setTimeout("s1.pause()",1000);

});

function saveDialog(imgSrc,webPage)
{

	$(activeClip).html('<a href="'+ webPage + '" target="_tjsks"><img src="'+imgSrc+'"></a>');

}

