//.log PLUGIN
jQuery.fn.log = function (msg) {
	//console.log("%s: %o", msg, this);
	//return this;
};

//browser detection
$(document).ready(function(){var a=navigator.userAgent.toLowerCase();$.browser.chrome=/chrome/.test(navigator.userAgent.toLowerCase());if($.browser.msie){$('body').addClass('browserIE');$('body').addClass('browserIE'+$.browser.version.substring(0,1))}if($.browser.chrome){$('body').addClass('browserChrome');a=a.substring(a.indexOf('chrome/')+7);a=a.substring(0,1);$('body').addClass('browserChrome'+a);$.browser.safari=false}if($.browser.safari){$('body').addClass('browserSafari');a=a.substring(a.indexOf('version/')+8);a=a.substring(0,1);$('body').addClass('browserSafari'+a)}if($.browser.mozilla){if(navigator.userAgent.toLowerCase().indexOf('firefox')!=-1){$('body').addClass('browserFirefox');a=a.substring(a.indexOf('firefox/')+8);a=a.substring(0,1);$('body').addClass('browserFirefox'+a)}else{$('body').addClass('browserMozilla')}}if($.browser.opera){$('body').addClass('browserOpera')}});

//randon number
function randNum() {
       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

// EXTEND FADE TOOGLE FUNCTION
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 

// FORM HELPER
this.helper = function(){
	$(".helper").focus(function(){
		var helpText = $(this).siblings('div.helpText').children('div').text();
		//alert('working');
		
		var pos = $(this).position();
		var fieldWidth = $(this).width();
		
		if (helpText == '') {
			helpText = $(this).parent().siblings('div.helpText').children('div').text();
		}
		//
		if (helpText != ''){ //show helptext only if something to show
		//alert(helpText);
		$("body").append("<div id='helper'>"+ helpText +"</div>");
		//$("#helper").addClass('bamboo');
		$("#helper")
			.css("position","absolute")
			.css("top",(pos.top-27) + "px")
			.css("left",(pos.left + fieldWidth + 18) + "px")	
			.css("z-index","1000")
		}
    });
	$(".helper").blur(function(){		
		$("#helper").remove();
    });			
};

// TOOLTIPS
this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 30;
		yOffset = 0;		
		// these 2 variable determine popup's distance from the cursor 
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

// FOLDERS
this.folders = function(){	
	$("#folders li ul").hide();
	$("#folders li:has(ul) > a").toggle(function(){
			$(this).parent().addClass("open");
			var el = $(this).parent();
			$(">ul ",el).show("fast");
		},
		function(){
			$(this).parent().removeClass("open");
			var el = $(this).parent();
			$(">ul ",el).hide("fast");
	});
        $("#folders li.open ul").toggle();
};

// FILTERS
this.filters = function(){	
	$("#tabs a").click(function() {
		var tgt = $(this).attr("id");
		if (tgt != "addthis"){
		$(".filters").hide();
		$("#tabs a.active").removeClass("active");
		$("#"+tgt+"_filters").show();
		$(this).addClass("active");
		return false;
		}
	});

};

this.togglebn = function(){
	//FOR REDESIGNED FORM
	$(".toggleBar").click(function () {	
		var tgt = $(this).attr("id");
		$("#"+tgt+"Table").toggle();
		if ($("#"+tgt+"Table").css("display")=="none"){
			$(this).removeClass("toggleon");
		} else {
			$(this).addClass("toggleon");
		}
		return false;
	});
};

/* MENU FIX */
function mainmenu(){
$("#mainnav li").hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
};

/* SUBNAV CURRENT PAGE */
function subnav(){
	$("#subnav li.on").not(":has(ul li.on)").addClass("current").children("a.on").addClass("current");
};

// starting the script on page load
$(document).ready(function(){
	subnav();
	tooltip();
	folders();
	filters();
	togglebn();
	mainmenu();
	addcontact();
	addCaptions();


});

//LOGIN LABELS
function initOverLabels () {
  if (!document.getElementById) return;  	

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // LABELs with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
	
    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      }
	  

      // Change the applied class to hover the label 
      // over the form field.
	  
      labels[i].className = 'overlabel-apply';
	  	  

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to LABEL elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.display = (hide) ? 'none' : 'inline';
      return true;
    }
  }
}

window.onload = function () {
  setTimeout(initOverLabels, 50);
};


/*SUBMIT POPUP*/

this.addcontact = function(){
$(".submit_addcontact").click(function(){ 
		$("#send_message").hide('fast');
		$(".pleasewait").show('fast');
		window.setTimeout(function(){document.send_contact.submit();},3000);
		//document.send_contact.submit();
		//alert ("works");
		return false;
	});
};


//TOGGLE VAL
(function($) {
	$.fn.toggleVal = function(theOptions) {
		theOptions = $.extend({
			focusClass: "focused", // class during focus
			changedClass: "changed", // class after focus
			populateFrom: "default", // choose from: default, label, or alt
			removeLabels: false // remove labels associated with the fields
		}, theOptions);
		
		return this.each(function() {
			// define our variables
			var defText = "";
			
			// let's populate the text, if not default
			switch(theOptions.populateFrom) {
				case "alt":
					defText = $(this).attr("alt");
					$(this).val(defText);
					break
				case "label":
					defText = $("label[for='" + $(this).attr("id") + "']").text();
					$(this).val(defText);
					break
				default:
					defText = $(this).val();
			}
			
			// let's give this field a special class, so we can identify it later
			$(this).addClass("toggleval");
			
			// now that fields are populated, let's remove the labels if applicable
			if(theOptions.removeLabels == true) {$("label[for='" + $(this).attr("id") + "']").remove(); }
			
			
			// on to the good stuff... the focus and blur actions
			$(this).focus(function() {
				if($(this).val() == defText) { $(this).val(""); }
								
				if(this.id == "password") {
					//alert('wow');
					var oldPassword = $(this).clone();
					$(this).replaceWith("<input type='password' id='newpassword' class='text' name='password' title='Password' value='' tabindex='2' />");
					//$("#newpassword").addClass(theOptions.focusClass).removeClass(theOptions.changedClass);
					$("#newpassword").toggleVal({populateFrom: "label",removeLabels: true});
					
					};
				
				// add the focusClass, remove changedClass
				$(this).addClass(theOptions.focusClass).removeClass(theOptions.changedClass);
								
			}).blur(function() {
				
				if(($(this).val() == "")&&(this.id == "newpassword")) {
					//alert("test");
					$("#newpassword").replaceWith("<label for='password'>Password</label><input id='password' class='text' type='text' name='password' title='Password' tabindex='2' />");
					$("#password").toggleVal({populateFrom: "label",removeLabels: true});
					};
				
				
				if($(this).val() == "") { $(this).val(defText); }
				
				// remove focusClass, add changedClass if, well, different
				$(this).removeClass(theOptions.focusClass);
				if($(this).val() != defText) { $(this).addClass(theOptions.changedClass); }
					else { $(this).removeClass(theOptions.changedClass); }
			});
		});
	};
})(jQuery);


/* IMAGE CAPTIONS*/

function addCaptions (){
$('.caption_left').each( 
	function() {
		var divWidth = this.width + 'px';
		var wrapper = '<div class="caption_left">' + '</div>'; //wrap in div
		$(this).removeClass();
		$(this).wrap(wrapper); 
		$('div.caption_left').css("width", divWidth);
		$(this).after('<cite>'+this.title+'</cite>');//add caption from title
	}
);
//
$('.caption_right').each( 
	function() {
		var divWidth = this.width + 'px';
		var wrapper = '<div class="caption_right">' + '</div>'; //wrap in div
		$(this).removeClass();
		$(this).wrap(wrapper); 
		$('div.caption_left').css("width", divWidth);
		$(this).after('<cite>'+this.title+'</cite>');//add caption from title
	}
); 
}
