///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
/*
var ad="<adMediaParameter>";
ad+=    "<adMediaStartAd>";
ad+=        "<adMediaStartURL>http://ad.doubleclick.net/imp;v7;j;206917889;0-0;1;16813773;4/4;27881805/27899684/1;;~aopt=2/1/ff/2;~okv=;sz=4x4;kw=xmlausgabevideo1;~cs=r%3fhttp://i.tfag.de/p.gif</adMediaStartURL>";
ad+=        "<adMediaEndURL>http://i.tfag.de/p.gif</adMediaEndURL>";
ad+=        "<adMediaMidURL>http://koeln.schwarzekarte.de/images/pix.gif</adMediaMidURL>";
ad+=        "<adMediaKlickURL>http://ad.doubleclick.net/click%3Bh=v8/3753/3/0/%2a/x%3B206917889%3B0-0%3B1%3B16813773%3B1354-4/4%3B27881805/27899684/1%3B%3B%7Eaopt%3D2/1/ff/2%3B%7Esscs%3D%3fhttp://www.finnair.de</adMediaKlickURL>";
ad+=        "<adMediaDetails>";
ad+=            "<adMediaType>video</adMediaType>";
ad+=            "<filename>http://motifcdn2.doubleclick.net/EMEA/3673/2008042508_Finair/Karaokeman1_488.flv</filename>";
ad+=            "<duration>10</duration>";
ad+=            "<averagebitrate>300</averagebitrate>";
ad+=        "</adMediaDetails>";
ad+=    "</adMediaStartAd>";
ad+=    "<adMediaEndAd>";
ad+=        "<adMediaStartURL>http://i.tfag.de/p.gif</adMediaStartURL>";
ad+=        "<adMediaMidURL>http://koeln.schwarzekarte.de/images/pix.gif</adMediaMidURL>";
ad+=        "<adMediaEndURL>http://i.tfag.de/p.gif</adMediaEndURL>";
ad+=        "<adMediaKlickURL>http://ad.doubleclick.net/click%3Bh=v8/3753/3/0/%2a/x%3B206917889%3B0-0%3B1%3B16813773%3B1354-4/4%3B27881805/27899684/1%3B%3B%7Eaopt%3D2/1/ff/2%3B%7Esscs%3D%3fhttp://www.finnair.de</adMediaKlickURL>";
ad+=        "<adMediaDetails>";
ad+=            "<adMediaType>video</adMediaType>";
ad+=            "<filename>http://videos.focus.de/test/20081001_Goalkeeper_continental_2010_466x349.flv</filename>";
ad+=            "<duration>20</duration>";
ad+=        "</adMediaDetails>";
ad+=    "</adMediaEndAd>";
ad+="</adMediaParameter>";
*/
var ad="<adMediaParameter>";
ad+=    "<adMediaStartAd>";
ad+=        "<DUMMY>DUMMY</DUMMY>";
ad+=    "</adMediaStartAd>";
ad+="</adMediaParameter>";

ad = stringToXML(ad);
ad = xml2array(ad);
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

var videoPlayer = new Array();

function addVideo(containerId, video_url, screenshot_url, video_id, play_length, language_code, user_name) 
{   
    // we need to define an offset to set the right clip number for the cue points if screenshot will be added
    playlistOffset = 1;
    showScreenshot = true;
    selectedVideoBox = location.href.split('#')[1];
    if ( selectedVideoBox == containerId ) {
        playlistOffset -= 1;
        showScreenshot = false;
    }
    
    videoPlayer[video_id] = new Array();
    videoPlayer[video_id]['cuePoints'] = new Array();
    videoPlayer[video_id]['init'] = true;
    
    videoPlayer[video_id]['playlist'] = new Array();
    
    // adding main video to playlist (clip name is the url which is called to track video views)
    videoPlayer[video_id]['playlist'][0] =  { 
                                                name: '/frontend.php/de/profile/trackVideoView/userName/'+ user_name +'/videoIdref/'+ video_id,
                                                id: 'userVideo',
                                                url: video_url                                                               
                                            };

    // adding video ads to playlist 
    addVideoAds(video_id, playlistOffset);

    // workaround, if duration of jpg equl 0, the player stops at jpg, equal/greater 1 the player start playing 
    
    if ( showScreenshot ) {
	    // adding preview picture to playlist 
	    videoPlayer[video_id]['playlist'].unshift (
	                                                { 
		                                                url: screenshot_url,
		                                                overlay: '/images/layout/'+language_code+'/play_button.png',		                                                
		                                                duration: 0                                                             
		                                            }
		                                           );
    }
    
    videoPlayer[video_id]['api'] = flashembed(  containerId, 
                                       { 
                                           src: "/images/layout/default/videoplayer/videoplayer_dark.swf",
                                           width: 332,
                                           height: 274
                                       }, 
                                       {
                                           config: {                                                       
                                                       playList: videoPlayer[video_id]['playlist'],     
                                                       initialScale: 'orig',                               
                                                       controlBarGloss: 'high', 
                                                       autoPlay: true,
                                                       menuItems: [ true, true, true, true, true, false, false ],
                                                       playerId: video_id                                                                    
                                                   }                       
                                       }
                                    );      
}

function addVideoAds(video_id, playlistOffset) {
    //possibleTrackingPics = new Array ('adMediaStartURL', 'adMediaMidURL', 'adMediaEndURL');
    // cuePoints on  the same time position make problems, so we will only call mid tracking pics by cue point
    possibleTrackingPics = new Array ('adMediaMidURL');

    for ( var adFlag in ad['adMediaParameter'] ) 
    {
	    tempAd = ad['adMediaParameter'][adFlag];
	    switch ( tempAd['adMediaDetails']['adMediaType'] ) 
	    {
	        case 'swf':
	        case 'video':                
			    videoObject =   { 
			                        url: tempAd['adMediaDetails']['filename'],                     
			                        controlEnabled: false,
			                        linkUrl: tempAd['adMediaKlickURL'],
			                        id: 'adTrackingPic',
			                        name: tempAd['adMediaStartURL']+ '||' + tempAd['adMediaEndURL']			                        
			                    };
			break;
			
			case 'jpg':
			default:
			    videoObject =   { 
	                                url: tempAd['adMediaDetails']['filename'],                     
	                                duration: tempAd['adMediaDetails']['duration'],
	                                controlEnabled: false,
	                                linkUrl: tempAd['adMediaKlickURL'],
	                                id: 'adTrackingPic',
                                    name: tempAd['adMediaStartURL']+ '||' + tempAd['adMediaEndURL']
	                            };
			break;
		}
	
	    switch ( adFlag )
	    {
	        case 'adMediaStartAd':
	            videoPlayer[video_id]['playlist'].unshift ( videoObject );
	            clipNo = 1;
	        break;
	        
	        case 'adMediaEndAd':
	        default:
	            videoPlayer[video_id]['playlist'].push ( videoObject );
	            clipNo = videoPlayer[video_id]['playlist'].length;
	        break;
	    }
	    
	    possibleTrackingPics.each ( 
	                                   function (value, index)
	                                   {                                            
                                            if ( tempAd[value] != undefined )
                                            {	                                           
	                                            switch ( value ) 
	                                            {
	                                                case 'adMediaEndURL':
	                                                    // not in use yet, see above
	                                                    time = tempAd['adMediaDetails']['duration']-1;
	                                                break;
	                                                
	                                                case 'adMediaMidURL':
	                                                    time = Math.floor(tempAd['adMediaDetails']['duration']/2);
	                                                break;
	                                                
	                                                case 'adMediaStartURL':
	                                                default:
	                                                   // not in use yet, see above
	                                                   time = 1; 
	                                                break;                                                                                             
	                                            }
                                                registerCuePoint( video_id, time, adFlag, clipNo + playlistOffset, 'callTrackingPic', tempAd[value] );
                                            }                                                                                                                                    	                                  
	                                   }
	                              );
    }	    
}

function registerCuePoint( video_id, time, nameFlag, clip_no, action, trackingPicture ) {
    cuePoint = new Object();
    cuePoint['name'] = nameFlag;
    cuePoint['time'] = time;
    cuePoint['clipNo'] = clip_no;
    cuePoint['action'] = action;
    cuePoint['trackingPicture'] = trackingPicture;
    
    videoPlayer[video_id]['cuePoints'].push ( cuePoint );
}

function onFlowPlayerReady() {       
    videoPlayer.each(   
        function(tempPlayer, videoId)
        {
            if ( tempPlayer['api'].getPlayerId ) {
                //alert(videoId + " = " + tempPlayer['api'] + "PlayerID: "+ tempPlayer['api'].getPlayerId());                               
                // adding cue points to player
                if ( videoPlayer[videoId]['init'] == true ) {                                                        
                   videoPlayer[videoId]['init']=false;
                   videoPlayer[videoId]['cuePoints'].each(
                       function(cuePoint, index)
                       {
                          videoPlayer[videoId]['api'].addCuePoints(
                                                                    [
                                                                        {
                                                                            name: cuePoint['name']+index,
                                                                            time: cuePoint['time'],
                                                                            parameters: {
                                                                                           videoId: videoId,
                                                                                           clip: cuePoint['clipNo'],
                                                                                           action: cuePoint['action'],
                                                                                           trackingPic: cuePoint['trackingPicture']
                                                                                        }
                                                                        }
                                                                     ]
                                                                   );                                                                                              
                       }                   
                   );
                }
                //tempPlayer['api'].ToClip(0);
            }
       }       
    );
}

function callTrackingPic ( trackingPicsString, position, random ) {
    trackingPics = trackingPicsString.split('||');
    if ( trackingPics[position] != undefined && trackingPics[position] != 'undefined' && trackingPics[position] != '' ) {        
	   if ( random ==true ) {     
	        searchFor = "[?].";
	        result = trackingPics[position].search( searchFor );
	           
	        if ( result == -1 ) {
	             randomAdd = "?";
	        }
	        else {
	            randomAdd = "&";
	        }
	        randomAdd += "random="+(Math.floor(Math.random()*10000));
	    }
	    else {
	        randomAdd = "";
	    }

        $('trackingPicContainer').innerHTML = '<img src="'+trackingPics[position]+randomAdd+'">';       
    }
}

function onPlay(clip) { 
    if ( clip.id != undefined ) {
        switch ( clip.id )
        {
            case "adTrackingPic":            
                // call tracking pics                
                callTrackingPic( clip.name, 0 );
            break;
            
            case "userVideo":
            default:                                           		        		        
		        // track video views
		        randomAdd = "?random="+(Math.floor(Math.random()*1000));
		        url = clip.name;               
		        new Ajax( url + randomAdd, {method: "get"}).request();
		        dynamicImpression();
            break;
        }
    }
}

function onClipDone(clip) {
    if ( clip.id != undefined ) {
        switch ( clip.id )
        {
            case "adTrackingPic":            
                // call tracking pics                
                callTrackingPic( clip.name, 1 );
            break;   
            
            case "userVideo":
            default:                                                                                       
                dynamicImpression();
            break;                 
        }
    }
}

function onCuePoint(cuePoint) {
  api = videoPlayer[cuePoint.playerId]['api']; 
  clipNo = api.getCurrentClip();
  
  cuePointForClip = cuePoint['parameters']['clip'];
  
  if ( clipNo == cuePointForClip) {
    
    switch (cuePoint['parameters']['action']) 
    {
        case "callTrackingPic":
            // call tracking pics                
            callTrackingPic( cuePoint['parameters']['trackingPic'], 0, true );
        break;
    }    
  }
}
