function trackEvent(category, action, label, value) {
    if (typeof (value) != 'undefined') {
        var tracker = function() { _gaq.push(['_trackEvent', category, action, label, value]); };
    }
    if (typeof (label) != 'undefined') {
        var tracker = function() { _gaq.push(['_trackEvent', category, action, label]); };
    }
    else {
        var tracker = function() { _gaq.push(['_trackEvent', category, action]); };
    }
    // push to analytics queue
    try {
        tracker();
        //console.log('_gaq.push([\'_trackEvent\', ' + category + ', ' + action + ', ' + label + ', ' + value + ']);');
    }
    catch (e) {
        // log error
        //console.log('an event tracking error occurred');
    }
}


function advertisingVideos() {
    var $videoSelector = $('#slideblock a');
    $videoSelector.click(function(e) {
        e.preventDefault();

        if (!$(this).hasClass('disabled')) {
            // grab data
            var filename = $(this).attr('href');
            var title = $(this).attr('title');
            // set hash tag
            $.address.value(title.replace(/ /g, '-') + '/');
            if (filename != "#") {
                try {
                    conoco.externalInterface('#video-swf').initPlayer(filename, title);
                }
                catch (e) {
                    // swf not ready
                }
                $(this)
					.addClass('active')
					.siblings()
						.removeClass('active')
				;
            }
        }
        return false;
    });

    // handle hash tag
    $.address.change(function(event) {
        if (typeof (event.pathNames[0]) != 'undefined' && event.pathNames[0] != 'tv' && event.pathNames[0] != 'gallery') {
            var videoTitle = event.pathNames[0].replace(/-/g, ' ');
            if (typeof (videoTitle) != 'undefined') {
                var $file = $("#slideblock a[title='" + videoTitle + "']");
                var filename = $file.attr('href');
                $("#vid-titles h2").text(videoTitle);
                Cufon.replace(['#vid-titles h2'], {
                    fontFamily: 'GothamCondensed'
                });
                videoBlackout(filename, videoTitle);
                //these don't reference the videos now :(
                //$('#ad-facebook').attr('href', "http://www.facebook.com/share.php?u=http://www.phillips66.com/advertising.aspx%23/" + event.pathNames[0]);
                //$('#ad-twitter').attr('href', "http://twitter.com/home?status=Watching http://www.phillips66.com/advertising.aspx%23/" + event.pathNames[0]);
            }
        }
    });

    $('#ad-facebook').click(function() {
        _gaq.push(['_trackEvent', 'share-facebook', window.location.hash.substr(2)]);
    });
    $('#ad-twitter').click(function() {
        _gaq.push(['_trackEvent', 'share-twitter', window.location.hash.substr(2)]);
    });
}

function videoBlackout(video_url, videoTitle) {
    /*if (video_up == false) {
    if (getCurrentScroll() > 160) {
    $("body,html").animate({ scrollTop: 160 }, 700, "easeInOutQuart");
    }
    $('#blackout').hide().css({ "left": 0 }).fadeIn(500);
    $('.advertising .shield').stop().animate({ "opacity": 0, "height": "450px" }, 500, "easeInOutQuart", function() {
    videoEmbed(video_url, videoTitle);
    });
    video_up = true;
    }
    else {*/
    $("#blackout_inner").empty();
    videoEmbed(video_url, videoTitle, true);
    /* }

    $('#blackout .close').click(function(e) {
    e.preventDefault();
    $.address.value('');
    //stop the video
    $("#blackout_inner").empty();
    $('#blackout').fadeOut(500, function() { $('#blackout').hide().css({ "left": 0 }) });
    $('.advertising .shield').stop().animate({ "opacity": 1, "height": "604px" }, 500, "easeInOutQuart");
    video_up = false;
    });*/
}

function videoEmbed(filename, title, autoplay) {
    $("#blackout_inner").append('<div id="video_holder"></div>');

    var config = {
        embed: "video_holder",
        src: "media/swf/videoplayer.swf",
        width: "594",
        height: "336",
        flashVersion: '9.0.0',
        expressInstall: 'media/swf/express-install.swf',
        vars: { flv_url: filename, flv_title: title, autoplay: autoplay },
        params: { scale: "scale", bgcolor: "#98A2AB", menu: "false", wmode: "opaque", allowFullscreen: true, allowscriptaccess: "always" },
        attributes: { id: 'video-swf', name: 'video-swf' }
    };

    var extension = filename.substr(filename.length - 3);
    var video_tag_dir = "media/video/";
    var video_tag_url;
    if (extension = "f4v") {
        video_tag_url = "media" + filename.substr(2, filename.length - 5); // + ".ogg";
    }
    else {
        //add mp3 and youtube
    }

    var fp_version = swfobject.getFlashPlayerVersion();

    if (fp_version["major"] >= 9) {
        swfobject.embedSWF(config.src, config.embed, config.width, config.height, config.flashVersion, config.expressInstall, config.vars, config.params, config.attributes);
    }
    else {
        $('#video_holder').append('<video controls="controls" width="' + config.width + '" height="' + config.height + '"><source src="' + video_tag_url + 'mp4" "type="video/mp4" /><source src="' + video_tag_url + 'ogg" "type="video/ogg" /></video>');
    }
}

$(document).ready(function() {
    advertisingVideos();
});

function jsIsReady() {
    return true;
}
