/* create a timeOut function in jQuery */
jQuery.fn.idle = function(time){
    return this.each(function(){
        var i = $(this);
        i.queue(function(){
            setTimeout(function(){
            i.dequeue();
            }, time);
        });
    });
};
/**
 * jQuery Plugin Toggle Fade v1.0
 * Requires jQuery 1.2.3 (Not tested with earlier versions).
 * Copyright (c) 2008 Gregorio Magini [gmagini at gmail dot com] 
 * 
 *	@param: Object Array. Arguments need to be in object notation.
 *	Returns: jQuery.
 *	Options:	
 *		speedIn: Sets the speed of the fadeIn effect. Default: "normal".
 *    speedOut: Sets the speed of the fadeOut effect. Default: same as speedIn.
 *
 *	Examples: 
 *    
 *    speedIn and speedOut both "normal":
 *		$("#toggle-link").toggleFade();
 *
 *    speedIn and speedOut both "fast":
 *		$("#toggle-link").toggleFade({ speedIn : "fast");
 *
 *    different settings for speedIn and speedOut:
 *		$("#toggle-link").toggleFade({ speedIn : 800, speedOut : 150 });
 *
 */

(function($) {
  $.fn.toggleFade = function(settings)
  {
  	settings = jQuery.extend(
  		{
        speedIn: "normal",
        speedOut: settings.speedIn
  		}, settings
  	);
  	return this.each(function()
  	{
  	  var isHidden = jQuery(this).is(":hidden");
      jQuery(this)[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
    });
  };
})(jQuery);
/**
 * jQuery-Plugin "relCopy"
 * 
 * @version: 1.0.2, 25.02.2010
 * 
 * @author: Andres Vidal
 *          code@andresvidal.com
 *          http://www.andresvidal.com
 *
 * Instructions: Call $(selector).relCopy(options) on an element with a jQuery type selector 
 * defined in the attribute "rel" tag. This defines the DOM element to copy.
 * @example: $('a.copy').relCopy({limit: 5}); // <a href="example.com" class="copy" rel=".phone">Copy Phone</a>
 *
 * @param: string	excludeSelector - A jQuery selector used to exclude an element and its children
 * @param: integer	limit - The number of allowed copies. Default: 0 is unlimited
 * @param: string	append - HTML to attach at the end of each copy. Default: remove link
 * @param: string	copyClass - A class to attach to each copy
 * @param: boolean	clearInputs - Option to clear each copies text input fields or textarea
 * 
 */

(function($) {

	$.fn.relCopy = function(options) {
		var settings = jQuery.extend({
			excludeSelector: ".exclude",
			emptySelector: ".empty",
			copyClass: "copy",
			append: '',
			clearInputs: true,
			limit: 0 // 0 = unlimited
		}, options);
		
		settings.limit = parseInt(settings.limit);
		
		// loop each element
		this.each(function() {
			
			// set click action
			$(this).click(function(){
				var rel = $(this).attr('rel'); // rel in jquery selector format				
				var counter = $(rel).length;
				
				// stop limit
				if (settings.limit != 0 && counter >= settings.limit){
					return false;
				};
				
				var master = $(rel+":first");
				var parent = $(master).parent();						
				var clone = $(master).clone(true).addClass(settings.copyClass+counter).append(settings.append);
				
				//Remove Elements with excludeSelector
				if (settings.excludeSelector){
					$(clone).find(settings.excludeSelector).remove();
				};
				
				//Empty Elements with emptySelector
				if (settings.emptySelector){
					$(clone).find(settings.emptySelector).empty();
				};								
				
				// Increment Clone IDs
				if ( $(clone).attr('id') ){
					var newid = $(clone).attr('id') + (counter +1);
					$(clone).attr('id', newid);
				};
				
				// Increment Clone Children IDs
				$(clone).find('[id]').each(function(){
					var newid = $(this).attr('id') + (counter +1);
					$(this).attr('id', newid);
				});
				
				//Clear Inputs/Textarea
				if (settings.clearInputs){
					$(clone).find('input, textarea').each(function(){
						$(this).val("");						  
					});					
				};
				
				$(parent).find(rel+':last').after(clone);
				
				return false;
				
			}); // end click action
			
		}); //end each loop
		
		return this; // return to jQuery
	};
	
})(jQuery);



$(document).ready(function(){
	// top nav rollover
	$(".rollover").hover(
	 function()
	 {
		 var imgRO = $("#topNavImgRO").attr("value");
		 var theImgOn = imgRO + "/images/" + $(this).attr("id")+ "_on.jpg";
		 // alert(theImgOn);
	  	 $("#topNav").attr("src", theImgOn);
	 },
	 function()
	 {
	  	 var theImgOff = $("#topNavImg").attr("value");
		 //alert(theImgOff);
	  	 $("#topNav").attr("src", theImgOff);
	 }
	);
	//sidebar rollover
	$(".rolloversb").hover(
	 function()
	 {
	  this.src = this.src.replace("_off","_on");
	 },
	 function()
	 {
	  this.src = this.src.replace("_on","_off")	;
	 }
	);
	// cast for project
	if(document.getElementById("castLink")){
		$('#castLink').click(function(){$('#castForProject').toggleFade('Fast');});	
	}
    // new project
	if(document.getElementById("projectLink")){
		$('#projectLink').click(function(){$('#newProject').toggleFade('Fast');});	
	}
	// search form show/hide options
	if(document.getElementById("talent_type_both")){
		$('#talent_type_both').click(function(){
			$('.searchItem').fadeOut('Fast');
			$('#searchName').fadeIn('Fast');
			$('#searchCamera').fadeIn('Fast');
			$('#searchVoice').fadeIn('Fast');
			$('#btnFindTalent').fadeIn('Fast');
		});	
	}
	if(document.getElementById("talent_type_voice")){
		$('#talent_type_voice').click(function(){
			$('.searchItem').fadeOut('Fast');
			$('#searchName').fadeIn('Fast');
			$('#searchVoice').fadeIn('Fast');
			$('#btnFindTalent').fadeIn('Fast');
		});	
	}
	if(document.getElementById("talent_type_camera")){
		$('#talent_type_camera').click(function(){
			$('.searchItem').fadeOut('Fast');
			$('#searchName').fadeIn('Fast');
			$('#searchCamera').fadeIn('Fast');
			$('#btnFindTalent').fadeIn('Fast');
		});	
	}
	// clone the add role field
	//$('#btnAddRole').click(
	//function() {
     //   $("#role_nameAdd").clone().appendTo('#moreRoles');	
     // }
    //);

	$("#btnAddRole").click(function(){
		var yourclass=".makeMore"; //The class you have used in your form
		var clonecount = $(yourclass).length; //how many clones do we already have?
		var newid = Number(clonecount) + 1; //Id of the new clone
		$(yourclass+":first").fieldclone({//Clone the original elelement
		newid_: newid, //Id of the new clone, (you can pass your own if you want)
		target_: $("#otherRoles"), //where do we insert the clone? (target element)
		insert_: "append", //where do we insert the clone? (after/before/append/prepend...)
		limit_: 20 //Maximum Number of Clones
		});
		return false;

	});
	// if there is a cookie, set the id to display:block
	var cookieSet = $.cookie('NUTSopenProject');
	
	
	if (cookieSet != null && document.getElementById("prepMyProject")){
			iconSet = cookieSet.replace("Link","Icon");
			cookieSet = cookieSet.replace("Link","");
			//alert(iconSet);
			cookieSet = "#"+cookieSet;
			$(cookieSet).fadeIn('Fast');
			iconSet = "#"+iconSet;
			// only set it if the cookied div is available -- can happen if a project is opened then the admin moves to a different user
			if(document.getElementById(iconSet)){
				var iconImg = $(iconSet).attr("src").replace("closed","open");
				$(iconSet).attr("src", iconImg);
			}
	}
	$('div.itemLink').click(
    function() {
		//reset all the displays and the cookie
		$('.projectDetails').fadeOut('fast');
		$.cookie('NUTSopenProject',null);
		//var opened = $('.iconClose').attr("src");
		//alert(opened);
		var iconImgD = $('.iconClose').attr("src").replace("open","closed");
		$('.iconClose').attr("src", iconImgD);
		
        // set or delete cookie to save which project is open
		var projDiv = $(this).attr("id");
		var projIDArray = projDiv.split("_");
		var projID = projIDArray[1];
		
		//alert(projID);
		$.cookie('NUTSopenProject',projDiv,{ path: '/' });
		projDivD = "#" + projDiv.replace("Link","");
		//alert(projDivD + " " +$(projDivD).is(":visible"));	
		var projIcon = "#" + projDiv.replace("Link","Icon");
		var iconImgC = $(projIcon).attr("src");
		if($(projDivD).is(":visible")){
			var iconImg = $(projIcon).attr("src").replace("open","closed");
			$.cookie('NUTSopenProject',null);
		}
		else{
			var iconImg = $(projIcon).attr("src").replace("closed","open");
			$.cookie('NUTSopenProject',projDiv,{ path: '/' });
		}
		// set the icon to display
		//alert(iconImgC.indexOf("open"));
		//if(iconImgC.indexOf("open") < 0){
		//	var iconImg = $(projID).attr("src").replace("open","closed");
		//}
		//else{
			//var iconImg = $(projIcon).attr("src").replace("closed","open");
		//}
		
		$(projIcon).attr("src", iconImg);

		$(this).next().toggleFade('fast');	
		
      }
    );
	$('div.itemLink2').click(
    function() {
        $(this).next().toggleFade('fast');	
      }
    );
	$('div.itemLinkBtn').click(
    function() {
        $(this).next().toggleFade('fast');	
      }
    );
    // faq open/close
	$('ol.faq li h3').click(
    function() {
		$('.answer').fadeOut('slow');
        $(this).next().fadeIn('fast');	
      }
    );
	
	// voice over play
	$("a.iframe").fancybox({
		'autoscale'			: 	true,
		'cyclic'				:   false,
		'overlayShow'		:	true,
		'scrolling'				: 'no',
		'overlayOpacity'	:	.7	
	});
	
});
