var scroller;
var holder;
var currentItem;
var currentSprite;
var flashLoaded = false;
var embedComplete = false;
var macBrowserVersion;
var isIOS;
var me;
function getID( swfID ){
       if(navigator.appName.indexOf("Microsoft") != -1){
           me = window[swfID];
       }else{
           me = document[swfID];
       }
 }
function onFlashLoaded(){
    //alert("ON FLASH LOADED");
    flashLoaded = true;
    if(embedComplete){
        preparePage();
    }
}
function report(str){
    alert(str);
}
function preparePage(){
    onPageReady(items);
    isIOS = (navigator.platform == "iPhone" || navigator.platform == "iPad");
    currentItem = items[0];
    currentSprite = document.getElementById(currentItem.divId);
    getID("flashContent");
    
    setVideoObject("false");
}
function onFlashEmbedComplete(e){
    embedComplete = true;
    /*if((e.success && flashLoaded) || (e.success && isIE)){
        preparePage();
    }*/
    
    if((e.success && flashLoaded) && (e.success)){
        preparePage();
    }
}
function setVideoObject(autoplay){
    //alert("SET VIDEO OBJECT");
    currentSprite.className = "videoLinkSelected";
    currentSprite.style.background = "url("+currentItem.thumb+")";
        
    var obj = new Object();
    obj.videoPath = currentItem.videoPath;
    obj.isAutoPlay = autoplay;
    if(autoplay == "false"){
        obj.bgImagePath = currentItem.bgImagePath;
        obj.defVol = "0.8";
    }
    
    if(me != null){
        me.onSetVideo(obj);
    }else{
        setNonFlashVideo(obj);
    }
}
function setNonFlashVideo(obj){
    if(supportsH264()){
        //alert("SUPPORTS H264");
        document.getElementById("flashVideoHolderToReplace").innerHTML="<video id='movie' width='500' height='375' preload controls autoplay><source src='"+obj.videoPath+"' /></video>";
    }else{
        //document.getElementById("flashVideoHolderToReplace").innerHTML = "<a href='"+obj.videoPath+"'>View Video</a>" ;
        document.getElementById("flashVideoHolderToReplace").innerHTML = "<object width='500' height='392' classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' codebase='http://www.apple.com/qtactivex qtplugin.cab'><param name='src' value='"+obj.videoPath+"'><param name='autoplay' value='true'><param name='controller' value='true'><embed src='"+obj.videoPath+"' width='500' height='392' autoplay='true' controller='true' pluginspage='http://www.apple.com/quicktime/download/'></embed></object>" ;
    }
    
}
function supportsVideo() {
    return !!document.createElement('video').canPlayType;
}
    
function supportsH264() {
    if (!supportsVideo()) { return false; }
    var v = document.createElement("video");
    return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
}

function onPageReady(data){
    isIE = (navigator.appName == "Microsoft Internet Explorer");
    scroller = document.getElementById("featureVideoScrollArea");
    holder = document.getElementById("flashVideo");
    if(!fPlayer){
        //holder.style.height = "375px";
        holder.style.top = "25px";    
    }
    
    writeContent(items);
    new Scroller(155, 758, "featureVideoScrollArea","btnLeft", "btnRight");
}
function writeContent(items){
    var l = items.length;
    scroller.innerHTML = "";
    for(var i=0;i<l;i++){
        var myId = "videoItem"+i;
        items[i].divId = myId;
        if(!isIOS){
            scroller.innerHTML += "<a href='javascript:void(0)' id='"+myId+"' class='videoLink' name='scrollItem' onmouseover='onRollItem(this);' onmouseout='onRollOutItem(this)' onclick='onSelectItem(this)'><span>"+items[i].title+"</span></a>";
        }else{
            scroller.innerHTML += "<a href='javascript:void(0)' id='"+myId+"' class='videoLink' name='scrollItem' onmouseover='onRollItem(this);'><span>"+items[i].title+"</span></a>";
        }
        var currentDiv = document.getElementById(myId);
        currentDiv.style.background = "url("+items[i].upThumb+")";
    }
}
function onRollItem(obj){
    if(obj != currentSprite){
        currentItem = getItem(obj.id);
        obj.style.background = "url("+currentItem.thumb+")";
    }
    
    if(isIOS){
        onSelectItem(obj);
    }
}
function onRollOutItem(obj){
    if(obj != currentSprite){
        if(currentItem == null){ currentItem = getItem(obj.id); }
        obj.style.background = "url("+currentItem.upThumb+")";
        currentItem = null;
    }
}
function onSelectItem(obj){
    //alert("ON SELECT ITEM");
    /*var newCurrent = getItem(obj.id); 
    if(obj != currentSprite){
        currentSprite.className = "videoLink";
        currentSprite.style.background = "url("+currentItem.upThumb+")";
        currentItem = newCurrent;
        currentSprite = document.getElementById(currentItem.divId)
        //turnOffAllButOne(items, currentItem)
    }
    
    //if(currentItem != null){
        //currentItem = getItem(obj.id);
        if(currentSprite != null){
            //currentSprite.className = "videoLink";
            //currentSprite.style.background = "url("+currentItem.upThumb+")"; 
            //turnOffAllButOne(items,)
        }
        currentSprite = obj;
        setVideoObject("true");
    }
    */
    
    if(currentItem != null){
        turnOffAllButOne(items, currentItem);
        currentSprite = obj;
        currentSprite.className = "videoLinkSelected";
        setVideoObject("true");
    }
}
function getItem(id){
    var l = items.length;
    for(var i=0;i<l;i++){
        if(items[i].divId == id){ return items[i]; }
    }
    return null;
}
function turnOffAllButOne(list,obj){
    var l = list.length;
    for(var i=0;i<l;i++){
        if(list[i] != obj){
            var spr = document.getElementById(list[i].divId);
            spr.className = "videoLink";
            spr.style.background = "url("+list[i].upThumb+")";  
            //videoLink
        }
    }
}
function Scroller(itemWidth,holderWidth, scrollId, leftId, rightId){
    this.initLeft = 0;
    this.items;
    this.itemWidth;
    this.holderWidth;
    this.btnLeft;
    this.btnRight;
    this.scroller;
    this.currentLeft = 0;
    this.inTransition = false;
    this.init = function(){
        this.itemWidth = itemWidth;
        this.holderWidth = holderWidth;
        
        this.btnLeft = document.getElementById(leftId);
        this.btnRight = document.getElementById(rightId);
        this.scroller = document.getElementById(scrollId);
        
        this.btnLeft.style.opacity=0;
        if(isIE){ this.btnLeft.filters.alpha.opacity = 0; }
        this.btnLeft.style.visibility="hidden";
        
        var thisObj = this;
        this.btnLeft.onclick = function(){ thisObj.doScroll(1); }
        this.btnRight.onclick = function(){ thisObj.doScroll(-1); }
        
        this.items = document.getElementsByName("scrollItem");
        if(this.items <= 1){
            this.btnRight.style.opacity=0;
            if(isIE){this.btnRight.filters.alpha.opacity = 0; }
            this.btnRight.style.visibility="hidden";
        }else{
            this.btnRight.style.opacity = 1;
            if(isIE){this.btnRight.filters.alpha.opacity = 100; }
        }
        
        scroller.style.width = String(this.items.length * this.itemWidth)+"px";
    
        if(scroller.style.left != ""){ 
            this.initLeft = parseInt(scroller.style.left);
            this.currentLeft = parseInt(scroller.style.left); 
        }
    }
    
    this.doScroll = function(dir){
        if(!this.inTransition){
             this.inTransition = true;
             
            var future = this.currentLeft + (this.itemWidth * dir);
            this.currentLeft = future;
            
            new Tween(scroller,500,{left:future}, {delay:0, ease:"strongEaseOut", scope:this, completeListener:"onScrollComplete"});
            if(future >= this.initLeft){
                new Tween(this.btnLeft,250,{opacity:0}, {delay:100, scope:this, completeListener:"onLeftFadeOutComplete"});
            }else if(future < this.initLeft && this.btnLeft.style.visibility == "hidden"){
                this.btnLeft.style.visibility = "visible";
                new Tween(this.btnLeft,250,{opacity:1}, {delay:100})
            }
            
            var visibleItems = Math.ceil(this.holderWidth/this.itemWidth);
            var max = this.itemWidth * (this.items.length - (visibleItems+1));
            if(future < -max){
                new Tween(this.btnRight,250,{opacity:0}, {delay:100, scope:this, completeListener:"onRightFadeOutComplete"});
            }else if(this.btnRight.style.visibility == "hidden"){
                this.btnRight.style.visibility = "visible";
                new Tween(this.btnRight,250,{opacity:1}, {delay:100});
            }
        }
    }
    
    this.onScrollComplete = function(obj){
        this.inTransition = false;
    }
    
    this.onRightFadeOutComplete = function(){
        this.btnRight.style.visibility = "hidden";
    }
    
    this.onLeftFadeOutComplete = function(){
        this.btnLeft.style.visibility = "hidden";
    }
    this.init();
}
//window.onload = onPageReady;
