var animTime = 7;
var hasImages;
var browser = true;

if (navigator.userAgent.indexOf("BlackBerry") >= 0){
	browser = false;
}

$(document).ready(function(){
	if(!browser){
		$('body').addClass("blackberry");
		//removeElementsforBrowser();
	}
	
	hasImages = $('#slider img').length > 1;
	setupRotator();
	var slideAmount = $('#slider').children().length;
	if(true || browser){
		$('#slider.nivoSlider').nivoSlider({
			animSpeed:1000,pauseTime:animTime*1000,directionNav: false,controlNav: false,
			beforeChange: function(){
				if($('.quote').length > 1) textRotate();
			}
		});
		if(slideAmount == 1){
			try{
				$('#slider.nivoSlider').data('nivoslider').stop();
			}catch(err){}
		}
	}
	
	$("ul.sf-menu").supersubs({ 
		minWidth:    5,   // minimum width of sub-menus in em units 
		maxWidth:    107,   // maximum width of sub-menus in em units 
		extraWidth:  1     // extra width can ensure lines don't sometimes turn over due to slight rounding differences and font-family 
	}).superfish({  // call supersubs first, then superfish, so that subs are not display:none when measuring. Call before initialising containing tabs for same reason. 
		onBeforeShow:function(){
			var ul = this;
			if(ul.parent( "li" ).hasClass("root")){
				setTimeout(function(){checkMenuWindow(ul)}, 50);
			}
		}
	});
	arrangeSidebarHeight();
	
	$(".sf-menu-container > ul > li > a").mouseover(function() {
		soundManager.play('botones');
	});
	
});

function removeElementsforBrowser(){
	//remove random image
	var children = $('#slider').children();
	var rnd = Math.floor(Math.random() * children.length);
	for(var index in children.toArray()){
		if(index != rnd){
			$(children[index]).remove();
		}
	}
	
	var quotes = $('div.quotes').children();
	if(quotes[rnd] == null){
		rnd = Math.floor(Math.random() * quotes.length);
	}
	for(var index in quotes.toArray()){
		if(index != rnd){
			$(quotes[index]).remove();
		}
	}
}

function checkMenuWindow(ul){
	ul.css("left", "0px")
	var ww = $(window).width();
	var offset = ul.offset();
	if(offset == null) return;
    var locUL = offset.left + ul.width();
    if (locUL > ww){
    	ul.css('left', "-" + (locUL - ww + 10) + "px");
    }
}

function arrangeSidebarHeight(){
	var height1 = $("div.container-content").innerHeight();
	var height2 = $("div.container.sidebar div.sidebar").innerHeight();
	if(height2 < height1){
		$("div.container.sidebar div.sidebar").css("height", height1 + "px");
	}
	
}

function setupRotator(){
	$('.quote:first').addClass('current').show();///.fadeIn(1000);
	if(!hasImages && $('.quote').length > 1){
		setInterval('textRotate()', animTime*1000);
	}
}

function textRotate(){
	var current = $('.quotes > .current').removeClass('current').fadeOut(1000);
	if(current.next().length == 0){
		$('.quote:first').addClass('current').fadeIn(1000);
	}else{
		current.next().addClass('current').fadeIn(1000);
	}
}
