// for redirection to mobile version of this page
// later used only for qr-code redirection to jobs of 'design report' readers
var shouldRedirectMobile = true;
var redirectedToMobile = false;
var lastPage = 'home';

/****
 * When document is loaded, do
 */
$(document).ready(function(){
	// Enable "AJAX Crawlable" mode.
  $.param.fragment.ajaxCrawlable( true );
	
	// On first load, init page. On reload, get state of last page
	var page = $.bbq.getState('page');
	if (page == "") { // init
		$.bbq.pushState({page: "home"});
		$(window).trigger('haschange');	// Fire haschange event when the page first loads
	} else { // reloading
		loadContent(page, $.bbq.getState('id'));
		hightlightMenuItem(page); // TODO: not working, check binding to haschange 
	}
	
	bindMouseHandler();
	
}); //close $(document).ready


/****
 * Bind click functionality to menu items, header (home),
 * 	pictures of cases (case studies), videos.
 * Bind handler for history navigation (back/forward browser keys) 
 *	with BBQ and haschange event.
 * Bind handler for mouseover.
 */
function bindMouseHandler() {
	// Click handler for navigation (menu, header, cases, video)
	$('.crosslink').live('click', menuClick); // located in menus.js
	//$('li.pic_overview').live('click', menuClick /*tileClick*/); // located in case_studies.js
	$('.video_item').live('click', videoClick); // located in case_studies.js
	
	// Mouseover handling for cases in case studies
	$('li.pic_overview').live({
			mouseenter:
				function () {
					$(this).addClass('hover');
			},
			mouseleave:
				function () {
					$(this).removeClass('hover');
				}
			});	
	
	// Attach handler for history 
	// TODO: WHY RELOAD AND NOT LOAD?
	$(window).bind('hashchange', reloadContent);
}


/****
 * Load new content via ajax and delivercontent.php
 * @param page	content hold in php file
 * @param id		id of case or service catched from data base
 */
function loadContent(page, target_id, isReload) {	
	// hold last used page
	// TODO: UNABLE TO GET THE LAST PAGE, HASCHANGE WAS TRIGGERED BEFORE
	var lastPage = $.bbq.getState('page');
	
	// handling for submenu_service (convert text-id of submenu_item to numeric id for dbase query)
	// but only, when id isn't already set to a correct service id 
	if (page == "service" && isNaN(target_id)) {
		// in inc/menu.php#submenu_service the id tag holds a text id of the service page
		target_id = convertServicePageToId(target_id);
	}
	if (isNaN(target_id)) { target_id = 0;}
	
	// save page state into history
	if (!isReload) {
		$.bbq.pushState({page: page, id: target_id});
	}
	
	// REDIRECT MOBILE USERS TO JOBS	 redirectToOldPage
	if (shouldRedirectMobile) { redirectMobiles(page, target_id); }
	
	// GET CONTENT VIA AJAX
	$.ajax({
		method: "get", url: "delivercontent.php", data: "page="+page + "&" + "id="+target_id,
		beforeSend: function(){	 // hide content and footer to get same fade behaviour on history navigation
		fadeOutContent($("#ajaxcontent, #footer")); /*	 $('#ajaxcontent').hide();*/
		},
		complete: function() {
			
		},
		//so, if data is retrieved, store it in html
		success: function(html){ 
			//show the html inside content div
			$("#ajaxcontent").html(html); 
			$("#ajaxcontent").css('opacity', '0'); // BUG: something sets opacity to 1 bevore this line
			//load all images before showing content (doesn't work now, call back function needed)
			preLoadImages();	
			
			// fade in content
			fadeInContent($("#ajaxcontent, #footer"), fadeInSpeed);
			// fade in and out desired menus
			fadeInOutMenus(page); 
			hightlightMenuItem(page, target_id);
/*
			// animate appearance of tiles in services and case studies 
			if (page == "case_studies") {	
				if (isIE()) {	// lastPage == 'service' ||
					showTiles(); 	
				}
				else { animateTiles(); }
				
			}
			if (page == "services") {
				if (isIE()) {	// lastPage == 'service' ||
					showTiles(); 
				}	
				else { 
					animateTiles();
				}
			}
*/
			// set background to gray on case stuides service overview
			if (page == "case_studies" || page == "services") {	
				$('#html, #ajaxcontent, #header, #footer, .menu').addClass('background_gray' /*, fadeInSpeed*/);
				//$('html').animate( { backgroundColor: 'black' }, 250);
			}
			else {
				$('.background_gray').removeClass('background_gray' /*, fadeInSpeed*/);
				//$('html').animate( { backgroundColor: 'white' }, 250);				
			}
			
			// remove minus-cursor back navigation click handler, when leaving case or service via menus or browser back/forw.
			if (page != "case" && page != "service" && page != "video") {	
				undbindMouseNavMinus(page);
			} else {
				if (page == "service") { bindMouseNavMinusTo($('html'), "services", "0"); }
				if (page == "case") { bindMouseNavMinusTo($('html'), "case_studies", "0"); }
				if (page == "video") { bindMouseNavMinusTo($('html'), "case", "0"); }
			}
		}
	}); //close $.ajax(

}


/****
 * Reload last content via ajax. Get last page, id, ... from bbq-state (history/haschange)
 * @param page	content hold in php file
 * @param id		id of case or service catched from data base
 */
function reloadContent() {
	var page = $.bbq.getState('page');
	var id = $.bbq.getState('id');
	
	loadContent(page, id, true);
}


/****
 * Redirects mobile user to mobile version of job-page 
 * when he used the qr-code in job advertisement
 */
function redirectMobiles(page, id) {
	// to mobile jobs
	if (page == 'jobs' && id == null && $.browser.mobile) {
		window.location.replace("./mobile/jobs/index.html");
		redirectedToMobile = true;
	}
}

/****
 * Redirects all users to the old homepage
 */
function redirectToOldPage(page, id) {
	if (page == 'jobs') {
		window.location.replace("./sign/hid_studio/jobs.htm");
	}
	else {
		window.location.replace("./sign/index.html");
	}		
}
