// JavaScript Document

function onLoad() {
	transformImages();
	
	if($('flash_description')) {
		// Main Page	
	}
	if($('article')) {
		// Theres an article on this page
		// go through the text in the body of the article
		// and look for wrestler names	
		checkWrestlerNames();
	}
}

function checkWrestlerNames() {
	new Ajax.Request("/ajax/wrestlers.php", {onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			var str = $('articleBody').innerHTML;
			ids = Array();
			json.wrestlers.each( function(wrestler) {
				if(str.match(wrestler.name)) {
					ids.push(parseInt(wrestler.id));
					str = str.replace(new RegExp(' '+wrestler.name+'([^A-Za-z0-9])','g'),' <span wrestlerid="'+wrestler.id+'" class="moreInfo wrestler_'+wrestler.id+'">'+wrestler.name+'</span>$1');

					h3 = new Element('h3').update(wrestler.name);
					img = new Element('img',{'src':'/images/roster/'+wrestler.picture, 'alt':wrestler.name});
					
					attributes = new Element('ul');
					extra = Array('hometown','height','weight','finisher');
					
					extra.each( function(attr) {
						if(wrestler[attr].length > 0) {
							attributeContainer = new Element('li');
							attributeType = new Element('span').update(capitaliseFirstLetter(attr));
							attributeValueContainer = new Element('ul');
							attributeValueHolder = new Element('li');
							attributeValue = new Element('span').update(wrestler[attr]);
							
							attributeValueHolder.insert({'bottom':attributeValue});
							attributeValueContainer.insert({'bottom':attributeValueHolder});
							attributeContainer.insert({'bottom':attributeType});
							attributeContainer.insert({'bottom':attributeValueContainer});
							attributes.insert({'bottom':attributeContainer});
						}
					});
						
					rosterItem = new Element('div',{'class':'roster_item'});
					
					rosterItem.insert({'bottom':img});
					rosterItem.insert({'bottom':h3});
					rosterItem.insert({'bottom':attributes});
					
					
					div = new Element('div',{'id':'wrestler_'+wrestler.id,'class':'moreInfo'}).insert({'bottom':rosterItem});
					$('body').insert({'bottom':div});	
				  }	
			  });
			  $('articleBody').innerHTML = str;
			  ids.each( function(wrestlerID) {
				$$('span.wrestler_'+wrestlerID).each( function(span) {
					new Tooltip(span, {mouseFollow: false,opacity: 100, 'content': 'wrestler_'+wrestlerID});
				});				  
			  });
			  /*
			  $$('span.moreInfo').each( function(span) {
			  });
			  /*
				new Ajax.Request("/ajax/wrestlers.php", {parameters:"ids="+ids.toJSON(),
					onSuccess: function(transport) {
						var json = transport.responseText.evalJSON();
						var wrestlers = json.wrestlers;
						
						wrestlers.each( function(wrestler) {
							h3 = new Element('h3').update(wrestler.name);
							img = new Element('img',{'src':'/images/roster/'+wrestler.picture, 'alt':wrestler.name});
							
							attributes = new Element('ul');
							extra = Array('hometown','height','weight','finisher');
							
							extra.each( function(attr) {
								if(wrestler[attr].length > 0) {
									attributeContainer = new Element('li');
									attributeType = new Element('span').update(capitaliseFirstLetter(attr));
									attributeValueContainer = new Element('ul');
									attributeValueHolder = new Element('li');
									attributeValue = new Element('span').update(wrestler[attr]);
									
									attributeValueHolder.insert({'bottom':attributeValue});
									attributeValueContainer.insert({'bottom':attributeValueHolder});
									attributeContainer.insert({'bottom':attributeType});
									attributeContainer.insert({'bottom':attributeValueContainer});
									attributes.insert({'bottom':attributeContainer});
								}
							});
								
							rosterItem = new Element('div',{'class':'roster_item'});
							
							rosterItem.insert({'bottom':img});
							rosterItem.insert({'bottom':h3});
							rosterItem.insert({'bottom':attributes});
							
							
							div = new Element('div',{'id':'wrestler_'+wrestler.id,'class':'moreInfo'}).insert({'bottom':rosterItem});
							$('body').insert({'bottom':div});	
							$$('span.wrestler_'+wrestler.id).each( function(span) {
								new Tooltip(span, {mouseFollow: false,opacity: 100, 'content': 'wrestler_'+span.readAttribute('wrestlerid')});
							});
						});
					}
					, method:'get' }
				);

			  //alert(ids.toJSON());
			 /* $$('span.moreInfo').each( function(span) {
				  //alert("Starting request for: "+span.innerHTML+" ("+span.readAttribute('wrestlerid')+")");
				  	new Ajax.Request("/ajax/wrestler.php", {parameters:"id="+span.readAttribute('wrestlerid'),
					 onSuccess: function(transport) {
						var json = transport.responseText.evalJSON();
						//alert(json.name);
						h3 = new Element('h3').update(json.name);
						img = new Element('img',{'src':'/images/roster/'+json.picture, 'alt':json.name});
						
						attributes = new Element('ul');
						extra = Array('hometown','height','weight','finisher');

						extra.each( function(attr) {
							if(json[attr].length > 0) {
								attributeContainer = new Element('li');
								attributeType = new Element('span').update(capitaliseFirstLetter(attr));
								attributeValueContainer = new Element('ul');
								attributeValueHolder = new Element('li');
								attributeValue = new Element('span').update(json[attr]);
								
								attributeValueHolder.insert({'bottom':attributeValue});
								attributeValueContainer.insert({'bottom':attributeValueHolder});
								attributeContainer.insert({'bottom':attributeType});
								attributeContainer.insert({'bottom':attributeValueContainer});
								attributes.insert({'bottom':attributeContainer});
							}
						});
						
						rosterItem = new Element('div',{'class':'roster_item'});
						
						rosterItem.insert({'bottom':img});
						rosterItem.insert({'bottom':h3});
						rosterItem.insert({'bottom':attributes});
						
						
						div = new Element('div',{'id':'wrestler_'+json.id,'class':'moreInfo'}).insert({'bottom':rosterItem});
						$('body').insert({'bottom':div});	
						$$('span.wrestler_'+json.id).each( function(span) {
							new Tooltip(span, {mouseFollow: false,opacity: 100, 'content': 'wrestler_'+span.readAttribute('wrestlerid')});
						});
					} , onFailure: function() {alert("HI")}
					, method:'get' });	
				  //alert(spans[i].innerHTML);
				  //$(spans[i]).observe('mouseover', function(event){
					//  wrestlerDetails($(Event.element(event)).readAttribute('wrestlerid'));
					 //});
			  });*/
		 }, method:'get' });		
}
function wrestlerSuccess(span) {	
//alert(transport.responseText);
	//alert("TEST");$('testSpan')
	//new Tooltip($('test'), {mouseFollow: false,'content': 'wrestler_'+$('test').readAttribute('wrestlerid')});
	//new Tooltip(span, {mouseFollow: false,'content': 'wrestler_'+span.readAttribute('wrestlerid')});
}
function capitaliseFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}
function transformImages() {
	// Change all images that have a link attached
	// that is in the image directory.
	// Instead of opening the url, the image will be
	// display inside a lightbox.
	
	// Get all images wrapped in an anchor
	images = $$('a img');
	
 	// Loop through each image
	images.each(function(image) {
		a = $(image).up('a');
		if(a.href.substr(0,35) == "http://www.w3lwrestling.com/images/"){ 
			a.rel = "lightbox[Images]";
		}
	});	
}

function isObject(targetID){
   var isFound = false;
   var el = $(targetID);
   if(el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED")){
	  isFound = true;
   }
   return isFound;
}
 
//Support function: creates an empty
//element to replace embedded SWF object
function replaceSwfWithEmptyDiv(targetID){
   var el = $(targetID);
   if(el){
	  var div = document.createElement("div");
	  el.parentNode.insertBefore(div, el);
 
	  //Remove the SWF
	  swfobject.removeSWF(targetID);
 
	  //Give the new DIV the old element's ID
	  div.setAttribute("id", targetID);
   }
}
 
function loadSWF(url, targetID){
 
   //Check for existing SWF
   if(isObject(targetID)){
	  //replace object/element with a new div
	  replaceSwfWithEmptyDiv(targetID);
   }
 
   //Embed SWF
   if (swfobject.hasFlashPlayerVersion("7")) {
	  var attributes = { data: url, width:"425", height:"356" };
	  var params = {};
	  var obj = swfobject.createSWF(attributes, params, targetID);
   }
}

function wrestlerDetails(id) {
	// show a box with wrestler details in it
	new Ajax.Request("/ajax/wrestler.php", {parameters:"id="+id, onSuccess: function(transport) {
			  var json = transport.responseText.evalJSON();
			  alert(json.name);
			  
		 }, method:'get' });		
}

function updateVideo(id) {
	new Ajax.Request("/ajax/video.php", {parameters:"id="+id, onSuccess: function(transport) {
			  var json = transport.responseText.evalJSON();
			  
			  // change the showcase video
			  loadSWF('http://www.youtube.com/v/'+json.url+'?enablejsapi=1&playerapiid=ytplayer','flash_player');
			  $('flash_title').innerHTML = json.title;
			  $('flash_description').innerHTML = json.description;
			  
			  // remove the thumbnail of the new showcase video
			  $('video_'+json.id).remove();
			  // add the thumbnail of the old showcase video
			  addThumbnail($('showcase_video').value);
			  
			  $('showcase_video').value = json.id;
		 }, method:'get' });
}
function addThumbnail(id) {
	new Ajax.Request("/ajax/video.php", {parameters:"id="+id, onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			
			var h5 = new Element('h5').update(json.title);
			var img = new Element('img',{'src':'http://i1.ytimg.com/vi/'+json.url+'/default.jpg','alt':'YouTube','width':120, 'height':90});
			var a = new Element('a',{'href':'/video/'+json.urlname+'/','onclick':'updateVideo('+json.id+');return false;'}).update(img);
			a.insert(h5,{position:'after'});
			var div = new Element('div', {'class':'thumbnail','id':'video_'+id}).update(a);
			
			$('thumbnails').insert(div,{position: 'after'});
		}, method:'get' });
}


Event.observe(window, 'load', onLoad);
