/*

	JS to handle menu clicks on web page SilverBox.
	Main purpose was to use Ajax to everything in the page, including menu system.
	A _lot_ of this code proved not to be flexible/expandible.

	And a lot of other stuff related to left hand side of page.
	Author: Olle Härstedt, 2010-08-13

	BUG: When reload with ?mmnr=x, current_mainmenu_nr is not set and causes submenuclick to fail.

*/
var current_mainmenu_nr;
var lastpic;
var chosenpic = 0;
var productwindow;
var productslideshow; 

$(document).ready(function() 
{
	var rightcol = $("#rightColumn");
	var leftcol = $("#leftColumn");
	//mytext = $("#text");
	var star = $("#star");
	//rotate();

	//productwindow = new ProductWindow();
	//productslideshow = new ProductSlideshow();

	//Galleria.loadTheme('galleria.classic.js');
	//$("#images").hide();
	//$("#thumbnailwrapper").hide();

	// Click flag/change lang
	$(".flag").click(function()
		{
			val = $(this).attr("alt");
			$.post(
				"/ajaxactionmodule.php",
				{action: "lang", lang: val},
				function(data) {
					// Nothing here
					location.reload();
				}
			);
		}
	);

	$("#nextthumbnails").click(function() {
		$("#gallerythumbnails").animate({left: -500}, {duration: 500});
	});
	$("#prevthumbnails").click(function() {
		$("#gallerythumbnails").animate({left: 0}, {duration: 500});
	});

	$("#feedback").click(function() {
		window.open('/feedback/view','Feedback','width=450, height=490, toolbar=no, location=no, directories=no')
	});

	//$("#leftColumn img").live('hover',getProdInfo, function() {});

	//productslideshow.timer();
});

// Function used to use AJAX with menu if JS is turned on
// PRE: 	nr, the number of the menu item, starting on 0
// POST:	null
// SIDEEFFECT: 
function mainmenuClick(nr, event) {
	
	return;
	// Used return false in html instead, 'onclick="bla(); return false;"'
	// Tried above stuff, gone back to preventDefault. Work in IE?
	event.preventDefault();
	
	// This is for the submenu routin.
	current_mainmenu_nr = nr;
	var url = "";

	// Hide images of not in products
	if (nr != 1) {
		//$("#images").hide();
		//productslideshow.show();
	}

	if (nr==0) {	// Main page
		submenuClick("0");		// Use submenu routin to display text
	}	
	// Show gallery 
	else if (nr == 1) {
            
		//productslideshow.hide();
		//productwindow.show();
		getTitel(1,0);

		$.get('commandresolver.php?command=menuproducts', 
			function(data) {
				$("#text").html(data); 
			}
		);
    }

	getSubmenu(current_mainmenu_nr);
	
	if (nr > 1) {	// fetch text from first submenu alternative
					// (TODO: startpage for every main menu alt?)
		submenuClick(nr);
	}

}

function submenuClick(nr) {

	return true;
	if (current_mainmenu_nr != 1) { // not products view
		getResource("commandresolver.php?command=gettextandimgs&mmnr=" + current_mainmenu_nr + "&smnr=" + nr);
	}
	else {	// fetch products
		getResource("commandresolver.php?command=getproductsbydir&group=" + nr);
	}

	return false;
}


function getText(mm, sm) {	// mm, sm = main menu, sub menu
	$.getJSON("ajaxactionmodule.php?action=gettext&mmnr=" + mm + "&smnr=" + sm, 
    	function(data) {
			$("#h1").html(data.header);
			$("#text").html(data.content);
        }
	);
}

function getTitel(mm, sm) {
	$.getJSON("ajaxactionmodule.php?action=gettext&mmnr=" + mm + "&smnr=" + sm, 
    	function(data) {
			$("#h1").html(data.header);
        }
	);
}

// getResource(url)
//
function getResource(url) {
	//$.get(url, function(data) { alert(data); });
	$.getJSON(url, function(data) {
		eval(data.javascript);
	});
}

function getSubmenu(mm) {	// mm = main menu
	

	getResource("commandresolver.php?command=getsubmenu&mmnr=" + current_mainmenu_nr);

	/*
	$('#submenu').hide(); 	// Do some animation.
	
	$.get("submenu.php?mmnr=" + mm,
		function(data) {
			document.getElementById("submenu").innerHTML = data;
		}
	);

	$('#submenu').fadeIn();
	*/
}

function getProdInfo(id) {
	$.getJSON("ajaxactionmodule.php?action=productinfo&id="+id, 
		function(data) {
			//rightcol.innerHTML = "<img src='products/" + data.img + "' />";
			//$("#rightColumn").html("<img src='products/" + data.img + "' />");
			//rightcol.html("<p>Pris: " + data.price + "</p>");
		}
	);
}

function responseAjax() {
	if(request.readyState == 4 && request.status == 200){
		document.getElementById("submenu").innerHTML = request.responseText;
		// Fetch first text from the submenu
		submenuClick("sm0");
	}
}

function productsResponseAjax() {
	if(request.readyState == 4 && request.status == 200) {
		document.getElementById("images").innerHTML = request.responseText;
		$('#images').galleria();
		submenuClick("sm0");	// Fetch text
        }
}

// THUMBNAIL GALLERY STUFF

function showPic (whichpic) {
	if (document.getElementById) {
		document.getElementById('placeholder').src = whichpic.src;
		$(lastpic).css('border','none');
		$(whichpic).css("border","solid pink 1px");
		lastpic = whichpic;
	} 
	else {
		//return true;
	}
}



