
sfHover = function(){
    var sfEls = $("topnav").getElementsByTagName("li");
    for (var i = 0; i < sfEls.length; i++) {
        Event.observe(sfEls[i], 'mouseover', function(e){
            var liNode = Event.element(e).parentNode;
            Element.addClassName(liNode, 'sfhover');
            Element.addClassName(liNode.parentNode.parentNode, 'sfhover');
            // get the child ul
            if (liNode.parentNode.parentNode.tagName == 'LI') {
                ul = liNode.parentNode;
            }
            else {
                ul = liNode.getElementsByTagName('ul')[0];
            }
            if (ul) {
                deployHideFrame(ul);
            }
            
        }, false);
        
        Event.observe(sfEls[i], 'mouseout', function(e){
            var liNode = Event.element(e).parentNode;
            Element.removeClassName(liNode, 'sfhover');
            Element.removeClassName(liNode.parentNode.parentNode, 'sfhover');
            sheathHideFrame();
        }, true);
    }
};
Event.observe(window, 'load', sfHover, false);

var currentVideoTitle = "";
var deployHideFrame = function(el){
    // get the hideFrame element
    var hideFrame = $('hideFrame');
    
    // give it properties
    
    
    // clone the element we want to hide behind
    Position.clone(el, hideFrame);
    
    // show the hideFrame
    Element.show(hideFrame);
    
};

var sheathHideFrame = function(){
    Element.hide('hideFrame');
};

var hideLoading = function(){
    loadAnimation = new PeriodicalExecuter(function(){
        Element.hide($('loading'));
        this.stop();
    }, 1);
};


var interstitialInit = function(e){
    Event.observe($('close_btn'), 'click', hideInterstitial, false);
    Event.observe($('outbound_link'), 'click', function(){
        fireTag('48.52');
        Element.hide('interstitial');
    }, false);
};

var showInterstitial = function(link){
    fireTag('73.1');
    var interstitial = $('interstitial');
    
    // set the outbound link
    $('outbound_link').href = link;
    
    // set the position
    var top = (Position.realOffset(document.body)[1] + 100) + 'px';
    var left = (document.body.scrollWidth / 2 - 260) + 'px';
    Element.setStyle(interstitial, {
        'left': left,
        'top': top,
        'display': 'block'
    });
    
    return false;
};
var hideInterstitial = function(e){
    Element.hide('interstitial');
    
    if (e) {
        Event.stop(e);
    }
    return false;
};
Event.observe(window, 'load', initializePage, false);
//var kuwt = function(){
(function(){
    var cn = "KUWTWidget";
    var config = {
        ur: {
            tabID: "urtab",
            frameID: "ursection",
            series: {
                sc: {
                    tabID: "ur-sc",
                    frameID: "ur-sc-content"
                },
                nw: {
                    tabID: "ur-nw",
                    frameID: "ur-nw-content"
                },
                cw: {
                    tabID: "ur-cw",
                    frameID: "ur-cw-content"
                },
                ft: {
                    tabID: "ur-ft",
                    frameID: "ur-ft-content"
                }
            },
            allLinkID: "urall"
        },
        ds: {
            tabID: "dstab",
            frameID: "dssection",
            series: {
                sc: {
                    tabID: "ds-sc",
                    frameID: "ds-sc-content"
                },
                nw: {
                    tabID: "ds-nw",
                    frameID: "ds-nw-content"
                },
                cw: {
                    tabID: "ds-cw",
                    frameID: "ds-cw-content"
                },
                ft: {
                    tabID: "ds-ft",
                    frameID: "ds-ft-content"
                }
            },
            allLinkID: "dsall"
        }
    }
    var info = (window.console) ? console.info : function(){
    };
    function toString(){
        return cn
    }
    function attachUI(e){
		//info(cn+" attachUI")
        $(config.ds.tabID).observe('click', showDSTab);
        $(config.ur.tabID).observe('click', showURTab);
		
		$(config.ds.series.sc.tabID).observe('click', showSeriesSC);
		$(config.ds.series.nw.tabID).observe('click', showSeriesNW);
		$(config.ds.series.cw.tabID).observe('click', showSeriesCW);
		$(config.ds.series.ft.tabID).observe('click', showSeriesFT);
		$(config.ur.series.sc.tabID).observe('click', showSeriesSC);
		$(config.ur.series.nw.tabID).observe('click', showSeriesNW);
		$(config.ur.series.cw.tabID).observe('click', showSeriesCW);
		$(config.ur.series.ft.tabID).observe('click', showSeriesFT);
    }
	function showTab(tab){
		var showTab = tab
		var hideTab = (tab == "ds")?"ur":"ds"
		$(config[hideTab]["tabID"]).removeClassName('on');
        $(config[showTab]["tabID"]).addClassName('on');
		//info('config['+showTab+']["frameID"] = '+config[showTab]["frameID"])
        $(config[showTab]["frameID"]).show();
        $(config[hideTab]["frameID"]).hide();
	}
    function showDSTab(e){
        if (e) {
            Event.stop(e)
        }
       // info("show driver standings")
		showTab("ds")
    }
    function showURTab(e){
        if (e) {
            Event.stop(e)
        }
       // info("show upcoming races")
		showTab("ur")
    }
	function showSeriesSC(e){
		if (e) {
            Event.stop(e)
        }
		showSeries("sc")
	}
	function showSeriesNW(e){
		if (e) {
            Event.stop(e)
        }
		showSeries("nw")
	}
	function showSeriesCW(e){
		if (e) {
            Event.stop(e)
        }
		showSeries("cw")
	}
	function showSeriesFT(e){
		if (e) {
            Event.stop(e)
        }
		showSeries("ft")
	}

	function showSeries(seriescode){
		$(config.ds.series.sc.tabID).removeClassName('on');
		$(config.ds.series.nw.tabID).removeClassName('on');
		$(config.ds.series.cw.tabID).removeClassName('on');
		$(config.ds.series.ft.tabID).removeClassName('on');
		$(config.ur.series.sc.tabID).removeClassName('on');
		$(config.ur.series.nw.tabID).removeClassName('on');
		$(config.ur.series.cw.tabID).removeClassName('on');
		$(config.ur.series.ft.tabID).removeClassName('on');
		//highlight
		$(config.ds.series[seriescode].tabID).addClassName('on');
		$(config.ur.series[seriescode].tabID).addClassName('on');
		// hide
		$(config.ds.series.sc.frameID).hide();
		$(config.ds.series.nw.frameID).hide();
		$(config.ds.series.cw.frameID).hide();
		$(config.ds.series.ft.frameID).hide();
		$(config.ur.series.sc.frameID).hide();
		$(config.ur.series.nw.frameID).hide();
		$(config.ur.series.cw.frameID).hide();
		$(config.ur.series.ft.frameID).hide();
		// show frame
		$(config.ds.series[seriescode].frameID).show();
		$(config.ur.series[seriescode].frameID).show();
	}
    Event.observe(window, 'load', attachUI, false);
    
    return {
        // only properties and methods returned will be public
        className: cn,
        toString: toString
    };
})();
// Video Widget
(function(){
    var cn = "VideoWidget";
	 var info = (window.console) ? console.info : function(){};
	 var config = {
	 	prevID:'vid_page_indicator_prev',
		nextID:'vid_page_indicator_next',
		thumbContID:'vid_carousel_pages',
		pagenumID:"vid_page",
		vidThumbs:"div.vid_thumb_cont",
		firstVid:"vid-1",
	 	maxpages:6,
		visiblewidth:433
	 };
	 var page = 0;
	 var baseX = 0
	 var selected
	 var selectedElement
	 
	 function updateVidCarousel(){
	 	var newX = baseX +(page * config.visiblewidth);
		var cont = $(config.thumbContID)
		cont.style.left = -newX+'px';
		var msg = (page+1) +" / "+ config.maxpages
		//info(msg)
		$(config.pagenumID).update(msg)
	 }
	 function stop(e){
	 	if (e) {
            Event.stop(e)
        }
	 }
	 function prevPage(e){
	 	stop(e);
		if (page>0){
			page--
			updateVidCarousel()
		}
	 }
	 function nextPage(e){
	 	stop(e);
		if (page<config.maxpages-1){
			page++
			updateVidCarousel()
		}
	 }
	 function selectVideo(e){
	 	stop(e);
	 	var element = Event.element(e);
	
		if (element.tagName == "IMG"){
			element = element.parentNode
		}
		if (selectedElement) {
			Element.removeClassName(selectedElement, "on")
		}
		Element.addClassName(element, "on")
		selectedElement = element
	 	var id = element.id
		
		var a = id.split("-")
		//info(a[1])
		//info(parseFloat(a[1]))
		var vidIndex = -1
		if (a && a.length > 1) {
			// using parseFloat because parseInt returned 0 for 08 and 09 - voodoo
			 vidIndex = parseFloat(a[1])-1;
			 var data = seriesVideos[vidIndex]
			 //info(data[0])
			 loadVideo(data[0], vidIndex)
		}
	 	
		
		if (window.console) {
			console.info("selectVideo " + vidIndex)
		}
	 }
	 
	function attachUI(e){
		//info(cn+" attachUI")
		$(config.prevID).observe("click", prevPage)
		$(config.nextID).observe("click", nextPage)
		$$(config.vidThumbs).each(function(vid_thumb){vid_thumb.observe("click", selectVideo)})
		selectedElement = $(config.firstVid)
		updateVidCarousel()
	}
    Event.observe(window, 'load', attachUI, false);
    return {
		// only properties and methods returned will be public
        className: cn
    };
})();
/* this is copied from media pages and should be broken out in to a separate js */
var ua = (function(){
    var agent = navigator.userAgent
    var isIE = function(){
        return !+"\v1";
    }()
    var isFF = function(){
        return agent.match(/Firefox/i) !== null;
    }();
    var isIPad = function(){
        return agent.match(/iPad/i) !== null;
    }();
    var isIPhone = function(){
        return agent.match(/iPhone/i) !== null;
    }();
    var isIOS = function(){
        return isIPad || isIPhone
    }();
    var iOSVersion = function(){
        var match = agent.match(/OS (\d+)_/i);
        if (match && match[1]) {
            return match[1];
        }
    }();
    var isAndroid = function(){
        return agent.match(/Android/i) !== null;
    }();
    var androidVersion = function(){
        var match = agent.match(/Android (\d+)\./i);
        if (match && match[1]) {
            return match[1];
        }
    }();
    var isBlackBerry = function(){
        return agent.match(/BlackBerry/i) !== null;
    }();
    var isKindle = function(){
        return agent.match(/Kindle/i) !== null;
    }();
    var isNookColor = function(){
        return false;
    }();
    
    var isMobile = function(){
        return isIOS || isAndroid || isBlackBerry || isKindle || isNookColor
    }();
    return {
        isIE: isIE,
        isFF: isFF,
        isIPad: isIPad,
        isIPhone: isIPhone,
        isIOS: isIOS,
        isAndroid: isAndroid,
        isBlackBerry: isBlackBerry,
        isKindle: isKindle,
        isNookColor: isNookColor,
        isMobile: isMobile,
        androidVersion: androidVersion,
        iOSVersion: iOSVersion
    }
})();

function getFlashInstance(strID){
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[strID];
    }
    else {
        return document[strID];
    }
}
function flashIsReady(){
	var vidIndex = 0
	var data = seriesVideos[vidIndex]
	loadVideo(data[0], vidIndex)
}

function createHashFromDate(dateStr){
	var dateAr = dateStr.toLowerCase().split(" ")
	var hash 
	if (dateAr.length > 2) {
		var day = parseInt(dateAr[1], 10)
		hash = dateAr[0]+"-"+day+"-"+dateAr[2]
	} else {
		hash = dateAr[0]+"-"+dateAr[1]
	}
	return hash
}

function loadVideo(path, index){
    //var index = getVideoData(path)
    var data
    var baseUrl = window.location.href.split("#")[0]
    var videoThumb
    var mobileVideo
    var link
    var hashDate
	var title
    if (index > -1) {
        data = seriesVideos[index]
        videoThumb = window.location.protocol + "//" + window.location.host + data[7]
        hashDate = createHashFromDate(data[1])
		window.location.hash = hashDate
		link = window.location.protocol + "//" + window.location.hostname + "/motorsports/video.html#" + hashDate
        title =data[2]
		currentVideoTitle = title
		mobileVideo = data[5]
		$("viddate").update(data[1])
		$("vidtitle").update(title)
		$("viddesc").update(data[3])
		$("vidpagelink").href = "/motorsports/video.html#"+hashDate
		addLikeButton(link);
		customAddThisButton('#sharethis_div', link, title, data[3], videoThumb, path);
		if (window.initRatings) {
			initRatings();
			getUserRating();
		} 
		
    }
    //Log.info(path);
    if (ua.isMobile) {
        document.getElementById("videoplayer").innerHTML = '<a href="' + mobileVideo + '"><img src="' + videoThumb + '" alt="' + title + '" /></a>'
    }
    else {
        getFlashInstance('videoplayer').loadVideo(path, "", "");
    }
}

function initVideo(){
	if (ua.isMobile) {
		//if(window.console){console.info("defaultVideo:"+defaultVideo)}
        //loadVideo(defaultVideo, defaultTitle, defaultDesc);
		var vidIndex = 0
		var data = seriesVideos[vidIndex]
		loadVideo(data[0], vidIndex)
    }
    else {
        var fv = {};
        var pa = {};
        pa.wmode = "transparent";
        pa.allowfullscreen = "true";
        pa.allowscriptaccess = "sameDomain";
        
        var at = {};
        at.id = "videoplayer";
        at.name = "videoplayer";
        
        swfobject.embedSWF("/motorsports/swfs/toyota_racing_video_standalone/tr_videoplayer.swf", "videoplayer", "452", "342", "8.0.0", "expressInstall.swf", fv, pa, at);
    }
}
function initializePage(){
	hideLoading()
	initVideo()
    interstitialInit()
	
}
window.fbAsyncInit = function() {
	FB.init({
		status: true,
		cookie: true,
		xfbml: true
	});
};

function onAddThis_Share(evt){
    var ft = {
        '<Video_Name>': currentVideoTitle,
        '<Social_Media_Type>': evt.data.service
    }
    fireTag('47.63', ft);
}

function addLikeButton(url) {
	var fb_div = document.getElementById('fbshare');
	fb_div.innerHTML = '<fb:like href="' + url + '" show_faces="false" colorscheme="dark" layout="button_count"></fb:like>';
	FB.XFBML.parse(fb_div);
}
