/**
 * JAVASCRIPT FOR LEARNING SCIENCE LTD - http://www.learnsci.co.uk
 */

var storeHistory = true;
var curTab = 'submenu_1';
var curTabNo = 1;
var csVisible = false;
var firstTime = true;

/**
 * Set things up once the page is ready
 */
$(document).ready(function()
{
	// Close case study window on outside click
    $('.case_studies_holder').hover(function() {
        mouse_is_inside = true;
    }, function() {
        mouse_is_inside = false;
    });

    $("body").mouseup(function(){
        if (!mouse_is_inside) csClose();
    });

    // Ajax history monitoring
    dhtmlHistory.initialize();
    dhtmlHistory.addListener(rshListener);
    rshListener(dhtmlHistory.getCurrentLocation());
    firstTime = false;
    
    // Fading gallery banner, if used 
    if ($('#gallery img.active').length > 0) setInterval("gallerySwitch()", 7000);
    
    // Precache images if needed (PHP-generated function)
    if (window.precache) precache();
});


//--- ALL PAGES ---------------------------

/**
 * Switch to another tab
 */
function switchTab(tab) {
	var id;
	var menuTab = '#' + tab + ' a';
	if (storeHistory) dhtmlHistory.add('h' + tab, '');
	
	// Only switch tab once any open case study has been closed
	var delay;
	if (curCs > 0) {
		csClose();
		delay = 310;
	} else {
		delay = 0;
	}
	
	setTimeout(function() {
		for (var i = 1; i <= 4; i++) {
			id = '#submenu_' + i + ' a';
			if ($(id).hasClass('submenu_selected') && menuTab != id) {
				$(id).removeClass('submenu_selected');
			} else if (menuTab == id) {
				$(id).addClass('submenu_selected');
			}
		}
		
		$('#' + curTab + '_content').hide();
		$('#' + tab + '_content').show();
		curTab = tab;
		curTabNo = tab.substring(8);
	}, delay);
	
	return delay;
}


/**
 * Switch to the next gallery image
 */
function gallerySwitch() {
    var active = $('#gallery img.active');

    if (active.length == 0) active = $('#gallery_last');

    var next = active.next().length ? active.next() : $('#gallery_first');

    active.addClass('last-active');

    next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            active.removeClass('active last-active');
        });
}


/**
 * Ajax history monitoring - rsh
 */
window.dhtmlHistory.create({
	toJSON: function(o) {
		return JSON.stringify(o); 
	}, fromJSON: function(s) {
		return JSON.parse(s); 
	} 
});


/**
 * Called when user navigates to an Ajax-based URL
 *   
 * @param newLocation
 * @param historyData
 */
var rshListener = function(newLocation, historyData) {
    if (firstTime && newLocation == '') {
    	firstTime = false;
    	return;
    }
	storeHistory = false;
	if (newLocation == '' || newLocation == 'h') newLocation = 'hsubmenu_1';
	newLocation = newLocation.substring(1);
	var usc = newLocation.indexOf('-');
	
	if (usc != -1) {
		var newTab = newLocation.substring(0, usc);
		var delay = 0;
		if (curTab != newTab) delay = switchTab(newTab) + 10;
		setTimeout(function() { showCaseStudy(newLocation.substring(usc+1)); }, delay);
	} else {
		switchTab(newLocation);
	}
	storeHistory = true;
};


/**
 * Image pre-caching
 * Call as imagesQ.queue_images(['http://.../img1.jpg', 'http://.../img2.jpg', ...]);
 */
imagesQ = {onComplete:function(){},onLoaded:function(){},current:null,qLength:0,images:
	[],inProcess:false,queue:[],queue_images:function(){var arg=arguments;for(var i=0;i<arg.length;i++){if(arg
	[i].constructor===Array){this.queue=this.queue.concat(arg[i]);}else if(typeof arg[i]==='string')
	{this.queue.push(arg[i]);}}},process_queue:function(){this.inProcess=true;this.qLength+=this.queue.length;while
	(this.queue.length>0){this.load_image(this.queue.shift());}this.inProcess=false;},load_image:function(imageSrc)
	{var th=this;var im=new Image;im.onload=function(){th.current=im;th.images.push(im);(th.onLoaded)();if
	(th.queue.length>0&&!th.inProcess){th.process_queue();}if(th.qLength==th.images.length){(th.onComplete)
	();}};im.src=imageSrc;}};


// --- CASE STUDIES ---------------------------

// Case study display and tracking
var curCs = -1;
var curCsPg = 1;
var mouse_is_inside = false;
var numPages = new Array();
numPages[1] = 4;
numPages[2] = 4;
numPages[3] = 4;
numPages[4] = 4;
numPages[5] = 3;

// Case study window positioning
var csOffsetY = new Array();
csOffsetY[1] = -298;
csOffsetY[2] = -60;
csOffsetY[3] = 0;
csOffsetY[4] = 0;


/**
 * Show the case study display
 */
function showCaseStudy(cs) {
	csGoToCs(cs);
	$('#case_study_more').hide();
	if (storeHistory) dhtmlHistory.add('h' + curTab + '-' + cs);
	
	if ($(window).height() - 475 < 356) {
		$.scrollTo('258px', 1500, {axis: 'y'});		
	}

	$("#case_study").css('marginTop', (475 + csOffsetY[curTabNo]) + 'px');
	var moveTo = csOffsetY[curTabNo] + 'px';
	
    $("#case_study").stop().animate({
        height: "475px",
        marginTop: moveTo
    }, {
        queue: false,
        duration: 300,
        easing: 'easeInOutQuad',
        complete: function(){
    	    $('#case_study_more').show();
        }
    });
}


/**
 * Navigate to the previous page within the current case study
 */
function csPrevPage() {
	curCsPg = curCsPg - 1;
	if (curCsPg < 1) {
		curCsPg = 1;
	} else {
		updateCsDisplay();
	}
}


/**
 * Navigate to the next page within the current case study
 */
function csNextPage() {
	curCsPg = curCsPg + 1;
	if (curCsPg > numPages[curCs]) {
		curCsPg = numPages[curCs];
	} else {
		updateCsDisplay();
	}
}


/**
 * Navigate to a specific page
 */
function csGoToPage(pg) {
	curCsPg = pg;
	if (curCsPg > numPages[curCs]) {
		curCsPg = numPages[curCs];
	} else {
		updateCsDisplay();
	}
}


/**
 * Close the case study display
 */
function csClose() {
	$('#case_study_more').hide();
		
	$("#case_study").css('marginTop', csOffsetY[curTabNo] + 'px');
	var moveTo = (475 + csOffsetY[curTabNo]) + 'px';
	
	$('#case_study').stop().animate({
        height: "0px",
        marginTop: moveTo
    }, {
        queue: false,
        duration: 300,
        easing: 'easeInOutQuad'
    });
}


/**
 * Navigate to the previous case study
 */
function csPrevCs() {
	curCs = curCs - 1;
	if (curCs < 1) {
		curCs = 1;
	} else {
		curCsPg = 1;
		updateCsDisplay();
	}
}


/**
 * Navigate to the next case study
 */
function csNextCs() {
	curCs = curCs + 1;
	if (curCs > 5) {
		curCs = 5;
	} else {
		curCsPg = 1;
		updateCsDisplay();
	}
}


/**
 * Navigate to a specific case study
 */
function csGoToCs(cs) {
	curCs = cs;
	if (curCs > 5) {
		curCs = 5;
	} else {
		curCsPg = 1;
		updateCsDisplay();
	}
}


/**
 * Update divs shown for the current case study display
 */
function updateCsDisplay() {
	for (var cs = 1; cs <= 5; cs++) {
		for (var pg = 1; pg <= numPages[cs]; pg++) {
			if (curCs == cs) {
				if (curCsPg == pg) {
					$('#cs' + cs + '_text' + pg).show();
					$('#cs' + cs + '_image' + pg).show();
					$('#cs' + cs + '_thumbnail' + pg).addClass('cs_active');
				} else {
					$('#cs' + cs + '_text' + pg).hide();
					$('#cs' + cs + '_image' + pg).hide();
					$('#cs' + cs + '_thumbnail' + pg).removeClass('cs_active');
				}
			} else {
				$('#cs' + cs + '_text' + pg).hide();
				$('#cs' + cs + '_image' + pg).hide();
				$('#cs' + cs + '_thumbnail' + pg).removeClass('cs_active');
			}
		}
		if (curCs == cs) {
			if (curCsPg == 1) {
				$('#case_study_prev').hide();
			} else {
				$('#case_study_prev').show();
			}
			if (curCsPg < numPages[curCs]) {
				$('#case_study_next').show();
			} else {
				$('#case_study_next').hide();
			}
			$('#cs' + cs).show();
		} else {
			$('#cs' + cs).hide();
		}
	}
}

