/*********************************
 Core Architects Website
 
 author    : Sandra Mansour
 date      : 4-May-2011
 file name : myscript.js
 
 Copyright 2011 52 Pick-up Inc.
 Do not use without permission
 info@52pick-up.com
 *********************************/


if (typeof (PU52) == 'undefined')
    var PU52 = {};


/***
 Class Generic() - All global functions should be placed in this class.
 ***/
PU52.Generic = {
	//variable declarations
	_first_nav: null,		//[NodeList] id of the first subnav (null if none displayed)
	_curr_level: 0,			//[integer] the current level
	
	init: function()
	{
		$("div.loading").hide();
		$('#PU52-main').fadeIn(1000);
	},
	
	/***
	 function showSubnav()
	 ***/
	showSubnav: function(thisObj, objId, level)
	{				
		$(thisObj).addClass("selected");
		if (PU52.Generic._first_nav != null)
		{
			prev = null;
		 	curr = PU52.Generic._first_nav.getCurrNode(level);

		 	//hide nodes starting from the current node 'curr':
		 	while (curr != null)
		 	{
			 	$("#" + curr._id).removeClass("selected");
			 	$("#" + curr._id + "_li").removeClass("selected");
			 	$("#" + curr._id).hide();
			 	
			 	if ($("#" + curr._id + "-5.nav5").length > 0)
			 	{
				 	$("#" + curr._id + "-5.nav5").removeClass("selected");
				 	$("#" + curr._id + "-5.nav5").hide();
			 	}
			 	if ($("#" + curr._id + "-4.nav4").length > 0)
			 	{
				 	$("#" + curr._id + "-4.nav4").removeClass("selected");
				 	$("#" + curr._id + "-4.nav4").hide();
			 	}
			 	
			 	prev = curr;
			 	curr = curr._next
			 	prev._next = null;			 	
		 	}
		 	if (level == 2)
		 		PU52.Generic._first_nav = null;
	 	}

		$("#" + objId).fadeIn('fast');
		if ($("#" + objId + "-5.nav5").length > 0)
			$("#" + objId + "-5.nav5").fadeIn('fast');
		if ($("#" + objId + "-4.nav4").length > 0)
			$("#" + objId + "-4.nav4").fadeIn('fast');
		
		if (PU52.Generic._first_nav == null)
			PU52.Generic._first_nav = new NodeList(objId, level);
		else
		{
			temp = new NodeList(objId, level);
			PU52.Generic._first_nav.add(temp);
		}
	}
}

/***
 Class JobPosting()
 ***/
PU52.JobPosting = {
	curr_id: "",	//[string] id of the current shown section

	/***
	 function showSection()
	 ***/
	showSection: function(id)
	{
		if (PU52.JobPosting.curr_id != "")
			$("#" + PU52.JobPosting.curr_id + " p").hide();
		$("#" + id + " p").show();
		PU52.JobPosting.curr_id = id;
	}
}


/***
 Class NodeList()
 ***/
function NodeList(id, level)
{
	this._id = id;			//[string] the id of this Node
	this._level = level;	//[integer] the level of this node
	this._next = null;		//[NodeList] the next node in the list
	
	/***
	 function add() - adds node to the end of the list
	 ***/
	this.add = function(nav)
	{
		if (this._next == null)
			this._next = nav;
		else
		{
			curr = this._next;
			while (curr._next != null)//go to the end
				curr = curr._next;
			curr._next = nav; //add the node
		}
	}
	
	/***
	 function getCurrNode() - gets the current node by level #
	 ***/
	this.getCurrNode = function(level)
	{
		temp = this;
		while (temp._next != null && temp._level != level)
			temp = temp._next;
	
		//if we didn't find the current level, return null:
		if (temp._level != level)
			temp = null;

		return temp;
	}
}


/***
 Class Gallery() - this gallery displays all images horizontally and handles
 				   left and right slides
 ***/
PU52.Gallery = {
	//constant variables:
	SPEED: 700,				//[integer] speed of the animation

	//variable declarations:
	_currImage: 1,				//[integer] index of the current image
	_imageCount: 0,				//[integer] the number of images displayed
	_currentPosition:0,			//[integer] the current position of the scrolling div
	_slide_in_progress: false,	//[boolean] indicates whether a slide is in progress

	//slide left (increasing) or right (decreasing) to the next image.
	slideNext: function(direction, device)
	{
		//only continue if another slide is NOT in progress!
		if (!PU52.Gallery._slide_in_progress)
		{
			PU52.Gallery._slide_in_progress = true;

			var margin = parseInt($("#PU52-image" + PU52.Gallery._currImage).css('margin-right').replace('px', ''));
			var imgWidth = $("#PU52-image" + PU52.Gallery._currImage).width() + margin;
			var nextPosition = 0;

			if (direction == 1) //slide right
			{
				var prevImgIndex = PU52.Gallery._currImage - 1;
				var prevImgWidth = $("#PU52-image" + prevImgIndex).width() + margin;
				nextPosition = PU52.Gallery._currentPosition + prevImgWidth;
			}
			else if (direction == -1) //slide left
				nextPosition = (direction * imgWidth) + PU52.Gallery._currentPosition;

			$("#PU52-ImageContainer").animate({left: nextPosition}, PU52.Gallery.SPEED, function() {
				PU52.Gallery._currImage -= direction;
				PU52.Gallery._currentPosition = nextPosition;

				//show left and right arrows:
				$("#PU52-right_arrow").show();
				$("#PU52-left_arrow").show();
				if (PU52.Gallery._currImage == PU52.Gallery._imageCount)
					$("#PU52-right_arrow").hide();
				else if (PU52.Gallery._currImage == 1)
					$("#PU52-left_arrow").hide();

				//update the slide text if the device is an iOS:
				if (device == "ios")
				{
					var navText = "";
					if (PU52.Gallery._currImage == PU52.Gallery._imageCount)
						navText = "Slide Right >>";
					else if (PU52.Gallery._currImage == 1)
						navText = "<< Slide Left";
					if (navText != "")
						$("#PU52-iOSNav").text(navText);
				}

				PU52.Gallery._slide_in_progress = false;
			 });
		}
	}
}


/***
 Class iOSGallery() - handles left and right slides on the iPhone to move images
 ***/
PU52.iOSGallery = {
	_start_x: 0,		//[integer] initial touch position
	_curr_x: 0,			//[integer] current position at slide end
	_current_width: 0,	//[integer] current width of the screen
	_previous_width: 0, //[integer] previous width of the screen
	_firstSlide: true, 	//[boolean] is this the first slide? initially, yes!
	
	touchStart: function(event)
	{
		var touch = event.touches[0];
		PU52.iOSGallery._start_x = touch.pageX;
		
		if (PU52.iOSGallery._firstSlide)
			PU52.iOSGallery._firstSlide = false;
	},

	touchMove: function(event)
	{
		event.preventDefault();

		//stop if a slide is in progress:
		if (!PU52.Gallery._slide_in_progress)
		{
			var touch = event.touches[0];
			PU52.iOSGallery._curr_x = touch.pageX;

			//determine the direction of the slide. for the iOS, the direction will be
			//the opposite of the website. so a negative value is a right slide and a
			//positive value is a left slide.
			var direction = PU52.iOSGallery._start_x - PU52.iOSGallery._curr_x;
			if (direction < 0)//slide right
				direction = 1;
			else if (direction > 0)//slide left
				direction = -1;

			//only slide if the following conditions are not true:
			//1. the _currImage does not equal _imageCount if direction is a left slide (-1)
			//2. the _currImage does not equal 1 if direction is a right slide (+1)
			if ((direction == -1 && PU52.Gallery._currImage != PU52.Gallery._imageCount) ||
			    (direction ==  1 && PU52.Gallery._currImage != 1))
				PU52.Gallery.slideNext(direction, "ios");
		}
	},
	
	touchEnd: function(event)
	{
		PU52.iOSGallery._start_x = 0;
		PU52.iOSGallery._curr_x = 0;
	}
}



/***********************
Class PhotoPlayer()
***********************/
PU52.PhotoPlayer = {
	/* constant variables */
	LOOP_MAX: 10,				//[integer] the number of times to loop through the set
	GROUP_COUNT: 3,				//[integer] the number of images in the set
	GROUP_ID: "PU52-Group_",	//[string] the image object ID
	DELAY: 5000,				//[string] the amount of time to delay before starting the player, in milliseconds
	SPEED: 800,				//[string] the speed at which to show/fade the images in milliseconds
	
	/* variables */
	_loop_count: 0,				//[integer] number of times the set has looped
	_player_is_on: 0,			//[boolean] indicates whether the player is on (true) or off (false)
	_curr_group: -1,			//[integer] the index of the previous group
	_next_group: -1,			//[integer] the index of the current group
	_timer: '',					//[Timeout] the timeout object
	
	Init: function(selected_ind)
	{
		PU52.PhotoPlayer._curr_group = selected_ind;
		$(window).load(function() {
			PU52.Generic.init();
			PU52.PhotoPlayer.Start();
		});
		$(window).blur(function() {
			PU52.PhotoPlayer.Stop();
		});
	},
	
	Start: function()
	{				
		if (!PU52.PhotoPlayer._player_is_on)
		{
			PU52.PhotoPlayer._player_is_on = 1;
			PU52.PhotoPlayer._next_group = PU52.PhotoPlayer._curr_group;
			PU52.PhotoPlayer._timer = setTimeout("PU52.PhotoPlayer.Play()", PU52.PhotoPlayer.DELAY + PU52.PhotoPlayer.SPEED);
		}
	},
	
	Play: function()
	{
		//stop the loop once we have reached the maximum number of loops:		
		if (PU52.PhotoPlayer._loop_count == PU52.PhotoPlayer.LOOP_MAX)
			PU52.PhotoPlayer.Stop();
		else if (PU52.PhotoPlayer._next_group == PU52.PhotoPlayer.GROUP_COUNT)
		{
			PU52.PhotoPlayer._loop_count++;
			PU52.PhotoPlayer._next_group = 1;
		}
		else
			PU52.PhotoPlayer._next_group++;
		
		if (PU52.PhotoPlayer._player_is_on)
		{
			$("#" + PU52.PhotoPlayer.GROUP_ID + PU52.PhotoPlayer._curr_group).fadeOut(PU52.PhotoPlayer.SPEED, function()
				{
					$("#" + PU52.PhotoPlayer.GROUP_ID + PU52.PhotoPlayer._next_group).delay('100').fadeIn(PU52.PhotoPlayer.SPEED);
				});

			PU52.PhotoPlayer._timer = setTimeout("PU52.PhotoPlayer.Play()", PU52.PhotoPlayer.DELAY);
		}
		
		PU52.PhotoPlayer._curr_group = PU52.PhotoPlayer._next_group;
	},
	
	Stop: function()
	{
		clearTimeout(PU52.PhotoPlayer._timer);
		PU52.PhotoPlayer._timer = null;
		PU52.PhotoPlayer._player_is_on = 0;
	}
}
