/********************************************************************************
* Created by Samir Sijeric 10/12/2009 SharejPoint.com
*
* Purpose: Rotates images in a Picure library
*
* Special thank to 
* Mike Alsup at http://malsup.com/jquery/ for his Cycle plugin
* Leandro Vieira Pinho - http://leandrovieira.com for his lightBox plugin
*********************************************************************************/
//Include CSS and JS
jP.includePath = 'http://www.sharejpoint.com/src/';
jP.include([
'jquery/plugins/jquery.cycle.js'
,'jquery/plugins/jquery-lightbox-0.5/js/jquery.lightbox-0.5.pack.js'
,'jquery/plugins/jquery-lightbox-0.5/css/jquery.lightbox-0.5.css'
]);

myjPart.ShowConfigDefaults = [];
	
myjPart.setjPartOptions({Plugin:{Name:'ImageRotator', Version:'2.0', Origin:'SharejPoint', Description:'Allows SharePoint administrator to display images from the image library', Developer:'Samir Sijercic'}});
//Override webpart functions
myjWebpart.readConfig = function ()
{
	var webpartOptions = {};
	$(this.ConfigTableDiv.find("input[type=text]")).each(function (idx, item) {
		webpartOptions[item.name.replace(/ /g, '')] = item.value;
	});
	$(this.ConfigTableDiv.find("input[type=checkbox]")).each(function (idx, item) {
		webpartOptions[item.name.replace(/ /g, '')] = item.checked;
	});
	this.Options.Local = webpartOptions;
}
myjWebpart.showConfig = function () {
	var configTable = this.ConfigTableDiv;
	var webpartOptions = this.Options.Local;
	//Remove all previously created config nodes
	$(configTable).find(".configRow").remove();
	
	if(webpartOptions)
	{
		configTable.append("<tr class='configRow'><td>SiteURL:</td><td><input type=text name='SiteURL' value='" + (webpartOptions.SiteURL || "") + "' style='width:500px' /></td></tr>");
		configTable.append("<tr class='configRow'><td>List Name:</td><td><input type=text name='List Name' value='" + (webpartOptions.ListName || "") + "' style='width:500px' /></td></tr>");
		var checked = "";
		if(webpartOptions.Thumbnails){checked = "checked"}else{checked = ""};
		configTable.append("<tr class='configRow'><td>Thumbnails:</td><td align='left'><input type=checkbox name='Thumbnails' " + checked + " /></td></tr>");
		if(webpartOptions.LightBox){checked = "checked"}else{checked = ""};
		configTable.append("<tr class='configRow'><td>Light Box:</td><td align='left'><input type=checkbox name='LightBox' " + checked + " /></td></tr>");
	}
	else
	{
		configTable.append("<tr class='configRow'><td>SiteURL:</td><td><input type=text name='SiteURL' value='' style='width:500px' /></td></tr>");
		configTable.append("<tr class='configRow'><td>List Name:</td><td><input type=text name='List Name' value='' style='width:500px' /></td></tr>");
		configTable.append("<tr class='configRow'><td>Thumbnails:</td><td align='left'><input type=checkbox name='Thumbnails' /></td></tr>");
		configTable.append("<tr class='configRow'><td>Light Box:</td><td align='left'><input type=checkbox name='LightBox' /></td></tr>");

	}
}

//Start Webpart logic code ----------------------------------- 
$(document).ready(function() { //Wait page to load and then apply webpart logic
	if(myjParts[JpartProcessing] != null)
	{
		var thisWebpart = myjParts[JpartProcessing];
		try
		{
			//jPart user code goes here ---------------------------
			if(thisWebpart.Options.Local != null)
			{
				var ImageListOptions = {};
				thisWebpart.MainDiv.append("<div class='slideshow'></div>");
				if(thisWebpart.Options.Local != null)
				{
					$.each(thisWebpart.Options.Local, function (idx, item) {
						ImageListOptions[idx] = item;
						
					});
					var ImageDataObj = jP.Lists.setSPObject(ImageListOptions.SiteURL, ImageListOptions.ListName);
					ImageDataObj.getPictures();
					//Get all images from the list
					$.each(ImageDataObj.JQueryData, function (idx, item) {
						var fileLocation = $(item).attr("ows_FileRef").split('#')[1];
						var originalFileLocation = fileLocation;
						var src = location.protocol + "//" + location.host + "/";
						if(ImageListOptions.Thumbnails)
						{
							var parts = $(item).attr("ows_NameOrTitle").split(".");
							if (parts.length > 1) {
							    parts[parts.length - 2] += "_" + parts.pop();
							}
							thumbnailName = parts.join(".");					
							fileLocation = fileLocation.substr(0, fileLocation.lastIndexOf("/")) + "/_t/" + thumbnailName + ".jpg";
						}
						if(ImageListOptions.LightBox)
						{
							var description = "";
							metaInfo = $(item).attr("ows_MetaInfo").split("vti_");
								
							$(metaInfo).each(function (idx, itemData) {
								if(itemData.indexOf("description") > -1)
								{
									description = itemData.split("|")[1].split("Keywords")[0];
								}
							});
							$('.slideshow').append('<a class="rotatingImage" href="' + src + originalFileLocation + '" title="' + description.replace(/\\r\\n/g, '<br>') + '"><img id="img' + idx + '" src="' + src + fileLocation + '" style="border-style: none" /></a>');
						}
						else
						{
							$('.slideshow').append('<img id="img' + idx + '" src="' + src + fileLocation + '" />');
						}
					});
					//Apply cycle jQuery plugin
					$('.slideshow').cycle({
						fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
					});
					
					if(ImageListOptions.LightBox)
					var settings = {imageLoading:$.includePath+'jquery/plugins/jquery-lightbox-0.5/images/lightbox-ico-loading.gif',
									imageBtnPrev:$.includePath+'jquery/plugins/jquery-lightbox-0.5/images/lightbox-btn-prev.gif',
									imageBtnNext:$.includePath+'jquery/plugins/jquery-lightbox-0.5/images/lightbox-btn-next.gif',
									imageBtnClose:$.includePath+'jquery/plugins/jquery-lightbox-0.5/images/lightbox-btn-close.gif',
									imageBlank:$.includePath+'jquery/plugins/jquery-lightbox-0.5/images/lightbox-blank.gif'}
						$('.rotatingImage').lightBox(settings);
				}
			}		
					
			//-----------------------------------------------------
		}catch(err){};
		JpartProcessing = JpartProcessing + 1;
	}
});
//End Webpart logic code -----------------------------------