/* 
	inlight.com.au - v2
	
	16/03/2009
	Tony Milne (Inlight Media)
*/

var PORTFOLIO_URL = "/portfolio?ajax=1&project=";

/** 
 * Used to log to the console.
 */
jQuery.fn.log = function (msg) 
{
	try
	{	
		if (console)	
			console.log("%s: %o", msg, this);
	}
	catch(e) {}
	
	return this;
}

$(document).ready(function() {			
	// apply_pngfix();
	init_login();
	init_portfolio();
	init_flash();	
	init_request_quote();
	set_twitter_message();
	set_delicious_link();
	set_digg_link();
	init_contact_form();
	setUpMapFancybox();
});

function apply_pngfix() {
	$("#page").pngFix();	
}

function project_load_callback()
{	
	// Sifr the heading for the new content.
	sIFR.replace(frutigerLinotype, {
	    selector: '#content h1',
	    css: [
	    	'.sIFR-root { color: #015bb4; font-size: 22px; }'		
	    ]
	});
	
	// Set the screenshotRotator to cycle through the images.
	$('#screenshotRotator ul').innerfade({
		speed: 500,
		timeout: 2500,
		type: 'sequence',
		containerheight: '320px'
	});
}

function init_login() {
	$("#clientLogin").click(function() {
		var login = $("#login");
		if (login.hasClass('closed')) {			
			// slide the panel up.
			login.animate({top: "0px"}, { 
				duration: 500, 
				complete: function() {
					login.removeClass('closed');
				}
			});
		}
		else {			
			// slide the panel down.
			login.animate({top: "38px"}, { 
				duration: 500, 
				complete: function() {
					login.addClass('closed');
				}
			});			
		}
		return false;
	});
	$("#login").css("top", "38px");
}

function init_portfolio() {
	var TOTAL_ITEM_COUNT = $("#portfolio ul li").length;
	var ITEMS_VISIBLE = 3;
	
	var WIDTH = 290; var MARGIN = 50;
	var MIN_LEFT = (TOTAL_ITEM_COUNT - ITEMS_VISIBLE) * -WIDTH;
	var MAX_LEFT = 0;
		
	$("#viewport ul").width(WIDTH * TOTAL_ITEM_COUNT - MARGIN);
	
	$("#feature #portfolio a.previous").click(function() {
		var ul = $("#viewport ul");
		var leftValue = parseInt(ul.css('left').replace('px', ''));
		
		if (!_scrolling && leftValue < MAX_LEFT) {
			leftValue += WIDTH;			
			__scrollPortfolio(ul, leftValue);
		}
		return false;
	});
	
	$("#feature #portfolio a.next").click(function() {
		var ul = $("#viewport ul");
		var leftValue = parseInt(ul.css('left').replace('px', ''));
		
		if (!_scrolling && leftValue > MIN_LEFT) {
			leftValue -= WIDTH;			
			__scrollPortfolio(ul, leftValue);
		}
		return false;
	});	
	
	var links = $("#feature #portfolio li a");
	links.click(function() {		
		var i = links.index(this);
		$("#content").load(PORTFOLIO_URL+i, null, project_load_callback);
		
		return false;
	});
}

function init_flash() {
	$('body.home #feature').flash({
	    src: 'flash/home_feature.swf',
	    width: 960,
	    height: 260,
	    wmode: 'transparent'
	});		
}

function init_request_quote()
{			
	$("a#requestQuote").fancybox({
		 zoomSpeedIn: 500,
		 zoomSpeedOut: 500,
		 overlayShow: true,
		 overlayOpacity: .75,
		 frameWidth: 630,
		 frameHeight: 540,
 		 hideOnContentClick: false
	}); 
}

function set_twitter_message() {
    var pageTitle = $("title").html();
    var shortPageTitle = pageTitle.substring(0,pageTitle.indexOf('|'));
    shortPageTitle = jQuery.trim(shortPageTitle);
    if(shortPageTitle == "") {
        $('li.twitter a').attr('href', "http://twitter.com/home?status=@inlightmedia Viewing Inlight Media's website (http://inlight.com.au)");
    } else {
        if(pageTitle.match('| Blog |')) {
            if(location.href.match('/blog/author/')) {
                $('li.twitter a').attr('href', "http://twitter.com/home?status=@inlightmedia Viewing posts written by "+shortPageTitle+" at Inlight Media ("+location.href+")");
            } else {
                $('li.twitter a').attr('href', "http://twitter.com/home?status=@inlightmedia Reading '"+shortPageTitle+"' at Inlight Media ("+location.href+")");      
            }            
        } else {
            $('li.twitter a').attr('href', "http://twitter.com/home?status=@inlightmedia Viewing '"+shortPageTitle+"' page at Inlight Media ("+location.href+")");
        }        
    }
}

function set_delicious_link() {
    $('li.delicious a').attr('href', "http://del.icio.us/post?url="+location.href);
}

function set_digg_link() {
    $('li.digg a').attr('href', "http://digg.com/submit?phase=2&url="+location.href);
}

function init_contact_form() {
	// Set _isAjaxRequest variable to avoid sending the page contents back after form submission.
	$("#ajax").val("true");	
	$("#thecontactform").validate({						
		submitHandler: function(form) {						
			$("#thecontactform").ajaxSubmit(function(result) {								
				if (result == "true")
				{
					// Remove the form components.
					$("#contactform").log("fading out thecontactform").fadeOut(500, function() {	
						// Display the thankyou message.
						$("#thankyouMessage").fadeIn(500);											
					});	
				}
				else
				{
					alert("There was an error sending your message. \r\n Perhaps you could try giving us a call on 03 9602 4747 or try again later.");
				}				
			});
		},		
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 mandatory field (it has been highlighted).'
					: 'You missed ' + errors + ' mandatory fields (they have been highlighted).';
				
				$("#thecontactform div.error span").html(message);
				$("#thecontactform div.error").show();
			} else {
				$("#thecontactform div.error").hide();
			}
		},
		rules: {
			name: "required",			
			email: {
				required: true,
				email: true
			},
			comment: "required"
		},
		messages: {
			name: "Please enter your name.",			
			email: {
				required: "Please enter your email.",
				email: "Please enter a valid email."
			},
			comment: "Please enter your comment."
		}
	});
}

var _scrolling = false;
function __scrollPortfolio(ul, leftValue) {
	_scrolling = true;
	ul.animate({left: leftValue}, {
		duration: 500,
		easing: "easeOutCubic",
		complete: function() {
			_scrolling = false;
		}
	});	
}

if (window.addEventListener) {
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
    window.addEventListener("keydown", function(e) {
    	kkeys.push(e.keyCode);
		if (kkeys.toString().indexOf(konami) >= 0) {        	
			alert(":)");
        	kkeys = [];
    	}
	}, true);
}

function setUpMapFancybox()
{		
	var map_url = $("a#map").attr("href");
	map_url += "?iframe=true";
	$("a#map").attr("href", map_url);
	
	$("a#map").fancybox({
		 zoomSpeedIn: 500,
		 zoomSpeedOut: 500,
		 overlayShow: true,
		 overlayOpacity: .75,
		 frameWidth: 640,
		 frameHeight: 480,
		 hideOnContentClick: false
	}); 
}