/*  =======================
    Layout switcher, switches classes at different sizes
    ======================= */

function setLayoutSwitcher(switchArray) {

	var switchers = new Array();

	var Switcher = function(switchParams) {
		var jqTarget = $(switchParams.selector);
		var ranges = switchParams.ranges;
		var lastClass = "";
		var lastRangeApplied = null;
		
		var checkThreshold = function(point) {
			var rangeToApply = null;

			for (var i = 0; i < ranges.length; i++) {
				var thBegin = 0;
				var thEnd = 9999999999;

				if (i == 0 && ranges[i].th != 0) {
					thBegin = ranges[i].th;
				} else if (i+1 < ranges.length) {
					thBegin = ranges[i].th;
					thEnd = ranges[i+1].th;
				} else {
					thBegin = ranges[i].th;
				}

				if (point >= thBegin && point < thEnd) {
					rangeToApply = i;
				}
			}

			if (rangeToApply != lastRangeApplied) {
				if (rangeToApply == null) {
					applyClass("");
				} else {
					if (ranges[rangeToApply].className) {
						applyClass(ranges[rangeToApply].className)
					} else {
						applyClass("")
					}
				}
				
				lastRangeApplied = rangeToApply;
			}
		}

		var applyClass = function(className) {
			// remove all classes
			var len = ranges.length;
			for(var i = 0; i < len; i++) {
				jqTarget.removeClass(ranges[i].className)
			}

			if (className != "") {
				jqTarget.addClass(className);
			}
		}

		return({
			checkThreshold:checkThreshold
		});
	}

	for (var i = 0; i < switchArray.length; i++) {
		switchers.push(new Switcher(switchArray[i]));
	}




	$(window).bind('resize', function() { checkWidth() } );

	checkWidth()

	function checkWidth(){

		var bodyWidth = $("body").width();

		for (var i = 0; i < switchers.length; i++) {
			//console.log(bodyWidth)
			switchers[i].checkThreshold(bodyWidth);
		}

	}
}



/*  =======================
    INITIALISE Scripts
    ======================= */

$(function(){
	
    setLayoutSwitcher([{
		selector:".homepage #photo", ranges:new Array(
			{th:0, className:"grid-colstart4-colend11"},
			{th:750, className:"grid-colstart4-colend8-floatleft-cols"}
    )},{
    	selector:".homepage #content", ranges:new Array(
    		{th:0, className:"grid-colstart4-colend11"},
    		{th:750, className:"grid-colstart9-colend11-floatright-cols"}
    )}]);

});