var slideInterval = 5000;

Array.prototype.contains = function (element){
	for (var i = 0; i < this.length; i++){
		if (this[i] == element){
			return true;
		}
	}
    return false;
};

jQuery(document).ready(function(){
	initHomeAjax();
	initAlignment();
});

var activeSrc = Array();

function initHomeAjax(){
	if(!isHome) return false;
	
	jQuery.post(HomePath,{ home_ajax : true },function(data){
		jQuery('#content div.image').append(data);
		jQuery('#content div.image > div').hide();
		
		if ($.browser.msie == false || ( $.browser.msie && $.browser.version != "6.0" ) ){
			jQuery('#content div.image > div img').each(function(){
				$(this).attr("rel",$(this).attr("src"));
			});
		}
		
		
		$divs = jQuery("#content div.image > div");
		var divsSize = $divs.size();
		var activeNumber = 0;
		var current;
		var theSame = false;
		var $activeDivs;
		var activeDivsSize = 0;
		while(activeNumber<5){
			current = Math.floor( Math.random() * (divsSize) );
			$temp = jQuery("#content div.image > div:eq("+current+")");
			if (!$temp.hasClass('active')){
				theSame = false;
				$activeDivs = jQuery("#content div.image > div.active");
				activeDivsSize = $activeDivs.size();
				for (var j=0;j<activeDivsSize;j++){
					if (jQuery($activeDivs.get(j)).find("img").attr("rel") == $temp.find("img").attr("rel") ){
						theSame = true;
						//alert("the same");
					}
				}
				if (!theSame){
					$temp.addClass("active");
					activeSrc.push($temp.find("img").attr('src'));
					activeNumber++;
				}
				
			}
		}
		
		jQuery("#content div.image > div.active").fadeIn();
		setInterval(function(){
			var $activeDivs = jQuery("#content div.image > div.active");
			var activeSize = $activeDivs.size();
			var toChange = Math.floor( Math.random() * (activeSize) );
			var flag = true;
			var $otherDivs = jQuery("#content div.image > div:not(.active)");
			var otherDivsSize = $otherDivs.size();
			var theSame = false;
			while(flag){
				var newDiv = Math.floor( Math.random() * (otherDivsSize) );
				$tmp = $("#content div.image > div:not(.active):eq("+newDiv+")");
				theSame = false;
				for (var j=0; j<activeSize; j++){
					if (jQuery($activeDivs.get(j)).find("img").attr("rel") == $tmp.find("img").attr("rel") ){
						theSame = true;
						//alert("the same");
					}
				}
				if (!theSame){
					flag = false;
					jQuery("#content div.image > div.active:eq("+toChange+")").removeClass("active").fadeOut();
					activeSrc[toChange] = $tmp.find("img").attr("src");
					$tmp.addClass("active").fadeIn();
					
				}
			}
			//jQuery("#content div.image > div:not(.active)").show();
			//jQuery("#content div.image > div:not(.active)").css("opacity",0.5);
		},slideInterval);
		
	});
};

function initAlignment(){
	
	var contentHeight = jQuery("#content").height();
	var sidebarHeight = jQuery("#sidebar").height();
	if (contentHeight < sidebarHeight){
		jQuery("#content").css("height",sidebarHeight+"px");
	}
	if ( jQuery(window).height() > jQuery("#main-wrapper").height() ){
		jQuery("#main-wrapper").css("height",jQuery(window).height()-40+"px");
	}
}