﻿/* 
Copyright (c) 2010 by Adecco management & consulting S.A.
Vendor: #0701
*/

/*
summary: this method takes the clicked item (actual image), searches the corresponding entry in the RotatingItemsArray.
RotatingItemsArray - entry could contains either a redirectUrl or a movieUrl.  Corresponding to the Url the user
is redirected to this Url, either in the same window, or in a new window, or a moviePlayer is rendered on the page.
The MoviePlayer could be a FlashPlayer, a Microsoft Windows Web Player, or a QuickTime Player.
             
para: idSuffix -> contains the generated random integer value to extend the element identifiers.
*/
function MainPictureClick(idSuffix, noFlashUrl) 
{
    var configsArray = document.ConfigsArray[idSuffix];

    /* gets the title across the mainPicture. */
	var title = document.getElementById('mainPicture'+idSuffix).alt;

	/* gets the rotatingItemsArray. */
    var rotatingItemsArray = document.RotatingItemsArray[idSuffix];
    
    /* searches the actual clicked itemIndex. */
	for ( var i = 0; i<rotatingItemsArray.length; i++)
	{
	    if(rotatingItemsArray[i]['PicTitle'] == title)
	    {
  			index = i;		
	    }
	}
    
    /* sets some variables from the array. */
	movieUrl = rotatingItemsArray[index]['MovieURL'];
	targetUrl = rotatingItemsArray[index]['TargetURL'];
	target = rotatingItemsArray[index]['Popup'];
	
	/* if existing a movieUrl. */
	if(movieUrl != "") 
	{
	    /* sets the mainPictureSection hidden and the moviePlayerSection visible. */
		document.getElementById('MainPictureSection'+idSuffix).style.display = "none";
		var moviePlayerSection = document.getElementById('MoviePlayerSection'+idSuffix);
		var VideoFlash = document.getElementById('VideoFlash'+idSuffix);
		VideoFlash.style.display = "block";
		
		/* removes all childs from the moviePlayerSection. */
		removeChildrenFromNode(moviePlayerSection);
		
		/* creates a new link-element for jQuery. */
		moviePlayer = document.createElement("a");
		moviePlayer.href = movieUrl;
		moviePlayer.id = "MoviePlayerUrl"+idSuffix;
		
		if((movieUrl.match(".mov") == ".mov" || movieUrl.match(".wmv") == ".wmv" || movieUrl.match(".avi") == ".avi"))
		{
		    /* gets the location of the flv/ flashPlayer to play the flv movie. */
            
            moviePlayer.className = "media"+idSuffix+" {width:" + document.getElementById('mainPicture'+idSuffix).width + ", heigth:"+ document.getElementById('mainPicture'+idSuffix).height+"}";
            moviePlayerSection.appendChild(moviePlayer);
            $('a.media'+idSuffix).media({
            width: document.getElementById('mainPicture'+idSuffix).width, 
            heigth: document.getElementById('mainPicture'+idSuffix).height});
	    }
	    else if(FlashDetect.installed)
	    {
	        /* searches the MoviePlayer fileExtension. */
		    if((movieUrl.match(".swf") == ".swf")) 
		    {
		        /* FlashPlayer: sets the corresponding class and styles to the link for jQuery and adds the link to the moviePlayerSection. */
		        moviePlayer.className = "media"+idSuffix+" {width:" + document.getElementById('mainPicture'+idSuffix).width + ", heigth:"+ document.getElementById('mainPicture'+idSuffix).height +", autoplay:  false, type:'swf'}";
		        moviePlayerSection.appendChild(moviePlayer);
                
                /* calls the jQuery media-methode which renders/ add a object-Tag to the moviePlayerSection. */
                $('a.media'+idSuffix).media({
                width: document.getElementById('mainPicture'+idSuffix).width, 
                heigth: document.getElementById('mainPicture'+idSuffix).height, type:'swf'});
		    }
            else 
            {
                /*//// Flash Movies (flv and youtube)  //////*/
                 
//                var container = document.createElement("div");
//                container.id = 'container';
//                
//	            moviePlayerSection.appendChild(container);
//                
//                jwplayer("container").setup({
//                    flashplayer: configsArray['MMRotatorPlayer'],
//                    file: movieUrl,
//                    height: document.getElementById('mainPicture'+idSuffix).height,
//                    width: document.getElementById('mainPicture'+idSuffix).width,
//                    skin: configsArray['MMRotatorPlayer'].replace(/MMRotatorPlayer.swf/g, 'skins/' + document.ConfigsArray[idSuffix]['FlashPlayerSkin'] + "/skin.xml")
//                }); 
            
                
                /* creates a new SWFObject and sets some parameter to this... 
                   see: http://code.google.com/p/swfobject/ and 
                   http://www.longtailvideo.com/players/jw-flv-player/ (implementation of FlashPlayer, for example click Download and see example code)
                */
                var so = new SWFObject(configsArray['MMRotatorPlayer'],'mpl'+idSuffix,document.getElementById('mainPicture'+idSuffix).width,document.getElementById('mainPicture'+idSuffix).height,'9');
                    so.addParam('allowfullscreen','true');
                    so.addParam('allowscriptaccess','always');
                    so.addParam('wmode','opaque');
                    so.addVariable('file', movieUrl);
                    
                    /* sets the playerSkin. */
                    if(document.ConfigsArray[idSuffix]['FlashPlayerSkin'] != 'Default') {
                    so.addVariable('skin',configsArray['MMRotatorPlayer'].replace(/MMRotatorPlayer.swf/g, 'skins/' + document.ConfigsArray[idSuffix]['FlashPlayerSkin'] + "/skin.xml"));
                    }

                    so.write('MoviePlayerSection'+idSuffix);
            }
	    }
	    else 
	    {
	        /* No Flash Plugin */
	       
	        var errorContainer = document.createElement("div");
	        
/////////////////////////////////////////////////////////////////////////////////////////////
///////////////// Section Commented original
////////////////////////////////////////////////////////////////////////////////////////////

//	        var  errorMessage = "You need to upgrade your Adobe Flash Player to watch this video."
//	        var errorMessageContainer = document.createElement("div");
//	        errorMessageContainer.innerHTML = errorMessage;
//	        
//	        var linkContainer = document.createElement("div");
//	        var link = document.createElement("a");
//	        link.href = "http://get.adobe.com/flashplayer/";
//	        link.innerHTML= "Please download it from Adobe.";
//	        
//	        var headLineTag = document.createElement("h3");

//	        linkContainer.appendChild(link);
//	        headLineTag.appendChild(errorMessageContainer);
//	        headLineTag.appendChild(linkContainer);     
//	        errorContainer.appendChild(headLineTag);

/////////////////////////////////////////////////////////////////////////////////////////////
///////////////// Section Commented original
/////////////////////////////////////////////////////////////////////////////////////////////
	        
	        //US906  
	               errorContainer.innerHTML = noFlashUrl;
	               
	        
	              
	              
	        
	        moviePlayerSection.appendChild(errorContainer);        
	    }	
	} 
	else 
	{		
	    /* if there is now movie, redirect user. */
		if(target  == 'True')
		{
			window.open(targetUrl, "Zweitfenster");
		}
		else
		{
			
			window.location = targetUrl;
		}	
	}
}

/*
    summary: removes all childNodes from the parameter Node.    
    para: node -> actual (parent) node.
*/
function redirectMultiMediaRotator(target, targetUrl)
{
    if(target  == 'True')
	{
		window.open(targetUrl, "Zweitfenster");
	}
	else
	{
		window.location = targetUrl;
	}
}

function removeChildrenFromNode(node)
{
   if(node == undefined ||
        node == null)
   {
      return;
   }
   var len = node.childNodes.length;

	while (node.hasChildNodes())
	{
	  node.removeChild(node.firstChild);
	}
}

/*
summary: This method sets the Titel and MainPicture-Properties of a clicked PreviewImage.      
para: idSuffix -> contains the generated random integer value to extend the element identifiers.
      title -> title of the clicked PreviewImageItem.
*/

function changePictureURL(title,idSuffix) {
    var index;

	var configsArray = document.ConfigsArray[idSuffix];
	
	/* gets the rotatingItemsArray. */
    var rotatingItemsArray = document.RotatingItemsArray[idSuffix];

    /* gets the containers, which have to be adapt. */
    var mainPictureSection = document.getElementById('MainPictureSection'+idSuffix);
    var VideoFlash = document.getElementById('VideoFlash'+idSuffix);
    var mainPicture = document.getElementById('mainPicture'+idSuffix);
	var subjectTitle = document.getElementById('SubjectTitle'+idSuffix);
	var titleContainer = document.getElementById('titleContainer'+idSuffix);
	
	/* searches the actual clicked PreviewItemIndex. */
	for ( var i = 0; i<rotatingItemsArray.length; i++)
	{
	    if(rotatingItemsArray[i]['PicTitle'] == title)
	    {
  			index = i;
	    }
	}
    
    /* gets the mainPicture size. */
    if(configsArray['MainPictureHeight'] != '0' && configsArray['MainPictureWidth'] != '0') {
	    var height = configsArray['MainPictureHeight'];
	    var width = configsArray['MainPictureWidth'];
	} else 
	{
	    var height = rotatingItemsArray[index]['PicHeight'];
	    var width = rotatingItemsArray[index]['PicWidth'];
	}
	
	/* sets the mainPicture size to the corresponding content containers. */
	mainPictureSection.style.width = width+"px";
	VideoFlash.style.width = width+"px";
	VideoFlash.style.height = height+"px";
	
	/* sets the mainPictureSection visible and the moviePlayerSection hidden. */
	if(mainPictureSection.style.display == "none")
	{
		mainPictureSection.style.display = "block";
		VideoFlash.style.display = "none";

	}

	if(configsArray['ShowTitleSection'] == 'True') {
	    subjectTitle.style.width = width;
	    titleContainer.style.width = width+"px";
	    
	    /* set the ImageTitel and title container Size. */
        if(configsArray['SetStandardTitle'] == 'False') 
        {
	        subjectTitle.innerHTML = rotatingItemsArray[index]['PicTitle'];
	        
	    }
	}
		
	/* Set the MainPicture Properties. */
	mainPicture.src = rotatingItemsArray[index]['PictureURL'];
	mainPicture.alt = rotatingItemsArray[index]['PicTitle'];
	mainPicture.title = rotatingItemsArray[index]['PicTitle'];
	mainPicture.height = height;
	mainPicture.width = width;
}
function openDialog(idSuffix) {

$( "#dialog-message"+idSuffix ).dialog({
			modal: true,
			width: 600,
			resizable: false,
			buttons: {
				Ok: function() {
				    
				    var configsArray = new Object();
				    //var configsArray = document.ConfigsArray[idSuffix];
				    
				    configsArray['Identifier'] = document.getElementById('Identifier'+idSuffix).value;

				    if(document.getElementById('EmbedJQuery'+idSuffix).checked == true) {
				        configsArray['EmbedJQuery'] = '1';
				    } else {
				        configsArray['EmbedJQuery'] = '0';
				    }
				    
				    configsArray['ItemsListUrl'] = document.getElementById('ItemsListUrl'+idSuffix).value;
				    configsArray['Category'] = document.getElementById('Category'+idSuffix).value;
				    configsArray['RotatingItemsNumber'] = document.getElementById('RotatingItemsNumber'+idSuffix).value;
				    configsArray['WebPartWidth'] = document.getElementById('WebPartWidth'+idSuffix).value;
				    configsArray['WebPartHeight'] = document.getElementById('WebPartHeight'+idSuffix).value;
				    
				    if(document.getElementById('ShowTitleSection'+idSuffix).checked == true) {
				        configsArray['ShowTitleSection'] = '1';
				    } else {
				        configsArray['ShowTitleSection'] = '0';
				    }
                    
                    if(document.getElementById('ShowMainPictureSection'+idSuffix).checked == true) {
				        configsArray['ShowMainPictureSection'] = '1';
				    } else {
				        configsArray['ShowMainPictureSection'] = '0';
				    }
				    
				    if(document.getElementById('ShowPreviewSection'+idSuffix).checked == true) {
				        configsArray['ShowPreviewSection'] = '1';
				    } else {
				        configsArray['ShowPreviewSection'] = '0';
				    }
				    
				    if(document.getElementById('ShowPrevNextButtons'+idSuffix).checked == true) {
				        configsArray['ShowPrevNextButtons'] = '1';
				    } else {
				        configsArray['ShowPrevNextButtons'] = '0';
				    }
				    
				    if(document.getElementById('SetStandardTitle'+idSuffix).checked == true) {
				        configsArray['SetStandardTitle'] = '1';
				    } else {
				        configsArray['SetStandardTitle'] = '0';
				    }
				    
				    configsArray['StandardTitleStyle'] = document.getElementById('StandardTitleStyle'+idSuffix).value;
				    configsArray['StandardTitle'] = document.getElementById('StandardTitle'+idSuffix).value;
				    configsArray['MainPictureWidth'] = document.getElementById('MainPictureWidth'+idSuffix).value;
				    configsArray['MainPictureHeight'] = document.getElementById('MainPictureHeight'+idSuffix).value;
				    configsArray['PreviewPictureWidth'] = document.getElementById('PreviewPictureWidth'+idSuffix).value;
				    configsArray['PreviewPictureHeight'] = document.getElementById('PreviewPictureHeight'+idSuffix).value;
				    configsArray['CycleMethod'] = document.getElementById('CycleMethod'+idSuffix).value;
				    configsArray['DisplayDuration'] = document.getElementById('DisplayDuration'+idSuffix).value;
				    configsArray['TransitionTime'] = document.getElementById('TransitionTime'+idSuffix).value;
				    configsArray['FlashPlayerSkin'] = document.getElementById('FlashPlayerSkin'+idSuffix).value;
				    
				    var postBackArgs =  JSON.stringify(configsArray);
				    CreatePostBack("dialog-message"+idSuffix,postBackArgs); 
				    
					$( this ).dialog( "close" );
				}
			}
		});

//    $( "#dialog-edit"+idSuffix ).dialog( "open" );
//    return false;
$( "#tabs"+idSuffix ).tabs();
}


function CreatePostBack(control, argument){
    __doPostBack(control, argument);
}


/*
Copyright (c) Copyright (c) 2007, Carl S. Yestrau All rights reserved.
Code licensed under the BSD License: http://www.featureblend.com/license.txt
Version: 1.0.4
*/
var FlashDetect = new function(){
    var self = this;
    self.installed = false;
    self.raw = "";
    self.major = -1;
    self.minor = -1;
    self.revision = -1;
    self.revisionStr = "";
    var activeXDetectRules = [
        {
            "name":"ShockwaveFlash.ShockwaveFlash.7",
            "version":function(obj){
                return getActiveXVersion(obj);
            }
        },
        {
            "name":"ShockwaveFlash.ShockwaveFlash.6",
            "version":function(obj){
                var version = "6,0,21";
                try{
                    obj.AllowScriptAccess = "always";
                    version = getActiveXVersion(obj);
                }catch(err){}
                return version;
            }
        },
        {
            "name":"ShockwaveFlash.ShockwaveFlash",
            "version":function(obj){
                return getActiveXVersion(obj);
            }
        }
    ];
    /**
     * Extract the ActiveX version of the plugin.
     * 
     * @param {Object} The flash ActiveX object.
     * @type String
     */
    var getActiveXVersion = function(activeXObj){
        var version = -1;
        try{
            version = activeXObj.GetVariable("$version");
        }catch(err){}
        return version;
    };
    /**
     * Try and retrieve an ActiveX object having a specified name.
     * 
     * @param {String} name The ActiveX object name lookup.
     * @return One of ActiveX object or a simple object having an attribute of activeXError with a value of true.
     * @type Object
     */
    var getActiveXObject = function(name){
        var obj = -1;
        try{
            obj = new ActiveXObject(name);
        }catch(err){
            obj = {activeXError:true};
        }
        return obj;
    };
    /**
     * Parse an ActiveX $version string into an object.
     * 
     * @param {String} str The ActiveX Object GetVariable($version) return value. 
     * @return An object having raw, major, minor, revision and revisionStr attributes.
     * @type Object
     */
    var parseActiveXVersion = function(str){
        var versionArray = str.split(",");//replace with regex
        return {
            "raw":str,
            "major":parseInt(versionArray[0].split(" ")[1], 10),
            "minor":parseInt(versionArray[1], 10),
            "revision":parseInt(versionArray[2], 10),
            "revisionStr":versionArray[2]
        };
    };
    /**
     * Parse a standard enabledPlugin.description into an object.
     * 
     * @param {String} str The enabledPlugin.description value.
     * @return An object having raw, major, minor, revision and revisionStr attributes.
     * @type Object
     */
    var parseStandardVersion = function(str){
        var descParts = str.split(/ +/);
        var majorMinor = descParts[2].split(/\./);
        var revisionStr = descParts[3];
        return {
            "raw":str,
            "major":parseInt(majorMinor[0], 10),
            "minor":parseInt(majorMinor[1], 10), 
            "revisionStr":revisionStr,
            "revision":parseRevisionStrToInt(revisionStr)
        };
    };
    /**
     * Parse the plugin revision string into an integer.
     * 
     * @param {String} The revision in string format.
     * @type Number
     */
    var parseRevisionStrToInt = function(str){
        return parseInt(str.replace(/[a-zA-Z]/g, ""), 10) || self.revision;
    };
    /**
     * Is the major version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required major version.
     * @type Boolean
     */
    self.majorAtLeast = function(version){
        return self.major >= version;
    };
    /**
     * Is the minor version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required minor version.
     * @type Boolean
     */
    self.minorAtLeast = function(version){
        return self.minor >= version;
    };
    /**
     * Is the revision version greater than or equal to a specified version.
     * 
     * @param {Number} version The minimum required revision version.
     * @type Boolean
     */
    self.revisionAtLeast = function(version){
        return self.revision >= version;
    };
    /**
     * Is the version greater than or equal to a specified major, minor and revision.
     * 
     * @param {Number} major The minimum required major version.
     * @param {Number} (Optional) minor The minimum required minor version.
     * @param {Number} (Optional) revision The minimum required revision version.
     * @type Boolean
     */
    self.versionAtLeast = function(major){
        var properties = [self.major, self.minor, self.revision];
        var len = Math.min(properties.length, arguments.length);
        for(i=0; i<len; i++){
            if(properties[i]>=arguments[i]){
                if(i+1<len && properties[i]==arguments[i]){
                    continue;
                }else{
                    return true;
                }
            }else{
                return false;
            }
        }
    };
    /**
     * Constructor, sets raw, major, minor, revisionStr, revision and installed public properties.
     */
    self.FlashDetect = function(){
        if(navigator.plugins && navigator.plugins.length>0){
            var type = 'application/x-shockwave-flash';
            var mimeTypes = navigator.mimeTypes;
            if(mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin && mimeTypes[type].enabledPlugin.description){
                var version = mimeTypes[type].enabledPlugin.description;
                var versionObj = parseStandardVersion(version);
                self.raw = versionObj.raw;
                self.major = versionObj.major;
                self.minor = versionObj.minor; 
                self.revisionStr = versionObj.revisionStr;
                self.revision = versionObj.revision;
                self.installed = true;
            }
        }else if(navigator.appVersion.indexOf("Mac")==-1 && window.execScript){
            var version = -1;
            for(var i=0; i<activeXDetectRules.length && version==-1; i++){
                var obj = getActiveXObject(activeXDetectRules[i].name);
                if(!obj.activeXError){
                    self.installed = true;
                    version = activeXDetectRules[i].version(obj);
                    if(version!=-1){
                        var versionObj = parseActiveXVersion(version);
                        self.raw = versionObj.raw;
                        self.major = versionObj.major;
                        self.minor = versionObj.minor; 
                        self.revision = versionObj.revision;
                        self.revisionStr = versionObj.revisionStr;
                    }
                }
            }
        }
    }();
};

