/**
 * Module: HR01
 * Controller JavaScript
 **/

 var C4GTI = C4GTI || {};

	C4GTI.HR01 = {
		selectedIndex:0,
		doneImagesLoad:false,
		interval:null,
		animationStarted:false,
		setContent: function() {
			//Handle Text 
			//Handle Selection
			if($('.moduleID-HR01 .selected div').hasClass("a1")) {
				$('.moduleID-HR01 .componentRow').addClass("first");
			} else {
				$('.moduleID-HR01 .componentRow').removeClass("first");
			}
			//Handle Images
			//$('.moduleID-HR01 .selected div.brand')
			if($('.moduleID-HR01 .selected div').hasClass('brand')) {
				src = $('.moduleID-HR01 .selected div.brand img').attr('src').replace('_200x113','_625x352');
				$('.moduleID-HR01 .selected div.brand img').attr('src', src);
				
			} else {
				src = $('.moduleID-HR01 .selected div.nobrand img').attr('src').replace('_A4','_A1');
				$('.moduleID-HR01 .selected div.nobrand img').attr('src',src);
			}

			Link = $('.moduleID-HR01 .selected h2 a').attr("href");
			LinkText = $('.moduleID-HR01 .selected h2 a').text();
			isExternal = $('.moduleID-HR01 .selected h2 a').attr("rel");
			//Update Image Link
			$(".moduleID-HR01 a.imageLink").attr('href',Link);
			$(".moduleID-HR01 a.imageLink span").text(LinkText);
			C4GTI.HR01.insertMore()
		},
		insertMore:function() {
			
			if(!$(".moduleID-HR01 .selected p.promoText a").hasClass('moreLink')) {
				
				morelink = "<a href='"+ Link +"' class='moreLink'><span>"+LinkText+"</span>&nbsp;</a>";
				$(".moduleID-HR01 .selected p.promoText").append(morelink);
				if(isExternal) {
					$(".moduleID-HR01 .selected p.promoText .moreLink").bind("click", function(e) {
						e.preventDefault();
						var w=window.open($(".moduleID-HR01 .selected p.promoText .moreLink").attr("href"));	
					})
				}
			}
		},
		updateContent:function(e,animate) {
			
			
			
				$('.moduleID-HR01 .selected p.watch-now').css("display", "none");
				$('.moduleID-HR01 .selected p').fadeOut(10);
				
				$('.moduleID-HR01 .selected').removeClass("selected");
				$(e).parents("div.component").addClass("selected");
				
				C4GTI.HR01.setContent();
				$('.moduleID-HR01 .selected p').fadeIn(600);
			
		},
		animateContent:function(animate) {
			
			C4GTI.HR01.selectedIndex =(C4GTI.HR01.selectedIndex<2)?C4GTI.HR01.selectedIndex+1:0;
			C4GTI.HR01.updateContent($(".moduleID-HR01 .componentRow .component:eq("+C4GTI.HR01.selectedIndex+") h2 a"),animate);
		},
		stopContent:function(e, animate) {
			C4GTI.HR01.startedAnimation = false;
			clearInterval(C4GTI.HR01.interval);
			
		},
		startAnimation:function(e,animate) {
			if (!C4GTI.HR01.startedAnimation) {
				C4GTI.HR01.startedAnimation = true;
				C4GTI.HR01.interval = setInterval("C4GTI.HR01.animateContent(" + animate + ")", 5000);
			}
		},
		init:function(animate) {
			module = $(".moduleID-HR01");
			moduleRow = $(".moduleID-HR01 .componentRow");
			module.removeClass("c3");
			module.addClass("c1");
			if(! moduleRow.prev('a').hasClass('imageLink')) {
			moduleRow.before("<a href='' class='imageLink'><span class='accessibility'></span></a>");
			}
			moduleRow.removeClass("disabled");
			moduleRow.addClass("enabled");
			$(".moduleID-HR01 .promoText").each(function(){
				$(this).summary({
					maxLength: 175
				})
			});
			
			C4GTI.HR01.setContent();
			$('.moduleID-HR01 .selected p').fadeIn(600);
			$('div.content .constrict a').click(function(e){
				C4GTI.HR01.selectedIndex = $(this).parents(".component").index();
				if(!$(this).parents("div").hasClass("selected")) {
					if($(this).attr("rel")=="external") {
						//stop tab opening new window
						e.stopImmediatePropagation()
					}
					C4GTI.HR01.updateContent(this);
					
					e.preventDefault();	
				}
			});
			if(animate) {
				$('div.content .constrict a,div.content .images a').bind("focus mouseover",function(e){
					C4GTI.HR01.stopContent(e,animate)
				});
				$('.moduleID-HR01').bind("focusout mouseleave",function(e){
					C4GTI.HR01.startAnimation(animate)
				});
				C4GTI.HR01.startAnimation(animate);
			}
			
		}
	};

