var srcurl = "/common/include/top/";

function  elemWrite(category,photo,url){
            srcurl = srcurl + "photo/" + photo;
            $(".pht-top-voice").find("img").attr({
      				src : srcurl,
      				alt : category
    				})
    				$("#btn_sample").parent("a").attr("href",url)

						$("#result").show()
						
}

function  parse_xml(data){

            var rand = Math.floor(Math.random() * $(data).find("category > item").length);
            var cnt = 0;

            $(data).find("item").each(function () {
                var $item = $(this);

                cnt++;
                
                    if(cnt == rand){
                    var category = $item.parent("category").attr("name");
                    var photo = $item.children("photo").text();
                    var url = $item.children("url").text();
            elemWrite(category,photo,url);
                    }
            });
            
}

$(function(){
    
    
            //Select all anchor tag with rel set to tooltip
            $('.carousel').mouseover(function(e) {
               
               //Grab the title attribute's value and assign it to a variable
               var tip = $(this).attr('title');   
               
               //Remove the title attribute's to avoid the native tooltip from the browser
               $(this).attr('title','');
               
               //Append the tooltip template and its value.
               		$(".carousel").append('<div id="tooltip"><img src="http://www.happyleaf.biz/common/images/icon_tooltip.gif"/></div>');  
                                    
               
            }).mousemove(function(e) {
               //Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
               $('#tooltip').css('top', e.pageY + -50 );
               $('#tooltip').css('left', e.pageX + -15 );
               
            }).mouseout(function() {
               //Put back the title attribute's value
               $(this).attr('title',$('.tipBody').html());
               //Remove the appended tooltip template
               $(".carousel").children('div#tooltip').remove();
               
            });
            
            
    $.ajax({
        type: 'GET',
  		  cache: false,  
        url: srcurl + "data.xml",
    		success: parse_xml
    });
    
    
});
