/**author Lorenzo Radice*/

/**main function
*@param xmlFile the XML file to read
*@param tagId the HTML tag Id to fit with new nodes
*@param boxWidth optional default video width
*@param boxHeight optional default video height  */
function executeCoverflow(folder, xmlFile, tagId, boxWidth, boxHeight){
	var html = '';
	var borderWidth = 90; //NB: keep these border values sync with milkbox.css
	var borderHeight = 98;
	var dataArray = new Array();
	dataArray = collectCoverflowDataFromXML(xmlFile, true);
	//dataArray = scaleImages(dataArray, 232, 30);
	
	/*default milkbox size: 600x333*/
	var width = ((boxWidth == undefined || boxWidth.length == 0 || boxWidth == null || isNaN(boxWidth) )? 600 : boxWidth) - borderWidth;
	var height = ((boxHeight == undefined || boxHeight.length == 0 || boxHeight == null || isNaN(boxHeight) )? 333 : boxHeight) - borderHeight;
	
	for (i=0; i<dataArray.length; i++){
		html = html + createHTMLImageAndLinkTags(folder, dataArray[i]);
	}
	var slider = document.getElementById(tagId);
	slider.innerHTML = html;
	
	return dataArray.length;
	
	
	
	/*--SUPPORT FUNCTIONS--*/
	
	/**Takes an associative array element and converts to <a ...><img/></a> tags*/
	function createHTMLImageAndLinkTags(folder, data){
		var htmlNode;
		longdesc = data[0].type;
		//alert('coverfunctions.js: '+data['type']); 
		switch(data[0].type){
			case 'img':
				var aTag = '<a href="'+data[0].link+'" target="'+data[0].wtarget+'" rel="milkbox['+data[0].titolo+']">';
				var imgTag = '<img src="'+folder+'/coverflow/'+data[0].img+'" alt="'+data[0].titolo+'" title="'+data[0].titolo+'" longdesc="'+longdesc+'"/>'; // width="'+data[0].img_size+'px" />'; 
				(data[0].link!=undefined || data[0].link!=null || data[0].link!='')? htmlNode = aTag+imgTag+'</a>' : htmlNode = imgTag;
				break;
			case 'video':
				var imgTag = '<img src="'+folder+'/coverflow/'+data[0].cover+'" alt="'+data[0].titolo+'" title="'+data[0].titolo+'" longdesc="'+longdesc+'"/>'; // width="'+data[0].img_size+'px" onClick="loadFlashMovie();"/>';
				var videoId = data[0].img.split("/")[data[0].img.split("/").length-1].split(".")[0];
				var aTag = '<a href="'+data[0].img+'" target="'+data[0].wtarget+'" rel="video['+data[0].titolo+']" rev="width:'+width+', height:'+height+'">';
				//var aTag = '<span id="'+videoId+'"  onclick="playVideo()">';
				htmlNode = aTag+imgTag+'</a>';
				break;
		}
		return htmlNode;
	};
	
	
	/**scales images: the central one is the biggest, others are scaled simmetrically
	* @param the array with data
	* @param width the width of the center image
	* @param decreaseNum the  image size decrement (pixels)
	* @deprecated */
	function scaleImages(dataArray, fullWidth, decreaseNum){
		var  decrease = 0;
		var numCenteredImg = parseInt(dataArray.length/2);  //(eg: 7/2=3.5->3 --> 4a img centrale)
		dataArray[numCenteredImg][0].img_size = fullWidth;
		for (prevImgNum = numCenteredImg - 1, nextImgNum = numCenteredImg + 1;
			prevImgNum>=0 && nextImgNum<dataArray.length;
			prevImgNum--, nextImgNum++){
				decrease += decreaseNum;
				dataArray[prevImgNum][0].img_size = fullWidth - decrease;
				dataArray[nextImgNum][0].img_size = fullWidth - decrease;
		}
		return dataArray;
	};
	
	
}
	
	
/*--GLOBAL FUNCTIONS (used in mainfile.html)--*/	
	
/**SWFObject 2.2: loads SWF with JavaScript*/
function loadFlashMovie(){
	var flashvars = {};
	var params = {};
	var attributes = {};
	swfobject.embedSWF("img/main.swf", "main", "232", "130", "9.0.0", false, flashvars, params, attributes);
}
	
/**
 * @param content the string content to check
 * @return true if it's an image, false otherwise
 */
function isImage(content){
     var regexp = new RegExp("[.](jpg|png|bmp|gif|raw|img|pcx|gif|cgm|svg|pns|jps|mpo|vml|xps|eps|jpeg|jfif|tiff|exif)$","i");
     return regexp.test(content);
}

/**
 * @param content the string content to check
 * @return true if it's a video, false otherwise
 */
function isVideo(content, extension){
     var regexp = new RegExp("[.](swf|avi|mp4|mpeg4|mp4v|mpeg|mpg|3gp|3g2|asf|asx|f4v|flv|mov|rm|vob|wmv)$","i");
	 if (extension)
		regexp = new RegExp("[.]("+extension+")$","i");
     return regexp.test(content);
}
	
	
