/*
Dynamic Application of Functions to DOM
*/
var _debugMode = true;
Event.observe(document, 'dom:loaded', init);
Event.observe(window, 'load', initAfterImages);


function init () {

}

function initAfterImages () {

	// NAV FUNCTIONS
	if($('nav_inner_shell')) {
		navigationDropDown();
	}
	
	// TAG LINES
	homePageTagLines();
	
	// TWITTER FUNCTIONS
	twitterFunctions();

	// SWAP OTHER HOVER IMAGES
	hoverSwapImages();

	// TEAM LIST SOCIAL NETWORK BUTTONS
	teamListSocialButtons();

	// ARROW LINKS
	arrowLinks();

	// AVAILABLE POSITIONS
	availablePositions();

	// CREATIVE WORK SLIDE SHOW
	//creativeWorkSlideshow();

}

// HOME PAGE TAG LINES
function homePageTagLines() {
	if($('home_feature_headline')) {	
					
		var slides = $$("div#home_feature_headline h1").toArray();
		slides[0].setStyle("display: block !important;"); // SET FIRST SLIDE VISIBLE
		
		var currSlide = 0;
		var wait = false;
			
		// ROTATER 
		function slideRotater(slides,currSlide,timeout,wait) {
				
			function rotateSlideOnTimeOut() {
				wait = true;
				animateArrowIcon();
				Effect.DropOut(slides[currSlide], { 
					afterFinish: function() {
						slides[currSlide].setStyle("display: none !important;"); 
						currSlide = slideRight(currSlide,slides.length);
						slides[currSlide].setStyle("display: block !important;");
						wait = false;
					}
				});	
			}
				
			function animateArrowIcon() {
				
				var oldHTML 	= $("home_feature_refresh").innerHTML;
				var newHTML 	= "";
				var timer 		= "";
				
				for(i=0; i<2; i++) {
					newHTML += "<img src=\"http://idgroupusa.cocodev.com/interface/images/icon_refresh_small_frame_0"+i+".png\" border=\"0\">"
				}
				
				$("home_feature_refresh").update(newHTML);
				
				var frames = $$("#home_feature_refresh img").toArray();
				var currFrame = 0;
				var totalFrames = 0;
				
				frames[currFrame].setStyle("display: block");
				
				function frameNext() {
					frames[currFrame].setStyle("display: none"); // SET CURRENT FRAME TO INVISIBLE
					if(currFrame == 1) {
						currFrame = 0;
					} else {
						currFrame++;
					}
					frames[currFrame].setStyle("display: block"); // DISPLAY THE NEXT FRAME
					totalFrames++;
					if(totalFrames == 3) {
						clearInterval(timer);
						$("home_feature_refresh").update(oldHTML);
					}
				}
				
				timer = setInterval(frameNext,150);	
				
				
			}
							
			 $("home_feature_refresh").observe('click', function(e) {
				if(!wait) {
					wait = true;
					Event.stop(e);
					Effect.DropOut(slides[currSlide], { 
						afterFinish: function() {
							slides[currSlide].setStyle("display: none !important;"); 
							currSlide = slideRight(currSlide,slides.length);
							slides[currSlide].setStyle("display: block !important;");
							wait = false;
						}
					});
				}
			});
				
			setInterval(rotateSlideOnTimeOut,timeout);
		}
			
		// ACTIVATE ROTATER
		slideRotater(slides,currSlide,5000);
		
		// INITAL NAVIGATION AUTO DROPDOWN
		var timeout = 2000; // TIME OUT FOR DROP DOWN IN MILLISECONDS
		homepageNavigationAutoDropDown(timeout); 
			
	}	
}

// TWITTER FUNCTIONS
function twitterFunctions() {
	if($("twitter_elements")) {
		
		$$('ul li div.twitter_element_shell div.twitter_item_content p a').each(function(n) {
			start 	= n.innerHTML.substr(0,9);
			end 	= n.innerHTML.substr(10);
			newText = '<span class="purple">'+start+'</span> '+end;
			n.update(newText);
		});
		
		// TWITTER DATES
		$$('ul li div.twitter_element_shell div.twitter_item_timeposted span.timeposted_date').each(function(n) {
			var newSting = "on " + Date.parse(n.innerHTML).toString('MM/dd/yyyy');
			n.update(newSting);
		});
	}
}

// HOVER SWAP IMAGES
function hoverSwapImages() {
	$$('.hoverSwap').each(function(n) {
		var original_image = n.src;
		n.observe('mouseover', function(e) {
			n.src = n.alt;
		});

		n.observe('mouseout', function(e) {
			n.src = original_image;
		});
	});	
}

// TEAM LIST SOCIAL BUTTONS
function teamListSocialButtons() {
	if($('aboutTeamList')) {
		// ABOUT TEAM LIST, HIDE EMPTY ELEMENTS
		$$('ul#aboutTeamList ul.aboutTeamSocialIcons li a[href=""]').each(function(n) {
			n.up().setStyle("display: none;");
		});
		
		// ABOUT TEAM LIST, HIDE EMPTY SKYPE ELEMENTS
		$$('ul#aboutTeamList ul.aboutTeamSocialIcons li a[href="skype:"]').each(function(n) {
			n.up().setStyle("display: none;");
		});
	}	
}

// ARROW LINKS
function arrowLinks() {
	$$("div.arrow_link_right").each(function(n) {
		n.observe('mouseover', function(e) {
			var children = n.descendants();
			var arrow = children[1]; // the arrow is the 2nd element
			arrow.setStyle("background-image: url(http://www.idgroupusa.com/interface/images/icon_arrow_purple.png);");
		});

		n.observe('mouseout', function(e) {
			var children = n.descendants();
			var arrow = children[1]; // the arrow is the 2nd element
			arrow.setStyle("background-image: url(http://www.idgroupusa.com/interface/images/icon_arrow_gray.png);");
		});
	});

	$$("div.arrow_link_left").each(function(n) {
		var children = "";
		var arrow = "";
		n.observe('mouseover', function(e) {
			children = n.descendants();
			arrow = children[1]; // the arrow is the 2nd element
			arrow.setStyle("background-image: url(http://www.idgroupusa.com/interface/images/icon_arrow_purple_left.png);");
		});

		n.observe('mouseout', function(e) {
			children = n.descendants();
			arrow = children[1]; // the arrow is the 2nd element
			arrow.setStyle("background-image: url(http://www.idgroupusa.com/interface/images/icon_arrow_gray_left.png);");
		});
	});	
}

// AVAIL POSITIONS IN CONTACT PAGE
function availablePositions() {
	if($('availablePositions')) {
		$$("ul#availablePositions li").each(function(n) {
			var children = "";
			var arrow = "";
			n.observe('mouseover', function(e) {
				children = n.descendants();
				arrow = children[2]; // the arrow is the 2nd element
				arrow.setStyle("background-image: url(http://www.idgroupusa.com/interface/images/icon_arrow_purple.png);");
			});

			n.observe('mouseout', function(e) {
				children = n.descendants();
				arrow = children[2]; // the arrow is the 2nd element
				arrow.setStyle("background-image: url(http://www.idgroupusa.com/interface/images/icon_arrow_gray.png);");
			});
		});
	}	
}

// SELECTED CREATIVE WORK LIST
/* function creativeWorkSlideshow() {
	if($('selectedwork_main_creativework') && $('selectedwork_scroller')) {
		var slides = $$("#selectedwork_main_creativework ul li").toArray();
		var currSlide = 0;
		
		if(slides.length > 0) { // DO NOT RUN IF THERE ARE NO SLIDES
			slides[0].setStyle("display: block !important;"); // SET FIRST SLIDE VISIBLE
			$$("#selectedwork_scroller div.arrow_link_left a").each(function(n) {
				n.observe('click', function(e) {
					Event.stop(e);
					currSlide = slideLeft(currSlide,slides.length);
					changeSlide(slides,currSlide);
				});
			});

			$$("#selectedwork_scroller div.arrow_link_right a").each(function(n) {
					n.observe('click', function(e) {
					Event.stop(e);
					currSlide = slideRight(currSlide,slides.length);
					changeSlide(slides,currSlide);
				});
			});
		}
		
	}	
}
*/

function slideLeft(currSlide,numSlides) {
	if(currSlide == 0) {
		currSlide = numSlides-1;
	} else {
		currSlide--;
	}
	return currSlide;
}

function slideRight(currSlide,numSlides) {
	if(currSlide == numSlides-1) {
		currSlide = 0;
	} else {
		currSlide++;
	}
	return currSlide;
}

function changeSlide(slides,currSlide) {
	for(i=0; i<slides.length; i++) {
		if(i == currSlide) {
			slides[i].setStyle("display: block !important;");
		} else {
			slides[i].setStyle("display: none !important;");
		}
	}
}

function navigationDropDown() {
	$("nav_open").observe('click', function(e) {
		Event.stop(e);
		$('nav_inner_shell').hide();
		Effect.SlideDown($('nav_dropdown'));
	});

	$("nav_close").observe('click', function(e) {
		Event.stop(e);
		Effect.SlideUp($('nav_dropdown'), { afterFinish: function() { $('nav_inner_shell').show(); } } );
	});
}

function homepageNavigationAutoDropDown(timeout) {
	$('nav_inner_shell').hide();
	Effect.SlideDown($('nav_dropdown'));
	
	function autoDropDownUp() {
		Effect.SlideUp($('nav_dropdown'), { afterFinish: function() { $('nav_inner_shell').show(); } } );
	}

	setTimeout(autoDropDownUp,timeout);
}

function t(f) {
  Try.these(f);
}

function cl (arg) {
  if (_debugMode && console) console.log(arg);
}

