/**
 * tools to enable microsites to do their thing
 *
 */

function switchContent(newContent) {
	$(".switchable").hide();
	$("#fake_h3").empty().html($('#'+newContent+' h3').html());
	$('#'+newContent).fadeIn("normal");
	updateScroller();
	pageTracker._trackPageview(newContent);
	return false;
}

// DOM ready stuff
$(document).ready(function(){
	$('<img src="images/btn_enlarge.gif" alt="enlarge" width="102" height="16" />').appendTo(".jCarouselLite >ul >li >a");
	$('<h3 id="fake_h3">&nbsp;</h3>').prependTo("#upper");
	$(".main .jCarouselLite").jCarouselLite({
		visible: 4,
		btnNext: ".main .next",
		btnPrev: ".main .prev"
	});
	$("#frmPrints").ajaxSubmit();
	$(".switchable:not(#introduction)").hide();
	$(".switchable h3").hide();
	updateScroller();
	$('.jCarouselLite >ul >li >a').click(enlarge_sample);
});

$.fn.ajaxSubmit = function(e) {
	/* Change a form's submission type to ajax */
	this.submit(function(){
		var params = {};
	$(this)
	.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
	.filter(":enabled")
	.each(function() {
	  params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
	});
	$("body").addClass("curWait");
		$.post(this.getAttribute("action") + "?call=ajax", params, function(xml){
			$("body").removeClass("curWait");
			strError = "Unable to submit form. Please try again later.";
			oFocus = null;
			$("AjaxResponse", xml).each(function() {
				strError = this.getAttribute("response");
			});
			if (strError == 'SUCCESS!') {
				// hide the form
				$("#frmPrints").hide();
				$('<h5>Thankyou for your request, you should receive your print shortly.</h5>').appendTo('#sample_print');
				pageTracker._trackPageview('formSubmission');
			} else {
				// display error
				$("div.formErrors").html(strError).filter(":hidden").fadeIn("normal");
				$('.scroll-pane')[0].scrollTo(0)
			}
			updateScroller();
		});
		return false;
	});
	return this;
}

// preload gallery previews
$(window).bind('load', function() {
    var preload = new Array();
    $('.jCarouselLite >ul >li >a').each(function() {
        // append _p to image name then push on preload queue
		preload.push($(this).attr('href').replace(/\.([a-z]+)$/i, "_p.$1"))
    });
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
});

// carousel images trigger simpleModal window
function enlarge_sample() {
	// set src of img to href with _p appended
	href = $(this).attr('href');
	$('#image_preview').attr({'src':href.replace(/\.([a-z]+)$/i, "_p.$1")});
	// set download button href
	$('#image_url').attr({'href':href, 'onclick':'pageTracker._trackEvent("sample","download","'+href.slice(href.lastIndexOf('/'))+'");'});
	// launch modal overlay
	$('#basicModalContent').modal({overlay: (65)}); 
	pageTracker._trackEvent('sample','preview',href.slice(href.lastIndexOf('/')));
	return false;
}

function updateScroller() {
	$('.scroll-pane').jScrollPane({scrollbarWidth:12,maintainPosition:false});
}