// FLYOUT FUNCTION - TO CHANGE IMAGES ON FRONT PAGE
jQuery.fn.flyOut = function() {
	// GET ID FOR BOX
	var id=($(this).attr("id").split("_"))[1];
	//GET IMAGE SOURCE INFORMATION AND USE THIS
	var idNext = parseInt(id);
	idNext = idNext + 1;
	var productElement = document.getElementById("box_" + idNext);
	if (productElement == null) {
		idNext = 1;
	}
	if (id != idNext) {
	$("#box_"+ id).fadeOut(3000);
		$("#box_"+ idNext).fadeIn(3000, function() {
			// ONCE FADE COMPLETED, REMOVE OLD IMAGE AND CHANGE NEW IMAGE ID TO OLD IMAGE ID
			// BEGIN THE PROCESS AGAIN
			$(this).animate({left:0}, 5000, function() {
				$(this).flyOut();
			});
		}); // END IMAGE FADED IN CONDITIONAL
	}
}; // END FLYOUT FUNCTION

// TALENTOUT FUNCTION - TO CHANGE TOMORROW'S TALENT BOX ON FRONT PAGE
jQuery.fn.talentOut = function() {
	// GET ID FOR BOX
	var id=($(this).attr("id").split("_"))[1];
	//GET IMAGE SOURCE INFORMATION AND USE THIS
	var idNext = parseInt(id);
	idNext = idNext + 1;
	var productElement = document.getElementById("profile_" + idNext);
	if (productElement == null) {
		idNext = 1;
	}
	if (id != idNext) {
	$("#profile_"+ id).fadeOut(3000);
		$("#profile_"+ idNext).fadeIn(3000, function() {
			// ONCE FADE COMPLETED, REMOVE OLD IMAGE AND CHANGE NEW IMAGE ID TO OLD IMAGE ID
			// BEGIN THE PROCESS AGAIN
			$(this).animate({left:0}, 5000, function() {
				$(this).talentOut();
			});
		}); // END IMAGE FADED IN CONDITIONAL
	}
}; // END TALENTOUT FUNCTION


$(function() {
		$(".inside_holder").corner("15px");
		$("#top").corner("15px");
		$(".top_logo").corner("tr 15px");
		$(".top_main").corner("br");
		$(".top_image").corner("left 15px");
		$(".top_share").corner();
		$(".top_share_box").corner("tl br");
		$("#profile_box").corner("15px");
		$(".profile_share_box").corner("tl br");
		$(".profile_share").corner();
		$("#search_box").corner("top 15px");
		$("#search_box").corner("bottom 10px");
		$(".subscribe_box input").focus(function() {
			var val = $(this).val();
			if (val == "Email Address") {
				$(this).val("");
			}
		});
		$("#search_box input").focus(function() {
			var val = $(this).val();
			if (val == "SEARCH") {
				$(this).val("");
			}
		});
		if ($.browser.msie) {
			$("link.changeme").attr("href","css/templateie.css");
		}
		$(".top_share_box .share").hover(function() {
			$(".top_share").fadeIn(500);
		}, function() {
		});
		$(".profile_share_box").hover(function() {
			$(".profile_share").fadeIn(500);
		}, function() {
		});
		$("#top").mouseleave(function() {
			$(".top_share").fadeOut(300);
		});
		$("#content_right").mouseleave(function() {
			$(".profile_share").fadeOut(300);
		});
		$(".top_share").mouseleave(function() {
			$(this).fadeOut(300);
		});
		$(".profile_share").mouseleave(function() {
			$(this).fadeOut(300);
		});

		$("#menu .menu_item img").hover(function() {
			$(this).animate({"opacity" : "0.8"}, 400);
		}, function() {
			$(this).animate({"opacity" : "1"}, 400);
		});
		$("#top").mouseover(function() {
			$(".profile_share").fadeOut(300);
		});
		$("#content_left").mouseover(function() {
			$.each(".top_share").fadeOut(300);
			$.each(".profile_share").fadeOut(300);
		});
		$("#content_right").mouseover(function() {
			$(".top_share").fadeOut(300);
		});
		// TURN SEARCH ARROW INTO SUBMIT BUTTON
		$("#search_button").click(function() {
			var text = $("input[name=search]").val();
			// CHECK IF SEARCH IS VALID AND IF SO REDIRECT TO SEARCH CAMPAIGN LIVE
			if (text != "SEARCH" && text != "") {
				text = text.replace(/ /g, "+");
				text = text.replace(/[^a-zA-Z+]/g, "");
				window.location = "http://www.campaignlive.co.uk/alist/index.cfm?fuseaction=CMO.AList.Search.Results&sSearchPhrase=" + text + "&nDirectoryEditionID=12&sSearchSubmit=Go";
			}
			else { 
				alert("Please enter a valid search term"); 
			}; // END SEARCH VALID CONDITIONAL
		}); // END SEARCH IMAGE CLICKED CONDITIONAL
		// TURN SUBSCRIBE ARROW INTO SUBMIT BUTTON
		$("#subscribe_button").click(function() {
			var text = $("input[name=subscribe]").val();
			// CHECK THAT TEXT IS VALID (BASIC CHECK, PHP CHECK LATER)
			if (text != "Email Address" && text != "") {
				$("form").submit();
			}
			else {
				alert("Please enter a valid email."); 
			}; // END SUBSCRIBE CLICKED CONDITIONAL
		}); // END SUBSCRIBE IMAGE CLICKED CONDITIONAL
		
		// INSIDE IMAGE ROTATING
		$("#box_1").animate({left:0}, 5000, function() {
			$(this).flyOut();
		});
		// TOMORROW'S TALENT IMAGE ROTATING
		$("#profile_1").animate({left:0}, 5000, function() {
			$(this).talentOut();
		});

		// INSIDE LINK OPENING ON PAGE
		// FORMATTITLE REQUIRED FOR LINKS AT BOTTOM OF FANCYBOX
		function formatTitle(title, currentArray, currentIndex, currentOpts) {
			return '<div id="tip7-title"><span><a href="' + currentArray + '" target="_blank" style="color: #f05033">NEW WINDOW</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a style="color: #f05033" href="javascript:;" onclick="$.fancybox.close();">CLOSE</a></span></div>';
		}
		// LINKS OPENING IN PAGE
		$("a.iframe").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	false,
			'width' : '90%',
			'height' : '90%',
			'titlePosition' 		: 'inside',
			'titleFormat'		: formatTitle
		});
	});
