// JavaScript Document
function roll(img_name, img_src){
	document[img_name].src = img_src;
}

function toggleNaviMenu(id,lang,mode){
	oImg = document.getElementById("sub_but" + id);
	if(mode=="active"){
		oImg.src="assets/templates/" + lang + "/img/navi/" + id + "_active.jpg";
	}else if (mode=="hover"){
		oImg.src="assets/templates/" + lang + "/img/navi/" + id + "_hover.jpg";
	} else {
		oImg.src="assets/templates/" + lang + "/img/navi/" + id + ".jpg";
	}
}

var $jq = jQuery.noConflict();
var quotesTickerTimeout = 7000;
var quoteSelectedTimeout = 3000;
var timer;

$jq(document).ready(function(){
	setQuotesTickerTimeout("client_quotes_ticker", "client_quotes_navbar", quotesTickerTimeout);
	updateQuotesNavigation("client_quotes_ticker", "client_quotes_navbar");
});

function getDivChildren(parentId){
	var container = document.getElementById(parentId);
	var children = new Array();
	var k = 0;
	if (container) {
		for (i=0; i<container.childNodes.length; i++) {
			if (container.childNodes[i].tagName == "DIV") {
				children[k++] = container.childNodes[i]; 
			}
	    }
	}
	return children;
}

function getFirstVisibleDivIndex(children){
	var selected = 0;
    for (i=0; i<children.length; i++) {
		if (children[i].style.display != "none") {
			selected = i;
		}
	}
	return selected;
}

function updateQuotesNavigation(parentId, navId, selectedDivId){
	var container = document.getElementById(navId);
	var children = getDivChildren(parentId);
	if (container) {
		var str = "";
	    for (i=0; i<children.length; i++) {
			if (children[i].id == selectedDivId || children[i].style.display != "none") {
				divClass = "nav_quote_selected";
			} else {
		    	divClass = "nav_quote_hidden";
			}
			divId = "nav_quote_" + i;
			str += "<div class=\"" + divClass + "\" id=\"" + divId + "\">&nbsp;</div>";
		}
		$jq("#" + navId).html(str);
	    for (i=0; i<children.length; i++) {
			divId = "nav_quote_" + i;
			//$jq("div#" + divId).click(function(){selectQuote(parentId, navId, $(this));});
			$jq("div#" + divId).bind("click", {index: i}, function(event) {
				selectQuote(parentId, navId, event.data.index);
			});
			$jq("div#" + divId).bind("mouseover", {index: i}, function(event) {
				selectQuote(parentId, navId, event.data.index);
			});
			$jq("div#" + divId).bind("mouseout", {index: i}, function(event) {
				setQuotesTickerTimeout(parentId, navId, quoteSelectedTimeout);
			});
	    }
	}
}


function showQuote(parentId, navId, fromDivId, toDivId, accelerator){
	hideAllQuotes(parentId, fromDivId);
	$jq("#" + fromDivId).fadeOut((360 / accelerator), function() {
		hideAllQuotes(parentId, "");
		updateQuotesNavigation(parentId, navId, toDivId);
		$jq("#" + toDivId).fadeIn((360 / accelerator), function() {
			hideAllQuotes(parentId, toDivId);
		});
    });
}

function resetQuotesTickerTimeout(){
    clearTimeout(timer);
}

function quotesTickerExists(parentId, navId){
	var quotes_ticker_exists = true;
	if (!document.getElementById(parentId)) {
		quotes_ticker_exists = false;
	}
	if (!document.getElementById(navId)) {
		quotes_ticker_exists = false;
	}
	return quotes_ticker_exists;	
}

function setQuotesTickerTimeout(parentId, navId, timeout){
	resetQuotesTickerTimeout();
	if(quotesTickerExists(parentId, navId)) {
		timer = setTimeout(function(){selectNextQuote(parentId, navId);}, timeout); 
	}
}

function hideAllQuotes(parentId, divId){
	var children = getDivChildren(parentId);
    for (i=0; i<children.length; i++) {
		if (children[i].id != divId) {
			children[i].style.display = "none";
		}
    }
}

function selectQuote(parentId, navId, index){
	if(quotesTickerExists(parentId, navId)) {
		var children = getDivChildren(parentId);
		var selected = getFirstVisibleDivIndex(children);
		if (selected != index) {
			if (children[selected].style && children[index].style) {
				showQuote(parentId, navId, children[selected].id, children[index].id, 4);
				resetQuotesTickerTimeout();
			}
		}
	}
}

function selectNextQuote(parentId, navId){
	if(quotesTickerExists(parentId, navId)) {
		var children = getDivChildren(parentId);
		var selected = getFirstVisibleDivIndex(children);
		var select_next = ((selected+1)%children.length);
		if (children[selected].style && children[select_next].style) {
			showQuote(parentId, navId, children[selected].id, children[select_next].id, 1);
			setQuotesTickerTimeout(parentId, navId, quotesTickerTimeout);
		}
	}
}


