var playAd = {
    plugins: {
        // controlbar settings
        controls: {
            //display: 'none',
            backgroundGradient: 'none',
            all: true,
            playlist: false,
            scrubber: false,
            mute: true,
            opacity: 0.7,
            height: 20
        }
    },
    // screen positioning
    screen: {
        top: 0, width: 480, height: 280
    },
    //license key
    key: '$6e84ee2c13fd88bcd40',
    // properties common to all clips
    clip: {},
    // playlist with two entries 
    playlist: [
    // user is forced to see this entry. pause action is disabled , autostart:true
            {
            url: null,
            // make duration a little shorter for this demo 
            duration: null,
            linkUrl: null,
            linkWindow: '_blank',
            scaling: 'scale',
            //linkWindow: '_blank',
            onStart: function() {
                eventListen("AdStart");
            }
            //,
            // when this event returns false - player's default behaviour is disabled, -> hide the control bar
            // onBeforePause: function() { 
            //    return false; 
            //}
                ,
            onFinish: function() {
                eventListen("complete");
            }
        },
    // this is the actual video. controlbar is shown 
            {
                url: null,
                scaling: 'scale',
                onStart: function(clip) {
                this.getControls().widgets({ scrubber: true })
                urchintrack("Trailers", "Play", clip.url);
            },
            // track pause event for this clip. time (in seconds) is also tracked        
            onPause: function(clip) {  
                urchintrack("Trailers", "Pause", clip.url) ;           
                //google._trackEvent("Videos", "Pause", clip.url, parseInt(this.getTime()));         
            },                 
             
            // track stop event for this clip. time is also tracked         
            onStop: function(clip) {             
                urchintrack("Trailers", "Stop", clip.url) ;
            },                  
             
            // track finish event for this clip         
            onFinish: function(clip) {             
                urchintrack("Trailers", "Finish", clip.url) ;        
            }
        }
        ]

};
var playTrailerOnly = {
    plugins: {
        // controlbar settings
        controls: {
            backgroundGradient: 'none',
            all: true,
            scrubber: true,
            mute: true,
            height: 20
        }
    },
    // screen positioning
    screen: {
        top: 0, width: 480, height: 280
    },
    //license key
    key: '$6e84ee2c13fd88bcd40',
    // properties common to all clips
    clip: {},
    // playlist with two entries 
    playlist: [
    // this is the actual video. controlbar is shown 
            {
            url: null
        }
        ]

};

jQuery().ready(function() {
    //call json from adtech.
    //$.getJSON(adtechURL, video_response); 
    addScript(adtechURL)
})

// setup player
var eventURL = new Array();
eventURL['AdStart'] = null;
eventURL['midpoint'] = null;
eventURL['firstQuartile'] = null;
eventURL['thirdQuartile'] = null;
eventURL['complete'] = null;
eventURL['mute'] = null;
eventURL['pause'] = null;
eventURL['replay'] = null;
eventURL['fullscreen'] = null;
eventURL['complete'] = null;

function addScript(url) {
    var script = document.createElement('script');
    script.src = url;
    document.body.appendChild(script);
}

function eventListen(eventName) {
    if ((typeof (eventURL[eventName]) != 'undefined') && (eventURL[eventName] != null)) {
        var arr = eventURL[eventName].split("¬");
        pic1 = new Image(1, 1);
        for (var i in arr) {
            pic1.src = arr[i];
            //alert("load: " + arr[i]);
        }
    }
}
function urchintrack(category,event,id){
    if (urchinTracker) {
    // comment out urchin track code, which is recording pageview, not event.!
       // urchinTracker('/' + category + '/' + event + '/' + id);
    }
}

function video_response(adtemplate) {
    // check if empty template
    var videoPlay;
    var eventname;
    //alert("trackURL" + adtemplate.VideoAdServingtemplate.Impression.URL);
    //alert("duration" + adtemplate.VideoAdServingtemplate.Video.VideoLength);

    if ((adtemplate.VideoAdServingtemplate.AdSystem == null)) {
        //alert("AdSytem" + adtemplate.VideoAdServingtemplate.AdSystem);
        playTrailerOnly.playlist[0].url = trailerURL;
        videoPlay = playTrailerOnly;

    } else {
        eventURL['AdStart'] = adtemplate.VideoAdServingtemplate.Impression.URL;
        var advideo = adtemplate.VideoAdServingtemplate.Video;
        var tr_events = adtemplate.VideoAdServingtemplate.TrackingEvents;
        playAd.playlist[0].url = advideo.MediaFiles.MediaFile.URL.replace(/&/g, '%26') ;
		//if ((advideo.VideoClicks.ClickThrough.URL != "") && (advideo.VideoClicks.ClickThrough.URL.length>2)) {
		if (advideo.VideoClicks.ClickThrough.URL.length>2) {
        	playAd.playlist[0].linkUrl = advideo.VideoClicks.ClickThrough.URL.replace(/&/g, '%26') 
		}
		
        playAd.playlist[0].duration = advideo.VideoLength;
        playAd.playlist[1].url = trailerURL;
        $.each(tr_events, function(i, item) {
            //alert("event:" + item.event + "=" + item.URL);
            eventname = item.event;
            if (typeof (eventURL[eventname]) != 'undefined') {
                if (eventURL[eventname] == null) {
                    eventURL[eventname] = item.URL;
                } else {
                    eventURL[eventname] = eventURL[eventname] + "¬" + item.URL
                }
            }
        });
        videoPlay = playAd;
    }
    //load and start the video.
    $f("splash", {src: "http://entertainment.ie/ssi/lib/fplayer/flowplayer.commercial.swf",wmode: "opaque"}, videoPlay);
    $f().play()
} 
