﻿$(document).ready(function(){
    var framesCount = $("iframe").length;
    if (framesCount == 0)
    {
        initAll();
    }
    else
    {
       $($("iframe")[framesCount - 1]).ready(function() {
           window.setTimeout("initAll();", 50);
       });
    }
});





function initAll()
{
    
	initHeaderHeight();
	initSlideShow();
    initJumprLinks();
}

function initSlideShow()
{
    $('.slideshow').cycle({
		fx: 'scrollLeft',
		speed: 1000,
		timeout: 7000
	});
}

function initHeaderHeight()
{
    imagesArr = $("#Logo img");
    if (imagesArr.length == 0)
    {
        $("#Header").height(68);
        $("#Navi").css("top", "45px");
    }

    var logoObj = $(".LogoStart");
    if (logoObj.length > 0)
    {
        var height_before = $("#content_wide").height();
        $("#content_wide").height(height_before + $(logoObj[0]).height())
    }
}

function initJumprLinks()
{
    var $list = $('#rp_list ul');
    var elems_cnt = $list.children().length;
    var initial = 200;

    if (elems_cnt == 0) return;

    $list.find('li').hide().andSelf().find('div').css('margin-left',-initial+'px');
    

	var loaded	= 0;
    var d = 200;
	var maxToShow = 4;
    //show 5 random posts from all the ones in the list. 
	//Make sure not to repeat
    if (elems_cnt < maxToShow)
    {
        for (var i = 0; i< elems_cnt; i++)
        {
            var $elem	= $list.find('li:nth-child('+ (i+1) +')');
            $elem.show();
        }
    }

    else
    {
	   while(loaded < maxToShow)
       {
		    var r 		= Math.floor(Math.random()*elems_cnt);
		    var $elem	= $list.find('li:nth-child('+ (r + 1) +')');
		    if($elem.is(':visible'))
			    continue;
		    else
            {
                if ($elem.length > 0)
                {
                    $elem.show();
		             ++loaded;
                }
            }
	    }
    }

	$list.find('li:visible div').each(function(){
		$(this).stop().animate({
			'marginLeft':'0px'
		},d += 100);
	});

    $list.find('li:visible').live('mouseenter',function () {
	$(this).find('div').stop().animate({
		'marginLeft':'-153px'
	},200);
    }).live('mouseleave',function () {
	    $(this).find('div').stop().animate({
		    'marginLeft':'0px'
	    },200);
    });
}
