var youTubes = {
    gateway : '<if' + 'rame title="YouTube video player" width="853" height="510" src="http://www.youtube.com/embed/hvBknEDBehg" frameborder="0" allowfullscreen></if' + 'rame>', 
    videoconf : '<if' + 'rame title="YouTube video player" width="853" height="510" src="http://www.youtube.com/embed/ZXwbkrkUvXc" frameborder="0" allowfullscreen></if' + 'rame>'
};

var product_delay = 4500;
var curProduct = null;
var curProductID = '';
var curVid = 0;
var numItems = 0;

function cycle_run(id)
{
    curProduct = setTimeout('cycle_products(' + id + ')', product_delay);
}

function cycle_products(id)
{
    if(!curProduct) return;

    var nextid = id + 1;
    if(nextid > numItems - 1) nextid = 0;

    $('#hero-cycle li:eq(' + id + ')').fadeOut(750);
    $('#hero-cycle li:eq(' + nextid + ')').fadeIn(750);

    $('#indicator-bar li a').removeClass('selected');
    $('#indicator-bar li:eq(' + nextid + ') a').addClass('selected');

    cycle_run(nextid);
    curProductID = $('#hero-cycle li:eq(' + nextid + ')').attr('pid');
}

function cycle_stop(pid)
{
    clearTimeout(curProduct);
    curProduct = null;
    curProductID = pid;
        
    $('#indicator-bar li a').removeClass('selected');
    $('#indicator-bar li:eq(' + curProductID + ') a').addClass('selected');
        
    $('#hero-cycle li').stop().css({ opacity: '1.0' }).hide();
    $('#ppanel-' + curProductID).show();
}

$(document).ready(function(){
    $(".equal-heights").equalHeights();
	
    // SEO BLOCK
    $('#contentWrap .pageWide').prepend($('.seobottom')); // move tab menu, which is currently display:none, from head to foot
    $('.seobottom').css('display', 'block'); // display tab menu
    // For older SEO footer:
    $('#contentWrap .pageWide').prepend($('#seoblock')); // move tab menu, which is currently display:none, from head to foot
    $('#seoblock').css('display', 'block'); // display tab menu

    // Homepage footer tabs
    $("#seoblock").tabs();
    $("#seoblock .readmore").click(function(){
        $("#seoblock .readmore").hide();
        //$("#seo-footer .remainder").show();
        $('.remainder').show().end().find('p.collapse').css('display','block');
    });
    $("#seoblock .collapse").click(function(){
        $("#seoblock .collapse").hide();
        //$("#seo-footer .remainder").hide();
        $('.remainder').hide().end().find('p.readmore').css('display','block');
    });
		
		
	// HERO CYCLE:
    numItems  = parseInt($('#hero-cycle li').length);
    
    $('#hero-cycle li').each(function(i, d){
        $(this).attr({ indx: i });
        $('<li />').attr({ indx: i }).appendTo('#indicator-bar');
        $('<a href="" />').appendTo('#indicator-bar li:last');
    });
    
    var ibarwidth = $('#indicator-bar li').outerWidth(true) * $('#indicator-bar li').length;
    
    $('#indicator-bar').css({ width: ibarwidth + 'px'});

    $('#hero-cycle li:gt(0)').hide();
    $('#quote-cycle li:gt(0)').hide();
    $('#indicator-bar li:eq(0) a').addClass('selected');
    curProductID = $('#hero-cycle li:eq(0)').attr('indx');
    
    cycle_run(0);  // So bizarre [TK]
    
    //hover over the product selector or product image
    $('#hero-cycle li, #indicator-bar li').mouseenter(function(){
        cycle_stop($(this).attr('indx'));
    });
    
    //unhover the product selector or product image
    $('#hero-cycle li, #indicator-bar li').mouseleave(function(){
        if(!curProduct)
        {
            var indx = $(this).attr('indx');
            $('#indicator-bar li:eq(' + indx + ') a').addClass('selected');
            cycle_run(indx);
        }
    });
    
    //disable selector click
    $('#indicator-bar li a').click(function(){ return false; });
	
	
	// VIDEO CAROUSEL
    $("#video-carousel").jCarouselLite({ 
        circular: true,
        visible: 1,
        btnPrev: ".btnPrev",
        btnNext: ".btnNext",
        start: 0
    });

    // YOUTUBE THING
    //quick and dirty youtube video display
    $('.show-youtubes').click(function(){
        var vid = $(this).attr('tubeid'), 
            vidw    = parseInt($(this).attr('vidw')), 
            vidh = parseInt($(this).attr('vidh'));
        overlayShadow(function(){
            $('<div />')
                .attr({ id: vid })
                .addClass('youtube-popup')
                .css({ width: vidw + 'px', height: vidh + 'px', marginLeft: parseInt((vidw / 2) * -1) + 'px', marginTop: parseInt((vidh / 2) * -1) + 'px' })
                .html(youTubes[vid])
                .appendTo('body');
            $('<div />')
                .addClass('close-youtube')
                .html('CLOSE')
                .css({ left: (vidw - 20) + 'px' })
                .click(function(){ killShadow(Array('#' + vid)) })
                .appendTo('#' + vid);
            $('#' + vid).show();
        }, Array('#' + vid));
        
        return false;
    });	
	
});


