/* RANDOM SUCCESS STORY + SUCCESS-STORY PAGEBROWSER + TOPNEWS PAGEBROWSER */

$(document).ready(function() {

	// Display a random success story
	randomSuccessStory();
	
	
	// Random Tab is called from the mainslider template, as tab-selection takes place there
	
	/* ### SUCCESS STORIES PAGEBROWSER ### */
	
	// Assign IDs to the ss-containers (used in the pagebrowser)
	id = 0;
	$("#n_col2 .n_col2_content").each(function() {
		$(this).attr("id", "ss_"+id);
		id++;
	});
	
	
	/* ### TOPNEWS PAGEBROWSER ### */
	
	// Assign IDs to the news item-containers (used in the pagebrowser)
	id = 0;
	$("#n_col1 .n_col1_content").each(function() {
		$(this).attr("id", "ni_"+id);
		id++;
	});
	
	// Hide all news elements
	$(".n_col1_content").hide();
	// Show the first news element
	$(".n_col1_content:first").show();
});


/* ### TOPNEWS PAGEBROWSER FUNCTIONS ### */

function previousNewsItem() {
	// If current news item ist the first, show the last, otherwise show the previous
	if(currentNI() == 0) activateNewsItem($(".n_col1_content").length-1);
	else activateNewsItem(parseInt(currentNI()-1));
}

function nextNewsItem() {
	// If current news item is the last, show the first, otherwise show the next
	if(currentNI() < $(".n_col1_content").length-1) activateNewsItem(parseInt(currentNI()+1));
	else activateNewsItem(0);
	
}

function activateNewsItem(newsId) {
	// Hides all news items
	$(".n_col1_content").hide();
	
	// Shows specified news item
	$("#n_col1 .n_col1_content:eq("+newsId+")").show();	
}


// Returns the id of the news item displayed
function currentNI() {
	var currentId = "";
	currentId = String($(".n_col1_content:visible").attr("id"));
	activeNI = currentId.substr(currentId.lastIndexOf("ni_")+3);
	return parseInt(activeNI);
}

/* SUCCESS STORY PAGEBROWSER */

// Returns the id of the success story displayed
function currentSS() {
	var currentId = "";
	currentId = String($(".n_col2_content:visible").attr("id"));
	activeSS = currentId.substr(currentId.lastIndexOf("ss_")+3);
	return parseInt(activeSS);
}


function nextSuccessStory() {
	// If current success story is the last, show the first, otherwise show the next
	if(currentSS() < $(".n_col2_content").length-1) activateSuccessStory(parseInt(currentSS()+1));
	else activateSuccessStory(0);
}

function previousSuccessStory() {
	// If current success story ist the first, show the last, otherwise show the previous
	if(currentSS() == 0) activateSuccessStory($(".n_col2_content").length-1);
	else activateSuccessStory(parseInt(currentSS()-1));
}

function activateSuccessStory(storyId) {
	// Hides all success stories
	$(".n_col2_content").hide();
	
	// Shows specified success story
	$("#n_col2 .n_col2_content:eq("+storyId+")").show();
	
	// Retrievs the background-image specified for each success story
	backgroundImg = $("#n_col2 .n_col2_content:eq("+storyId+") input[type=hidden]").attr("value");
	
	// Display the background-image
	$("#topnews").css("background-image", "url("+backgroundImg+")");
}

/* ### RANDOM SUCCESS STORY ### */
function randomSuccessStory() {
	// Get number of success stories
	availableStories = $("#n_col2 .n_col2_content").length;
	
	// Selects a random success story
	showStory = parseInt(getRandom(0, availableStories-1));
	
	// Displays a random success story
	activateSuccessStory(showStory);
}

/* ### RANDOM SLIDER TABS ### */

function randomTab() {
	// Get the location
	loc = String(document.location);
	
	// Get active tab id
	idxOfTab = loc.lastIndexOf("#tab_");		//For mainslider
	idxOfTab2 = loc.lastIndexOf("/tab/");	//If you came back from the detailslider
	
	// If tabid was found set tab active
	// Sets tab active, if you are on the homepage
	if(idxOfTab != -1) {
		activeTabId = loc.substr(idxOfTab+5);
		activateTab(activeTabId);
	}
	else
	{
		// Sets tab active, if you came back to the homepage from the detailslider
		if(idxOfTab2 != -1) {
			//activeTabId = loc.substr(idxOfTab2+8); 
			activteTabId = loc.substring(idxOfTab2+5, loc.lastIndexOf("/display"));
			activateTab(activeTabId);
		}
		// Otherwise activate random tab
		else {
			tabs = new Array();
			
			// Write all ID to an array
			$("div.tabs_wrap ul li a").each(function() {
				tabs.push($(this).attr("id"));
			});
			
			// Get a random key from the array
			selectedIdx = getRandom(0, tabs.length-2); // -2 because the last tab should never be display randomly
	
			// Select the content from the random key
			selectedTab = tabs[selectedIdx];
			tabId = selectedTab.substr(selectedTab.lastIndexOf("_")+1);
			
			// Activate random tab
			activateTab(tabId);
		}
	}
}

function activateTab(tabId) {
	tabId = parseInt(tabId);
	// Hide all
	$("div.tabs_wrap ul li").removeClass("selected");
	$("div[id^=slidertab_]").css("display", "none");
	
	// Show selected
	$("#tab_switch_"+tabId).parent("li").addClass("selected");
	$("#slidertab_"+tabId).css("display", "block");	
	$("#slidertab_"+tabId+" div[id^=slides_]").css("width", "84.18em").css("height", "155px").show();
	
}


/* ### RANDOM FUNCTION ### */

function getRandom(min, max) {
	if( min > max ) {
		return( -1 );
	}

	if( min == max ) {
		return( min );
	}
	
	return(min + parseInt(Math.random()*(max-min+1)));
}

