// CONTENTS ====================================================================
/*
- DEFAULT 							- common code that runs on every page.
- UTILS 							- util functions
*/

// VARS --------------------------------------------------------------
var arr_proj 		= [];
var int_itemWidth	= 0
var int_current 	= 0;
var int_xpos 		= 0;
var int_offLeft 	= 0;
var int_offRight 	= 0;

var int_currPhoto	= -1;
var int_currZ		= 10;
var photoInterval;
var str_textareaMessage;

// DEFAULT --------------------------------------------------------------
function InitDefault(){
	
	$('#no-script').hide();
	
	str_textareaMessage = $('#Message').attr('value');
	
	// call other Init functions
	SetExternalLinks();
	InitProjects();
	InitPhotos();
	$('textarea').defaultValueInput();
	$('.btn-submit').click(function(){
		if( $('#Message').attr('value') == '' || $('#Message').attr('value') == str_textareaMessage ){
			$('#form-error').fadeIn();
			return false;
		}else{
			$('#form-error').fadeOut();
		}
	});
}

// Project Rotator --------------------------------------------------------------
function InitProjects(){
	$("#projects").addClass("active");
	
	int_itemWidth	= $("#projects li:first").width()
	int_xpos 		= ($("#projects").width() - 860) / 2;
	int_offLeft 	= ( 0 - int_itemWidth );
	int_offRight 	= ( $("#projects").width() + int_itemWidth );
	
	var indHtml = "<a href='' class='proj-ind three'></a>";
	
	// get all items
	$("#projects li").each( function(){ 
		arr_proj.push($(this)) 
		$(this).append(indHtml)
	} );
	
	// init position
	$("#projects li").css("left", int_offRight);
	$("#projects li .img img").css("left", -540);
	$("#projects h4").css("left", 270);
	PositionItems();
	
	// set controls
	$("#next").data("posLeft", ($("body").width() / 2) + 425).css("left", $("#next").data("posLeft"));
	$("#prev").data("posLeft", ($("body").width() / 2) - 475).css("left", $("#prev").data("posLeft"));
	
	
	$("#next, #prev").hover(Roll, Out)
	$("#next").click(function(){
		if(int_current < (arr_proj.length - 3) ){
			int_current++;
			PositionItems();
		}
		return false;
	})
	$("#prev").click(function(){
		if(int_current > 0){
			int_current--;
			PositionItems();
		}
		return false;
	})
	
	// add rotate behaviour to the images themselves
	$("#projects li .img img").click(function(){
		var int_gotoLeft = parseInt( $(this).css('left') ) - 270;
		if(int_gotoLeft <= -270 && int_gotoLeft > -540) int_gotoLeft = -270;
		if(int_gotoLeft < -540) int_gotoLeft = 0;
		// set class on inds
		var theLi = $(this).parent().parent();
		$(theLi).find(".proj-ind").removeClass("one").removeClass("two").removeClass("three");
		switch(int_gotoLeft){
			case 0:
				// 1
				$(theLi).find(".proj-ind").addClass("one");
				break;
			case -270:
				// 2
				$(theLi).find(".proj-ind").addClass("two");
				break;
			case -540:
				// 3
				$(theLi).find(".proj-ind").addClass("three");
				break;
		}
		$(this).stop().animate({"left":int_gotoLeft},1000, "easeInOutExpo");
	})
	
	$("#projects li .proj-ind").click(function(){
		$(this).parent().find("img").trigger("click");
		return false;
	});
	
	// wait then fade in controls
	$(".control").delay(1000).fadeIn("slow");
	
}

function Roll(){
	if( !$(this).hasClass("disabled") ){
		var amount = 25;
		if( $(this).attr("id") == "prev" ) amount = -25;
		$(this).stop().animate({"left":$(this).data("posLeft") + amount},800, "easeOutExpo")
	}
}

function Out() {
	$(this).stop().animate({"left":$(this).data("posLeft")},800, "easeOutExpo")
}

function PositionItems(){
	for (var i=0; i < arr_proj.length; i++) {
		
		// kill hangover anims
		arr_proj[i].stop();
		arr_proj[i].find("h4").stop();
		arr_proj[i].find("img:first").stop();
		
		// rest ind classes
		arr_proj[i].find(".proj-ind").removeClass("one");
		arr_proj[i].find(".proj-ind").removeClass("two");
		arr_proj[i].find(".proj-ind").removeClass("three");
		
		// init vars
		var int_endx;
		var str_ease		= "easeInOutExpo";
		var int_alpha 		= 0.5;
		var int_imgLeft		= 0;
		var str_txtAlign	= "left";
		var int_txtLeft		= 0;
		var int_indLeft		= 0;
		var adjusted		= 0;
		
		// set vars for each case
		if( i < int_current ){
			int_endx 		= int_offLeft;
			int_imgLeft		= 0;
		}else if( i > (int_current + 2) ) {
			int_endx 		= int_offRight;
			int_imgLeft 	= -540;
			int_txtLeft		= 270;
			int_indLeft		= 270;
			str_txtAlign	= "right"
		}else{
			adjusted 		= i - int_current;
			int_endx 		= int_xpos + ((int_itemWidth + 25) * adjusted);
			int_alpha 		= 1;
			int_imgLeft		= adjusted * -270;
			switch(adjusted){
				case 0:
					str_txtAlign 	= "left";
					// int_txtLeft		= -25;
					int_txtLeft		= 0;
					int_indLeft		= 0;
					arr_proj[i].find(".proj-ind").addClass("one");
					break;
				case 1:
					str_txtAlign 	= "center";
					int_txtLeft 	= ( 270 - arr_proj[i].find("h4").width() ) / 2;
					int_indLeft		= 113;
					arr_proj[i].find(".proj-ind").addClass("two");
					break;
				case 2:
					str_txtAlign 	= "right";
					// int_txtLeft		= ( 270 - arr_proj[i].find("h4").width() ) + 25;
					int_txtLeft		= ( 270 - arr_proj[i].find("h4").width() );
					int_indLeft		= 231;
					arr_proj[i].find(".proj-ind").addClass("three");
					break;
			}
		}
		
		// Adjust text position for IE's 6 & 7 (overflow requirements mean no fancy text overhang in those browsers)
		if ( $.browser.msie && Math.floor($.browser.version) ) {
  			switch(adjusted){
				case 0:
					int_txtLeft		= int_txtLeft + 25;
					break;
				case 2:
					int_txtLeft		= int_txtLeft - 25;
					break;
			}
		}
		
		arr_proj[i]
		.delay(adjusted * 100)
		.animate({"left":int_endx},800, str_ease)
		.find("img:first")
		.delay(adjusted * 100)
		.animate({"left":int_imgLeft},1000, str_ease);
		
		arr_proj[i].find("h4")
		.delay(adjusted * 200)
		.animate({"left":int_txtLeft},800, str_ease)
		
		arr_proj[i].find(".proj-ind")
		.delay(adjusted * 200)
		.animate({"left":int_indLeft},800, str_ease)
		
	}
	(int_current == 0) 						? $("#prev").addClass("disabled") : $("#prev").removeClass("disabled");
	(int_current == (arr_proj.length - 3)) 	? $("#next").addClass("disabled") : $("#next").removeClass("disabled");
}

// PHOTO ALBUM --------------------------------------------------------------
function InitPhotos(){
	
	photoInterval = setInterval ( "RotatePhoto()", 6000 );
	$('#photo-album').addClass('active');
	$('#photo-album').click(function(){
		clearInterval(photoInterval);
		RotatePhoto();
	});
	
}

function RotatePhoto(){
	
	var str_ease		= "easeInExpo";
	
	int_currPhoto ++;
	if(int_currPhoto == $('#photo-album img').length ) int_currPhoto = 0;
	int_currZ ++;
	
	var thePhoto 	= $('#photo-album img')[int_currPhoto];
	var xPos 		= parseInt( $(thePhoto).css('left') );
	$('#caption').fadeOut(750);
	$(thePhoto)
	.animate({"left": -220}, 800, 'easeInExpo', function(){
		
		var theText = $(thePhoto).attr('alt');
		if(theText == 'You can find me here.') theText = 'You can find me <a href="http://maps.google.co.nz/maps?f=q&source=s_q&hl=en&geocode=&q=breakaway+republic&sll=-41.244772,172.617188&sspn=46.020881,135.263672&ie=UTF8&hq=breakaway+republic&hnear=&z=13&iwloc=A" rel="external">here</a>.'
		
		$('#caption')
		.css('-moz-transform', 'rotate('+RandRange(-3,3)+'deg)')
		.css('-webkit-transform', 'rotate('+RandRange(-3,3)+'deg)')
		.html( theText )
		.fadeIn(800);
		$(thePhoto)
		.css('z-index', int_currZ)
		.animate({"left": xPos}, 800, 'easeOutExpo')
	})
	
}

// UTILS --------------------------------------------------------------
jQuery.fn.eqByRow = function(){
	/*
	Purpose: equalise the heights of li's in a list by row
	usage: $('#eq-this-list').eqByRow();
	*/
	var inRow 	= Math.floor( $(this).width() / $(this).children("li:first").width() );
	var items 	= $(this).children('li');
	for (var i = 0; i < ($(items).length / inRow); i++){
		var max 	= 0;
		var start 	= (i * inRow);
		for (var j = start; j < start + inRow; j++){
			if(items[j]){
				if( $($(items)[j]).height() > max ) max = $($(items)[j]).height();
			}
		};
		for (var k = start; k < start + inRow; k++){
			if(items[k]){
				$($(items)[k]).height(max);
			}
		};
	};
}

jQuery.fn.biggerClick = function(){
	/*
	Purpose: makes a container 'hot', and binds it click event to go to the location of the first <a>'s href it finds within it
	usage: $('#make-these-hot ul li').biggerClick();
	*/
	$(this).hover(
		function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		}
	)
	$(this).click(function(){
		window.location = $(this).find("a:first").attr("href");
	})
}

function SetExternalLinks(){
	/*
	Purpose: checks all <a>'s in the doc, if it has a rel attribute of 'external', sets to open in new window
	usage: <a href="http://www.google.com" rel="external">Google</a>
	*/
	// $('a[rel=external]').click(function(){ window.open(this.href); return false; });
	$('a[rel=external]').live('click', function(){ window.open(this.href); return false; });
}

function RandRange(minNum, maxNum) {
	return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}

jQuery.fn.defaultValueInput = function(){
	for (var i=0; i < $(this).length; i++) {
		$(this[i]).data('defaultValue', $(this[i]).attr('value'));
		$(this[i]).addClass('default-value');
	};
	$(this).focus(function(){
		if( $(this).attr('value') == $(this).data('defaultValue') ){
			$(this).attr('value', '');
			$(this).removeClass('default-value');
		}
	});
	$(this).blur(function(){
		if( $(this).attr('value') == '' || $(this).attr('value') == $(this).data('defaultValue') ){
			$(this).attr('value', $(this).data('defaultValue'));
			$(this).addClass('default-value');
		}
	});
}

// ======================================================================

