$(document).ready(function() {
    writeFlash();
    callCycle();
    correctRightColHeight();
    $('.controls a').hover(on, off);
});


function correctRightColHeight() {
    var $the_correct_height = $("#content").height();
    $("div.rightCol").css({ height: $the_correct_height + "px" });
}


function callCycle() {
    $('<div class="slide-controls"><a class="previous-slide" href="#">Previous slide</a><ul class="controls"></ul><a class="next-slide" href="#">Next slide</a></div>').appendTo("#slideshow");
    $('#the').cycle({
        timeout: 5000,
        delay: 2000,
        speed: 500,
        pager: '.controls',
        next: '.next-slide',
        prev: '.previous-slide',
        before: onAfter,
        pagerAnchorBuilder: function(idx, slide) {
            var src = $('img', slide).attr('rel');
            return '<li><a href="#" rel="' + $(slide).attr('rel') + '" class="slide-item"></a><div class="tooltip"><h3>' + slide.title + '</h3></div></li>';
        }
    });

    $("a.next-slide,a.previous-slide,a.slide-item").click(function() {
        $("#the").cycle('pause');
    });
};


function getExt(file) {
    return (/[.]/.exec(file)) ? /[^.]+$/.exec(file.toLowerCase()) : '';
}

function writeFlash() {
    $.each($("#the li"), function(i) {
        var thesrc = $('img', $(this));
        var thesrcsrc = $(thesrc).attr("src");
        var ext = getExt(thesrcsrc);
        if (ext == "swf") {
            var typeMarkup = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="291" height="126" id="slide' + i + '"><param name="wmode" value="transparent" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="' + thesrcsrc + '" /><embed src="' + thesrcsrc + '" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="291" height="126" wmode="transparent"></embed></object>';
            $(thesrc).remove();
            $(this).prepend(typeMarkup);
        }
    })
}


var indexdepth = 0;
function onAfter() {
    var $file = $(this).attr('rel');
    var $newimage = '<img src="' + $file + '" alt=""/>';
    $('.image-panel').prepend($newimage);
    $('.image-panel img:first-child').css({ 'opacity': 0 }).fadeTo(1000, 1).css('zIndex', indexdepth++);
    if ($('.image-panel').children().length > 2) {
        $('.image-panel img:last-child').remove();
    }
    replaceBackgroundImage();
}


function replaceBackgroundImage() {
    $('div.backgroundImage').removeAttr("style");
}

/* Reload Homepage slideshow sIFR content */
function rewriteFlash() {
    var crankMinus = { src: '/templates/public/flash/crankMinus.swf' };
    var crankPlus = { src: '/templates/public/flash/crankPlus.swf' };

    // You probably want to switch this on, but read <http://wiki.novemberborn.net/sifr3/DetectingCSSLoad> first.
    // sIFR.useStyleCheck = true;
    sIFR.activate(crankMinus);
    sIFR.activate(crankPlus);

    sIFR.replace(crankPlus, {
        selector: '.tactical-title'
	  , wmode: 'transparent'
      , css: [
        '.sIFR-root { color: #ef3e42;}'
      ],
        tuneHeight: -5, offsetTop: 0
    });


    sIFR.replace(crankMinus, {
        selector: '.tactical-date'
    	  , wmode: 'transparent'
    	  , forceSingleLine: true
          , css: [
            '.sIFR-root { color: #666666;}'
          ],
        tuneHeight: -2, offsetTop: -2
    });

}

function on() { $(this).parent().addClass('on'); }
function off() { $(this).parent().removeClass('on'); }