$(function () {

    // Share this 
    stLight.options({
        publisher: '123456-7890a-bcdefg-hijkl',
        offsetLeft: '-270',
        offsetTop: '150'
    });

    // Lightbox
    if ($('.lightbox').is('.lightbox')) {
        $(".lightbox").colorbox({ slideshow: false, width: '570px', scrolling: false, onComplete: function () {
            stButtons.locateElements();
        }, onClosed: function () {
            $('#page').removeClass('noprint');
        }
        }).click(function () {
            $('#page').addClass('noprint');
        });
    }

    // Events accordion - Upcoming
    $('.event-row-upcoming').hide();
    $('.event-show-upcoming').toggle(function () {
        $('.event-row-upcoming').slideDown("slow");
        $(this).removeClass('event-head').addClass('event-head-open');
    }, function () {
        $('.event-row-upcoming').slideUp("slow");
        $(this).removeClass('event-head-open').addClass('event-head');
    });

    // Events accordion - Past
    $('.event-row-past').hide();
    $('.event-show-past').toggle(function () {
        $('.event-row-past').slideDown("slow");
        $(this).removeClass('event-head').addClass('event-head-open');
    }, function () {
        $('.event-row-past').slideUp("slow");
        $(this).removeClass('event-head-open').addClass('event-head');
    });

    // Country language select drop down
    $(".dropdown dd ul").hide();

    $(".dropdown img.flag").addClass("flagvisibility");

    $(".dropdown dt a").click(function () {
        $(".dropdown dd ul").toggle();
        return false;
    });

    $(".dropdown dd ul li a").click(function () {
        var text = $(this).html();
        $(".dropdown dt a span").html(text);
        $(".dropdown dd ul").hide();
        $("#result").html("Selected value is: " + getSelectedValue("sample"));
    });

    function getSelectedValue(id) {
        return $("#" + id).find("dt a span.value").html();
    }

    $(document).bind('click', function (e) {
        var $clicked = $(e.target);
        if (!$clicked.parents().hasClass("dropdown"))
            $(".dropdown dd ul").hide();
    });
    $("#flagSwitcher").click(function () {
        $(".dropdown img.flag").toggleClass("flagvisibility");
    });

    if ($('.mixes-nav').is('.mixes-nav')) mixesNavAccordion();

    if ($('.mixes-carousel').is('.mixes-carousel')) DisCarousel.init.setup(100, 20);
    miniCarousel();
    if ($('.the-story').is('.the-story')) DisStoryTimeline.com.setup();

    //Flash movie player
    if ($('.video-trigger').is('.video-trigger')) videoPlayer();
});

function videoPlayer()
{
	$('body').append('<div id="video-overlay"></div><div id="video-player"></div>');
	
	$(window).keypress(function(e){
		if(e.charCode == 0) closeVideo();
	});
    	
	$('.video-trigger').click(function(){
    	var sVideoSrc = $(this).attr('rel');
    	swfObject(sVideoSrc);
    	
		$('#video-player').css({'display':'block'});
		$('#video-overlay').fadeIn('fast');
		centerVideo();
		
		return false;
    });
    
    $('#video-overlay').click(function(){
    	closeVideo();
    });
    
    $(window).resize(function(){
		centerVideo();
	});
}

function swfObject(sVideoSrc)
{
	//SWF object stuff
	var flashvars = {
		src:sVideoSrc
	};
	var params = {};
	var attributes = {};

	swfobject.embedSWF("/flash/player.swf", "video-player", "480", "360", "9.0.0", "expressInstall.swf",flashvars,params,attributes);
}

function closeVideo()
{
	$('#video-overlay').hide();
    $('#video-player').remove();
    $('body').append('<div id="video-player"></div>');
}

function centerVideo()
{
	var nLeftOffset = document.body.clientWidth/2 - 480/2;
	$('#video-player').css({'left':''+nLeftOffset+'px'});	
}

function mixesNavAccordion()
{
    $('.mixes-nav').find('h3').each(function () {
        $(this).css({'cursor':'pointer'})
        if (!$(this).hasClass('open')) $(this).next().hide();

        $(this).click(function () {
            if (!$(this).hasClass('open')) {
                $('.open').next().slideUp('fast').prev().removeClass('open');
                $(this).next().slideDown('fast').prev().addClass('open');
            }
            return false;
        })
    });
}

function miniCarousel()
{
	var oMinCarousel = $('.mini-carousel');
	
	oMinCarousel.find('.thumbs').show();
	oMinCarousel.find('.images').css({'height':'290px'})
	oMinCarousel.find('.images img').hide().css({'position':'absolute'});
	oMinCarousel.find('.images img').eq(0).show();
	
	oMinCarousel.find('.thumbs img').click(function(){
		var nRel = $(this).attr('rel');
		
		if(nRel != oMinCarousel.find('.current').attr('rel'))
		{
			oMinCarousel.find('.current').removeClass('current');
			oMinCarousel.find('.images img:visible').hide();
			oMinCarousel.find('.images img').eq(nRel).show();
			oMinCarousel.find('.thumbs img').eq(nRel).addClass('current');
		}
	});
}

function assignLightBoxClick() { 
    // Lightbox
    
        $(".lightbox").colorbox({ slideshow: false, width: '570px', scrolling: false, onComplete: function () {
            stButtons.locateElements();
        }, onClosed: function () {
            $('#page').removeClass('noprint');
        }
        }).click(function () {
            $('#page').addClass('noprint');
        });
}
