/* 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);
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
function AddTag(tagID,projID){
	var result = "Tag_ID=" + tagID + "&Project_ID=" + projID;
	$.ajax({
		type: "POST",
		url: "system/admin_add_project_tag.cfm",
		data: result,
		success: function(msg){
				$("#currentTagsList").load('system/admin_show_project_tag.cfm?Project_ID=' + projID);
			
			}
	});

}

function removeTag(tagID,projID){
	var result = "Tag_ID=" + tagID + "&Project_ID=" + projID;
	//alert(theID);
	$.ajax({
		type: "POST",
		url: "system/admin_remove_tag.cfm",
		data: result,
		success: function(msg){
					$("#currentTagsList").load('system/admin_show_project_tag.cfm?Project_ID=' + projID);
			
			}
	});
}
$(document).ready(function(){

	
	if (document.getElementById("splashWrapper")){
		// hide the video div
		$('#videoWrapper').css('display', 'none'); 
		$('#featuredText').css('display', 'none'); 
		// fade in step by step
		$("#splash0").idle(100).fadeIn('fast');
		$("#splash1").idle(500).fadeIn('fast');
		$("#splash2").idle(1000).fadeIn('fast');
		$("#splash3").idle(1500).fadeIn('fast');
		$("#splash4").idle(2000).fadeIn('fast');
		// fade out
		//$("#splashStepWrapper").idle(3000).fadeOut('fast');
		// tagline
		$("#splash5").idle(3500).fadeIn('fast');
		// splash
		
		$("#splashWrapper").idle(5500).fadeOut('fast');
		// show the video div
		$('#videoWrapper').idle(5500).fadeIn();
		$('#featuredText').idle(5500).fadeIn();
	 }
	if (document.getElementById("loginDiv")){
		$('#custLogin').click(
			function() {$('#loginDiv').slideToggle('fast'); });
	}
	
	// messagebox 
	if(document.getElementById("messageBox")){
		// fade out
		$('#messageBox').idle(5000).fadeOut('fast');
	}
	
	// project photos for admin
	if (document.getElementById("managePhotos")){
		$('#managePhotosLink').click(
			function() {$('#managePhotos').slideToggle('fast'); });
	}
	
	// project gallery/lightbox
	if (document.getElementById("projectImgGallery")){
		jQuery(function($) {

		$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
		
		$('ul.gallery_demo').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
 				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
		});
	}
	// home slide show
	function onAfter() { 
		//$('#homeSliderText').html(this.title); 
	}
	function onBefore() { 
		$(this).css('visibility', 'visible'); 
	}
	if (document.getElementById("featuredWrapper")){
		$('#featuredImgs').cycle({ 
			fx:    'fade', 
			timeout:  5000,
			slideExpr: '.featuredImg',
			prevNextEvent: 'click',
			next:   '#next',  // selector for element to use as click trigger for next slide 
    		prev:   '#prev',  // selector for element to use as click trigger for previous slide 
			fit: 1,
 			before: onBefore,
			after: onAfter
		});
		// pause show
		$('#sliderBtnPause').click(function() { 
			$('#featuredImgs').cycle('pause'); 
		});
		// resume show
		$('#sliderBtnGo').click(function () {
			$('#featuredImgs').cycle('resume');
		});
	}
	
	$('.navWrapper').hover(
			function() { $('.subNavWrapper', this).fadeIn('fast'); },
			function() { $('.subNavWrapper', this).fadeOut('slow'); });
	

	// portfolio slide show
	if (document.getElementById("portfolioWrapper")){
		$('#portfolioImgs').cycle({ 
			fx:    'fade', 
			timeout:  5000,
			slideExpr: '.portfolioImg',
			prevNextEvent: 'click',
			next:   '#next',  // selector for element to use as click trigger for next slide 
    		prev:   '#prev',  // selector for element to use as click trigger for previous slide 
			fit: 1,
 			before: onBefore,
			after: onAfter
		});
		// pause show
		$('#sliderBtnPause').click(function() { 
			$('#portfolioImgs').cycle('pause'); 
		});
		// resume show
		$('#sliderBtnGo').click(function () {
			$('#portfolioImgs').cycle('resume');
		});
	}
	
	if(document.getElementById("tagDiv")){
		$('#tagLink').click(
			function() {$('#tagDiv').toggleFade('fast'); });
	}
	// add tag 
	
	
	$("#btnAddTag").click(function() {  
		var result = "Tag_Name=" + document.theForm.Tag_Name.value;
		$.ajax({
			type: "POST",
			url: "system/admin_add_tag.cfm",
			data: result,
			success: function(msg){
				var projectID = document.theForm.Project_ID.value;
				$("#tagsAvailList").load('system/admin_show_tag.cfm?Project_ID=' + projectID);
				$('#tagDiv').idle(300).fadeOut('fast');
				
				}
		});
	});
	
	// add tag to project
	$(".tagID").click(function() { 
		var theID = $(this).attr("id");
		theID = theID.replace("tagID_","");
		var result = "Tag_ID=" + theID + "&Project_ID=" + document.theForm.Project_ID.value;
		$.ajax({
			type: "POST",
			url: "system/admin_add_project_tag.cfm",
			data: result,
			success: function(msg){
						var projectID = document.theForm.Project_ID.value;
						$("#currentTagsList").load('system/admin_show_project_tag.cfm?Project_ID=' + projectID);
				
				}
		});
	});				
	// remove tag from project
	$(".removetagID").click(function() { 
									 
		var theID = $(this).attr("id");
		theID = theID.replace("removetagID_","");
		var result = "Tag_ID=" + theID + "&Project_ID=" + document.theForm.Project_ID.value;
		//alert(theID);
		$.ajax({
			type: "POST",
			url: "system/admin_remove_tag.cfm",
			data: result,
			success: function(msg){
						var projectID = document.theForm.Project_ID.value;
						$("#currentTagsList").load('system/admin_show_project_tag.cfm?Project_ID=' + projectID);
				
				}
		});
	});	
	
});
