
var activeMiniTrailer = false;

function videoFinish() {
    if (activeMiniTrailer) {
    	activeMiniTrailer.addClass('postroll');
    	activeMiniTrailer.find('.video_overlay').show();
    	activeMiniTrailer.children('embed').remove();
    	activeMiniTrailer.children('object').remove();
    	activeMiniTrailer = false;
	}
}
function miniTrailer(spot, id, forcejoin) {
	videoFinish();

	var player = (typeof spot == 'string' ? $(spot) : spot);
	player.children().css('display', 'none');

	activeMiniTrailer = player;

	var videooptions_url = "get_scene/" + id + "/autoplay-1/show-info-0" + (forcejoin ? "/force-1" : "");

	player.flash(
		{
			src: "http://static.contentabc.com/player/wide_tour_player.swf",
			width: 470,//478,
			height: 282,
			wmode: "transparent",
			align: "middle",
			allowfullscreen:true,
			allowscriptaccess: "always",
			allownetworkaccess: "always",
			flashvars: {
				options: videooptions_url
			}
		},
		{version:"10.0.22"}
	);
}

function introVideo() {
    var previewer = $('.video_preview:first');
    previewer.html('');
    previewer.parents('a').click(function() { return false; });

    var videooptions_url = "http://static.contentabc.com/intros/iktg/iktg-intro.xml";

    previewer.flash(
        {
            src: "http://static.contentabc.com/player/wide_tour_player.swf",
            width: 396,
            height: 223,
            wmode: "transparent",
            align: "middle",
            allowfullscreen:true,
            allowscriptaccess: "always",
            allownetworkaccess: "always",
            flashvars: {
                options: videooptions_url
            }
        },
        {version:"10.0.22"}
    );
}

$(document).ready(function(){

	introVideo();

    $('a.playbtn').click(function() {
        if($(this).attr('href') == '#')
        {
        	var id = $(this).attr('id').replace('btn_', '');
			miniTrailer($(this).parent().parent(), id, 0);
			return false;
        }
    });


    $('.play_overlay').click(function() {
		var link = $(this).find('a.playbtn:first');

        //need to set the yahoo tracking here as well
		name = $(this).attr('yahootrackid');
		YWATracker.setCF(2, name);
		YWATracker.setAction("03");
		YWATracker.submit_action();

        if(link.attr('href') == '#')
        {
        	var id = link.attr('id').replace('btn_', '');
			miniTrailer($(this).parent(), id, 0);
			return false;
        }
        else
        {
			window.location.href = link.attr('href');
        }
    });

    // Track all basic links
	$('a.trackyahoo').each(function(){
		try {
			if (!YWATracker || !$(this).attr('yahootrackid'))
				return;

			$(this).click(function(){
				var name = $(this).attr('yahootrackid').replace('[]#&;:?-*~`´)(=%!','');

				if (name && YWATracker){
					name = name.replace('not_clickable_',' Trailer ').replace('undefined',' Trailer ');
					YWATracker.setCF(2, name);
					YWATracker.setAction("03");
					YWATracker.submit_action();
				}
			});
		} catch(e) {}
	});
});
