/**
 * @author jwhitcraft
 */

function openLiveTimingScoring(e) {
    try {
        var event = e || window.event;
        event.preventDefault();
    } catch(err) {}
    var win = window.open("http://scoring.indycar.com/flash/?site=ims&utm_medium=popup&utm_source=indy500&utm_campaign=site_ts_click","timing","top=100,left=100,width=1000,height=730,resizable=no");
}

function setLeadClip(clip) { Ext.fly('lead_stories_image').addClass(clip); }
function removeLeadClip(clip) { Ext.fly('lead_stories_image').removeClass(clip); }
function pauseTimer() { Ext.fly('newsswitcher').dom.pauseTimer(); }

var _fbInit = false;
var _fbVis = false;
var _fbActiveTag = 'tab_rm_one';
var _fbApiKey = 'ea6aa3839a9a30d6f3945f388d70e343';
function toggleFacebook(show) {
    if(!_fbInit) {
        FB.init(_fbApiKey, "/xd_receiver.htm");

        try {
            var els = Ext.select('#fbComments li');
            els.addClassOnOver('hover');
            els.addListener('click', function(e) {
                var id = e.getTarget().id
                if(id != _fbActiveTag) {
                    var el = e.getTarget(null, null, true);
                    if(el.dom.nodeName != "LI") el = el.parent('li');
                    id = el.id;
                    Ext.fly(_fbActiveTag).removeClass('active');
                    Ext.fly(_fbActiveTag+'_content').removeClass('tc_active');
                    el.addClass('active');
                    Ext.fly(el.id+'_content').addClass('tc_active');
                    _fbActiveTag = el.id;
                }

                if(id == "tab_rm_two" || id == "tab_rm_three") {
                    Ext.Ajax.request({
                        url: (id == "tab_rm_two") ? '/index/tweets' : '/index/tweets/type/indytalk',
                        callback: function(opt, success, resp) {
                            Ext.fly(this.tab_id + '_content').update(resp.responseText);
                        },
                        scope: {tab_id: id }
                    })
                }
            });
        } catch(e) {
            console.warn(e);
        }

        _fbInit = true;
    }

    if(!Ext.fly('lead_stories_image').hasClass('ts_leadclip')) setLeadClip('ts_leadclip');
    if(show == "true") {
        Ext.fly('fbComments').show();
    } else {
        Ext.fly('fbComments').hide();
    }
    _fbVis = Ext.fly('fbComments').isVisible();
}

function timerComplete(type) {
    if(_fbVis) toggleFacebook("false");
    if(type == "next") {
        _curStory++;
        if(_curStory > (_stories.length-1)) _curStory = 0;
    } else {
        _curStory--;
        if(_curStory < 0) _curStory = (_stories.length-1);
    }
    switchStory();
}

var leadTemplateHTML = new Ext.XTemplate(
    '<tpl if="this.showMore(showmorelink)">',
    '<a href="{link}"><img src="{img_path}" width="574" height="323" /></a>',
    '</tpl>',
    '<tpl if="this.showMore(showmorelink) == false">',
    '<img src="{img_path}" width="574" height="323" />',
    '</tpl>',
    { showMore : function(val) { return (val == 'yes') } }
);

function switchStory() {
    var s = _stories[_curStory];

    var _copy = Ext.fly('lead_stories_copy_inner');
    _copy.child('h1').update(s.headline);
    _copy.child('div').update(s.blurb);

    if(Ext.fly('lead_stories_image').hasClass('leadclip')) {
        removeLeadClip('leadclip');
        removeLeadClip('ts_leadclip');
        removeLeadClip('chat_leadclip');
    }
    var flashvars = {};
    var width = 574;
    var height = 323;

    if(s.type_id == 19 || s.type_id == 24) {
        width = 900;
        height = 696;
        var flashvars = _RMFlashVars;
    }
    if (Ext.fly('flashLeadContainer') == null) {
        Ext.DomHelper.insertFirst('lead_stories_image_inner', {
            id: 'flashLeadContainer',
            tag: 'div'
        });
    }

    if (Ext.fly('leadStorySwf')) {
        swfobject.removeSWF('leadStorySwf');
    }
    
    if(typeof s.showmorelink == "undefined") {
    	s.showmorelink = "no";
    }

    if (s.img_path.indexOf('.swf') > 0) {

        if (s.showmorelink == "yes") {
            flashvars.linkurl = s.link
        }
        var attributes = {};
        var params = {};
        params.wmode = "transparent";
        params.allowScriptAccess = "sameDomain";
        params.allowFullScreen = "true";
        attributes.id = "leadStorySwf";
        swfobject.embedSWF(s.img_path, "flashLeadContainer", width, height, "9.0.115", false, flashvars, params, attributes);
    } else {
    	Ext.fly('flashLeadContainer').show();
		var v = leadTemplateHTML.overwrite('flashLeadContainer', s, true);
    }

    Ext.fly('newsswitcher').dom.resetTimer();
}

function addDesignClip(e) {
    try {
        var event = e || window.event;
        event.preventDefault();
    } catch(err) {}

    Ext.fly("designit_container_clip").replaceClass("designit_no_clip", "designit_clip");
}

function removeDesignClip(e) {
    try {
        var event = e || window.event;
        event.preventDefault();
    } catch(err) {}

    Ext.fly("designit_container_clip").replaceClass("designit_clip", "designit_no_clip");
}

function gotoURL(url)
{
    document.location.href = url;
}

function logPageView(url)
{
    if(url == "" || url == undefined) return;
    try {
        pageTracker._trackPageview(url);
    } catch (err) {}
}

function initTwitter(el){
    Ext.fly(el.id + '_content').update("Loading...");
    Ext.Ajax.request({
        url: '/index/tweets/type/indytalk',
        callback: function(opt, success, resp){
            Ext.fly(this.tab_id + '_content').update(resp.responseText);
        },
        scope: {
            tab_id: el.id
        }
    })
}

function initFacebook(){
    if (_fbInit == false) {
        FB.init(_fbApiKey, "/xd_receiver.htm");
        _fbInit = true;
    }
}



Ext.onReady(function(){
    var els = Ext.select('#sidebar_tabs_inner li');
    els.addClassOnOver('hover');
    els.addListener('click', function(e){
        if (e.getTarget().id != active_tab) {
            var el = e.getTarget(null, null, true);
            if (el.dom.nodeName != "LI") el = el.parent('li');
            logPageView('/tabchange/' + el.id);
            Ext.fly(active_tab).removeClass('active');
            Ext.fly(active_tab + '_content').removeClass('tc_active');
            el.addClass('active');
            Ext.fly(el.id + '_content').addClass('tc_active');

            if (el.id == "tab_four") {
                initTwitter(el);
            } else if (el.id == "tab_five" && !_fbInit) {
                initFacebook();
            }
            active_tab = el.id;
        }
    });

    var activeTab = Ext.query('ul.tabs li.active');
    if(activeTab.length >= 1) {
        activeTab = activeTab[0];
        if (activeTab.id == "tab_four") {
            initTwitter(activeTab);
        } else if (activeTab.id == "tab_five" && !_fbInit) {
            initFacebook();
        }
    }

    Ext.select('a.ga_external').on('click', function(e){
        el = e.getTarget(null, null, true);
        if(el.dom.nodeName != 'A') el = el.parent('a');
        logPageView('/outbound/' + el.dom.href.replace("http://",''));
    });


    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    params.allowScriptAccess = "sameDomain";
    var attributes = {};
    attributes.id = "newsswitcher";
    swfobject.embedSWF("/images/homepage/newsswitcher.swf", "nhc_replace", "110", "30", "9.0.115", false, flashvars, params, attributes);

    var flashvars = {};
    var params = {};
    params.wmode = "transparent";
    var attributes = {};
    attributes.id = "sponsor_bar";
    swfobject.embedSWF("/images/flash/sponsor_bar.swf?v=1.0.1", "sb_container", "900", "42", "9.0.0", false, flashvars, params, attributes);
});


