/*
 * jQuery - swfGallery
 * http://www.danaweb.dk/
 *
 * Copyright (c) 2010 Mikkel Damm
 *
 * Date: 2010-07-23 14:49:21 -0500 (Fri, 23 July 2010)
 * Version: 2.0
 */

var swfGalleryPath;
var swfGalleryName;
var swfGalleryEmail;
var swfGalleryXMLUrl;
var swfGallerySiteId;
var swfGalleryLanguageId;
var swfGalleryObj = null;
 
(function($) {
    $.fn.swfGallery = function(params) {

		// Set default parameters
        var defaults = {
            swfObject: "/includes/swf/Flashgallery.swf",
            getXMLURL: "/includes/aspx/FlashgalleryGetData.aspx",
            siteID: 0,
            languageID: 1,
			galleryExist: true,
			swfObjectWidth: 0,
			swfObjectHeight: 0,
			siteName: "",
			siteEmail: "",
			flashWmode: "transparent"
        };

        var params = $.extend(defaults, params);
		var flashAvailable = $.flash.available;
		var flashVersion = $.flash.hasVersion(9.1);

        return this.each(function() {
            swfGalleryObj = $(this);

			// Set global variables
			swfGalleryPath = params.swfObject;
			swfGalleryName = params.siteName;
			swfGalleryEmail = params.siteEmail;
			swfGalleryXMLUrl = params.getXMLURL;
			swfGallerySiteId = params.siteID;
			swfGalleryLanguageId = params.languageID;
			
            var currObjectHtml = swfGalleryObj.html();

            // Check if there is 'no flash active' text in div object
            if (currObjectHtml.length < 0 || currObjectHtml == "") {
				swfGalleryObj.html('<div id="noFlash">This gallery plugin requires Flash Player version 9.0 or later.<br /><a href="http://www.adobe.com/go/getflashplayer" title="Download Flash Player" target="_blank">Click here to download Flash Player</a></div>');
				$('#noFlash').css({fontSize:11, color:"#000000", fontFamily:"Arial", display:"none", textAlign:"center"});
			}
			
			// Check if flash is found on users computer and check user's flash version (must be higher than 9.1)
			if (flashAvailable == false || flashVersion == false) {
				$('#noFlash').css('display', 'block');
			} else {
			
				// Check if there is found a gallery to the current site
				if ((params.galleryExist == true || params.galleryExist == "true") && params.getXMLURL != "") {
					
					// Check params w & h and checks div objects size
					var swfWidth = (params.swfObjectWidth == 0 ? swfGalleryObj.outerWidth() : params.swfObjectWidth);
					var swfHeight = (params.swfObjectHeight == 0 ? swfGalleryObj.outerHeight() : params.swfObjectHeight);
					var swfTime = new Date().getTime();
					
					swfGalleryObj.flash({
						swf: params.swfObject + "?nocache=" + swfTime,
						width: swfWidth,
						height: swfHeight,
						allowFullScreen: true,
						allowScriptAcces: 'always',
						wmode: params.flashWmode,
						flashvars: {
							name: params.siteName,
							email: params.siteEmail,
							xmlfile: params.getXMLURL,
							siteid: params.siteID,
							languageid: params.languageID
						}
					});
					
				} else {
					
					// Hide gallery container when no gallery is found
					swfGalleryObj.css('display', 'none');
					
				}
			}
        });
    };
})(jQuery);


// Flash & Javascript function interaction
function getCallFromFlash(paramType, paramObj) {
	if (paramType == "openFlashFullscreen") {
		
		var prependId = "flashGalleryContainerFull";
		var prependCss = {position:"fixed",top:0,left:0,zIndex:9999,backgroundColor:"#000000",width:"100%",height:"100%",display:"block"}
		var prependHtml = '<div id="' + prependId + '"></d iv>';
		var swfTime = new Date().getTime();
		
		$(prependHtml).prependTo('body');
		$('#' + prependId).css(prependCss);
		$('#' + prependId).flash({
			swf: swfGalleryPath + "?nocache=" + swfTime,
			width: '100%',
			height: '100%',
			allowFullScreen: true,
			allowScriptAcces: 'always',
			wmode: 'transparent',
			flashvars: {
				name: swfGalleryName,
				email: swfGalleryEmail,
				xmlfile: swfGalleryXMLUrl,
				siteid: swfGallerySiteId,
				languageid: swfGalleryLanguageId,
				fullscreen: 'true',
				imageid: paramObj.imageId
			}
		});
		
	} else if (paramType == "closeFlashFullscreen") {
	
		$('#flashGalleryContainerFull').remove();
	
	} else if (paramType == "") {
	
	}
}

function setCallToFlash(paramObj) {
	swfGalleryObj.flash(function() { 
		this.javascriptCallFlash();
	});
}
