
/**
 * opens the url specified in a modalbox with the title as headline
 * 
 * @param url address to open in the modalbox
 * @param title headline to show
 * @return void
 */
function openModalbox(url,title){
	
	//check needed values
	if(typeof url == "undefined" || typeof title == "undefined"){
		return;
	}	
	
	jQuery("#ajax_content")
		.html("")
		.load(
			url,
			function() {
			  jQuery(this)
			        .dialog('destroy') //delete a dialog if it is still attached
			        .dialog({
			            title: title,
			            modal: true,
			            bgiframe: true,
			            autoOpen: true,
			            minWidth: 550,
			            maxWidth: 1000,
			            minHeight: 500,
			            maxHeight: 800,
			            height: 500,
			            width: 550,
			            draggable: true,
			            buttons: { 
			        		"Ok": function() { 
			        					jQuery(this).dialog("close");
			        		}
			        	},
			        	show: 'slide',
			        	hide: 'slide'
			        });
			}
		)
	;
};

/**
 * returns the filename for the hover-image
 * @param filename src-filename of the image
 * @return normal src but with "_hover" included in the filename 
 */
function getImageNameHover(filename){
	return filename.substring(0,filename.lastIndexOf("."))+"_hover"+filename.substring(filename.lastIndexOf("."));
}

/**
 * returns the filename for the normal (non-hover) image
 * @param filename src-filename of the image
 * @return normal src but without "_hover" included in the filename 
 */
function getImageNameNonhover(filename){
	return filename.replace("_hover","");
}

/**
 * replaces the current image with one name imagename_hover
 * @param eventObject object which triggered the event
 * @param isHover whether the hover state is given or not 
 * @return void
 */
function swapImage(eventObject,isHover){

	filename = jQuery(eventObject).find('img').attr('src');	

	if(isHover){
		new_name = getImageNameHover(filename);
	}else{
		new_name = getImageNameNonhover(filename);
	}
	//change the image
	jQuery(eventObject).find('img').attr('src',new_name);
}

/**
 * @see http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
 */
(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  jQuery.preLoadImages = function() {
	    var args_len = arguments.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	  }
})(jQuery);

//stuff we want to run on doc ready
jQuery(document).ready(function($) {
	  jQuery("#navigation li a")
		  .hover(
				function(){
			  		swapImage(this,true);
		  		},
		  		function(){
		  			swapImage(this,false);
		  		}	
		  );
	  
	  //preload all hover-images
	  jQuery("#navigation li a img").each(
			  function(){
				  jQuery.preLoadImages( getImageNameHover(jQuery(this).attr('src')));
			  }
	  );
	  
	  jQuery("#watchlist_link")
	  	.effect('highlight',{"speed":2000})
	  	.effect('highlight',{"speed":2000})
	  	.effect('highlight',{"speed":2000});	  
});
