/**
 * @author Manish Buttan
 Copyright (c) 2011. Artificial Machines
 */

$ = jQuery;

// On load
$(document).ready(init);

// Animation image names
var animImageNames;

// Animation images array 
var animImages = new Array();

// Path to the images
var imgPath = "../images/";

// Variable to load images
var count;

// start point of animation
var leftPos = 0;

// Main initialization
function init() {
	onHomeClick();
	$("#iosBodyText").hide();
	$("#androidBodyText").hide();
	$("#webBodyText").hide();
	$("#aboutBodyText").hide();
	
	//Initalize the array of animation images
	animImageNames = new Array(
		"cramzy_jokes_1.jpg",
		"cramzy_jokes_2.jpg",
		"cramzy_jokes_3.jpg",
		"cramzy_jokes_4.jpg",
		"cramzy_jokes_5.jpg",
		"cramzy_jokes_6.jpg",
		"cramzy_quotes_1.jpg",
		"cramzy_quotes_2.jpg",
		"cramzy_quotes_3.jpg",
		"cramzy_quotes_4.jpg",
		"cramzy_quotes_5.jpg",
		"blackstone_1.jpg",
		"blackstone_2.jpg",
		"blackstone_3.jpg",
		"blackstone_4.jpg",
		"blackstone_5.jpg",
		"blackstone_6.jpg",
		"blackstone_7.jpg",
		"blackstone_factory_1.jpg",
		"blackstone_factory_2.jpg",
		"blackstone_factory_3.jpg",
		"hme_smartcall_1.jpg",
		"hme_smartcall_2.jpg",
		"hme_smartcall_3.jpg",
		"hme_smartcall_4.jpg",
		"hme_smartcall_5.jpg",
		"lifeshield_1.jpg",
		"lifeshield_2.jpg",
		"lifeshield_3.jpg",
		"lifeshield_4.jpg",
		"lifeshield_5.jpg",
		"lifeshield_6.jpg",
		"lifeshield_7.jpg",
		"visteon_glare_hud_1.jpg",
		"visteon_glare_hud_2.jpg",
		"visteon_glare_hud_3.jpg",
		"visteon_glare_hud_4.jpg",
		"visteon_glare_hud_5.jpg",
		"visteon_glare_hud_6.jpg",
		"behr_color_smart_1.jpg",
		"behr_color_smart_2.jpg",
		"behr_color_smart_3.jpg",
		"behr_color_smart_4.jpg",
		"behr_color_smart_5.jpg",
		"behr_color_smart_6.jpg",
		"behr_color_smart_7.jpg",
		"hdr_1.jpg",
		"hdr_2.jpg",
		"hdr_3.jpg",
		"hdr_4.jpg",
		"veritas_1.jpg",
		"veritas_2.jpg",
		"veritas_3.jpg",
		"veritas_4.jpg",
		"veritas_5.jpg",
		"savant_1.jpg",
		"savant_2.jpg",
		"savant_3.jpg",
		"savant_4.jpg",
		"savant_5.jpg",
		"savant_6.jpg",
		"savant_7.jpg",
		"art_authority_1.jpg",
		"art_authority_2.jpg",
		"art_authority_3.jpg",
		"art_authority_4.jpg",
		"art_authority_5.jpg",
		"art_authority_6.jpg",
		"mcdonalds_1.jpg",
		"mcdonalds_2.jpg",
		"mcdonalds_3.jpg",
		"mcdonalds_4.jpg"
	);
	
	// Sort the array so that we start with a different set of images every time
	animImageNames.sort(randOrd);
	
	// Start the animation
	startAnimation();	
}

// Animation script for portfolio animation
function startAnimation() {
	count = 0; // animation counter
	setTimeout(animate, 2000); // start the animation after about 2 seconds
	loadImages(); // start loading the images
}

var duration = 500; // for time between images
var easing = 'easeInOutQuad'; // transition type

// Callback function for animate
var callback = function(){

	if ($("#imageContainer").position().left < -19000) // if the images have finished - rewind
	{
		$("#imageContainer").animate({
			'left': '+=19500' // change the left position of the container
		}, 1000, function() {
			setTimeout(animate, 4000); // call animate after 3 seconds
		});
	} else {
		setTimeout(animate, 4000); // call animate after 3 seconds
	}
}

// The animation
function animate() {
	$("#imageContainer").animate({
		'left': '-=200' // move container by 200 px;
	}, 
	{easing: easing,
	duration: duration,
	complete: callback
	});	
}

function randOrd(){
	return (Math.round(Math.random())-0.5); // randomize the order of the images
} 


// Image loader
function loadImages() {
	var loc = window.location.href; // URL
	var index = loc.lastIndexOf("/"); // get index of last /
	var str = loc.substring(0, index); // truncate to folder 
	
	// for all the animation image namesnames 
	for (var i=0; i<animImageNames.length; i++)
	{
		// Add the div of the image
		$("#imageContainer").append("<img id='image"+ i +"' src='" + str + '/images/' + animImageNames[i] + "' />");
	}
}

// To highlight the menu items
function onMenuMouseOver(event) {
	$(event.target).addClass("highlightClass");	
}

// To remove highlight
function onMenuMouseOut(event) {
	$(event.target).removeClass("highlightClass");
} 

// To show the Home div
function onHomeClick() {

	$("#home").addClass("selectedClass");
	$("#ios").removeClass("selectedClass");
	$("#android").removeClass("selectedClass");
	$("#web").removeClass("selectedClass");
	$("#about").removeClass("selectedClass");
	
	$("#homeBodyText").show('highlight');
	$("#iosBodyText").hide();
	$("#androidBodyText").hide();
	$("#webBodyText").hide();
	$("#aboutBodyText").hide();
	
}

// To show the iOS div
function onIosClick() {
	$("#home").removeClass("selectedClass");
	$("#ios").addClass("selectedClass");
	$("#android").removeClass("selectedClass");
	$("#web").removeClass("selectedClass");
	$("#about").removeClass("selectedClass");

	$("#homeBodyText").hide();
	$("#iosBodyText").show('highlight');
	$("#androidBodyText").hide();
	$("#webBodyText").hide();
	$("#aboutBodyText").hide();
}

// To show the Android div
function onAndroidClick() {
	$("#home").removeClass("selectedClass");
	$("#ios").removeClass("selectedClass");
	$("#android").addClass("selectedClass");
	$("#web").removeClass("selectedClass");
	$("#about").removeClass("selectedClass");
	
	$("#homeBodyText").hide();
	$("#iosBodyText").hide();
	$("#androidBodyText").show('highlight');
	$("#webBodyText").hide();
	$("#aboutBodyText").hide();
}

// To show the Web div
function onWebClick() {
	$("#home").removeClass("selectedClass");
	$("#ios").removeClass("selectedClass");
	$("#android").removeClass("selectedClass");
	$("#web").addClass("selectedClass");
	$("#about").removeClass("selectedClass");
	
	$("#homeBodyText").hide();
	$("#iosBodyText").hide();
	$("#androidBodyText").hide();
	$("#webBodyText").show('highlight');
	$("#aboutBodyText").hide();
}

// To show the About div
function onAboutClick() {
	$("#home").removeClass("selectedClass");
	$("#ios").removeClass("selectedClass");
	$("#android").removeClass("selectedClass");
	$("#web").removeClass("selectedClass");
	$("#about").addClass("selectedClass");
	
	$("#homeBodyText").hide();
	$("#iosBodyText").hide();
	$("#androidBodyText").hide();
	$("#webBodyText").hide();
	$("#aboutBodyText").show('highlight');
}
