var defaultSearchBoxContent = 'Enter keywords to search for';
var defaultBreadCrumbsTop = 0;
var defaultBottomSliderTop = 0;
var commentSliderScrollTop = 0;

var defaultLogin = '';
var defaultPassword = '';

var opened = 'true';

jQuery(document).ready(function() {
	
	initVisualRotate();	
	initMenu();	
	initTopSlider();	
	initBottomSlider();
	initCommentSlider();
	initExternalLinks();
	initSearchBox();
	//initBookmark();

	initBreadCrumbs();
	
	initCommentTabsID();
	
	initFormInputs();

	initRowsHighlighting();
	initProductHovers();
	
	initContactFields();
	initSidebarDropdowns();
	
	initBackToTop();
	initLocalLinks();
	//initLoginBox();
	initAjaxSorting();
	
	
	setTimeout(function(){ initTestimonials(1); }, 10000);

	
	/*jQuery.each(preloadImages, function(index, item){
		preloaded[index] = jQuery.create('img', {
		    'src': item
		});
	});*/
        
        initAboutBio();
		
});

function initAboutBio(){
   jQuery('.people .heading').hover(function(){
      user = jQuery(this).attr('id');
      jQuery(this).parent('li').addClass('hover');
   }, function(){
      jQuery(this).parent('li').removeClass('hover');
   });
   
   jQuery('.people .heading').click(function(){
      user = jQuery(this).attr('id');
      jQuery('.about-bio').height(jQuery('.about-bio li[class!=hidden]').height());
      jQuery('.about-bio li').each(function(){
         jQuery(this).addClass('hidden');
      });
      jQuery('.people li').each(function(){
         jQuery(this).removeClass('active');
      });
      jQuery(this).parent('li').addClass('active');
      jQuery('.about-bio li[id=' + user + '-text]').width(735);
      var new_height = jQuery('.about-bio li[id=' + user + '-text]').height();
      jQuery('.about-bio').animate({height: new_height + "px"}, 1000);

      jQuery('.about-bio li[id=' + user + '-text]').removeClass('hidden');
   });
}

function isUrl(s) {
	var regexp = new RegExp();
  regexp.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+jQuery");
	return regexp.test(s);
}

function initVisualRotate()
{
	jQuery('div.visual img').attr('id','visual');
	if (jQuery('#visual').attr('id') != undefined)
	{
		var currentImage = 0;
		var nextImage = 0;
		
		var image_cookie = jQuery.cookie('current-image');
		if (isNaN(parseInt(image_cookie)))
		{
			currentImage = 1;
		}
		else
		{
			currentImage = image_cookie;
		}
		
		if (currentImage >= 3)
		{
			nextImage = 1;
		}
		else
		{
			nextImage = parseInt(currentImage) + 1;
		}
		jQuery('#visual').css('height','289px');
		jQuery.cookie('current-image',nextImage);
	}
}

function initMenu()
{
	var submenuDivs = jQuery('div.sub-menu');
	submenuDivs.css('width',jQuery(window).width() + 'px');
	
	var submenuLis = jQuery('ul#nav > li');
	var lis_count = jQuery(submenuLis).length;	
	jQuery.each(submenuLis, function(index, item) {			
		jQuery(item).hover(
      function () {
        jQuery(this).addClass('hover');		
      }, 
      function () {
        jQuery(this).removeClass('hover');	  
      }
    );
	});
}

function initTopSlider()
{
	jQuery('#products-detail').css('visibility', 'visible');
	jQuery('#products-detail div.content').attr('id','products-slider');
	jQuery('ul.thumbs').attr('id','thumbs-slider');
	if (jQuery('#products-slider').attr('id') != undefined)
	{
		if (jQuery('#thumbs-slider').hasClass('hidden-default')) {
			jQuery('#thumbs-slider').css('display','none');
			jQuery('#thumbs-slider').removeClass('hidden-default');
		}

//Max-min bw pages
		var top_slider_state = jQuery.cookie("topSlider");
		if (top_slider_state == "on") {
			if (jQuery('div.t-box a').hasClass('maximize'))
			{
				//jQuery('#products-slider').attr('_height',211);
				if (jQuery('#products-slider').hasClass('no-pager')){
				//	jQuery('#products-slider').attr('_height',175);
				}
				/* Lite slider */
				if (jQuery('#products-slider').hasClass('lite')){
				//	jQuery('#products-slider').attr('_height',90);
				}
				if (jQuery('#thumbs-slider').attr('id') != undefined)
				{
					sliderSmoothHideBlock('thumbs-slider');
				}
				sliderSmoothShowBlock('products-slider');
				jQuery.cookie('topSlider', 'on', {expires: 30, path: '/'});
				jQuery('div.t-box a').removeClass('maximize');
				jQuery('div.t-box a').addClass('minimize');
			}			
			//return false;
		};

//Maximize-minimize		
		jQuery('div.t-box a').click(function() {
			if (jQuery(this).hasClass('minimize'))
			{
				if (jQuery('#products-slider').attr('id') != undefined)
				{
					//jQuery('#thumbs-slider').attr('_height', jQuery('#thumbs-slider').attr('_open_height') );
					sliderSmoothShowBlock('thumbs-slider');
				}
				sliderSmoothHideBlock('products-slider');
				jQuery.cookie('topSlider', 'off', {expires: 30, path: '/'});

				jQuery(this).removeClass('minimize');
				jQuery(this).addClass('maximize');
			}
			else
			{
				//jQuery('#products-slider').attr('_height',211);
				if (jQuery('#products-slider').hasClass('no-pager')){
					//jQuery('#products-slider').attr('_height',175);
				}
				/* Lite slider */
				if (jQuery('#products-slider').hasClass('lite')){
				//	jQuery('#products-slider').attr('_height',90);
				}
				if (jQuery('#thumbs-slider').attr('id') != undefined)
				{
					//jQuery('#thumbs-slider').attr('_open_height', jQuery('#thumbs-slider').height() );
					sliderSmoothHideBlock('thumbs-slider');
				}
				sliderSmoothShowBlock('products-slider');
				jQuery.cookie('topSlider', 'on', {expires: 30, path: '/'});

				jQuery(this).removeClass('maximize');
				jQuery(this).addClass('minimize');
			}			
			return false;
		});
		
	}
}

function sliderSmoothHideBlock(id,useScroll,scrollToValue)
{
//alert('hide '+id);
	jQuery('#'+id).animate({opacity:0}, 0, 'linear',
		function() {
		//	jQuery('#'+id).css('min-height','0px');
			jQuery('#'+id).css('overflow','hidden');
			
			if (useScroll)
			{				
				jQuery(window).scrollTo({top:scrollToValue}, 500, {onAfter:function() {
						jQuery('#'+id).css('visibility','hidden');
						jQuery('#'+id).css('display','none');  ;
					}}
				);
			}
			else
			{
				jQuery('#'+id).css('visibility','hidden');
				jQuery('#'+id).css('display','none'); 
			}
			//jQuery('#'+id).css('visibility','hidden');
		}
	);
}

function sliderSmoothShowBlock(id,useScroll,scrollToValue)
{
	//alert('show '+id);
	
	jQuery('div.breadcrumbs').attr('id','bread-crumbs-div');
	if (jQuery('#bread-crumbs-div').attr('id') != undefined)
	{
		var breadcrumbsHeight = jQuery('#bread-crumbs-div').height();
	}
	else
	{
		var breadcrumbsHeight = 0;
	}	
	jQuery('#'+id).css('opacity','0');
	jQuery('#'+id).css('visibility','visible');
	jQuery('#'+id).css('display','block');
		
			jQuery('#'+id).css('overflow','visible');
			if (useScroll)
			{
				jQuery(window).scrollTo({top:scrollToValue - breadcrumbsHeight}, 500, {onAfter:function() {
						jQuery('#'+id).animate({opacity:1}, 500);
					}}
				);
			}
			else
			{
				jQuery('#'+id).animate({opacity:1}, 500);
			}	
		
	
}

function initBottomSlider()
{
	jQuery('#exploring div.content').attr('id','exploring-slider');
	if (jQuery('#exploring-slider').attr('id') != undefined)
	{
		jQuery('#exploring-slider').css('display','none');
		hideBlock('exploring-slider');
		
		jQuery('a.start').click(function() {
			if (jQuery(this).hasClass('stop'))
			{
				var coords = jQuery(this).getCoordinates();
				var footerCoords = jQuery('#footer').getCoordinates();
				var newTop = coords.bottom - jQuery(window).height() + jQuery('#footer').height();
				sliderSmoothHideBlock('exploring-slider',true,newTop);
				jQuery.cookie('bottomSlider', 'off', {expires: 30, path: '/'});
				jQuery(this).removeClass('stop');
				jQuery(this).addClass('start');
			}
			else
			{	
				var coords = jQuery(this).getCoordinates();
				jQuery('#exploring-slider').attr('_height',331);
				sliderSmoothShowBlock('exploring-slider',true, coords.top);
				jQuery.cookie('bottomSlider', 'on', {expires: 30, path: '/'});
				jQuery(this).removeClass('start');
				jQuery(this).addClass('stop');
			}
			return false;
		});
		
	}
}

function hideBlock(id)
{
	jQuery('#'+id).css('opacity','0');
}

function initBreadCrumbs()
{
	jQuery('div.breadcrumbs').attr('id','bread-crumbs-div');
	if (jQuery('#bread-crumbs-div').attr('id') != undefined)
	{
		defaultBreadCrumbsTop = jQuery('#header').height() + 3;
	
		jQuery(window).scroll(function() {
			scrollBreadCrumbs();
		});
		
		jQuery(window).ready(function() {
			scrollBreadCrumbs();
		});
	}
}

function scrollBreadCrumbs()
{
	if (jQuery('#bread-crumbs-div').attr('id') != undefined)
	{
		var scrollTop = jQuery(window).scrollTop();
		
		if (jQuery.browser.msie && jQuery.browser.version == 6)
		{
			if (scrollTop < defaultBreadCrumbsTop)
			{
				//scrollTop = defaultBreadCrumbsTop;
				jQuery('#bread-crumbs-div').css('position','absolute');
				jQuery('#bread-crumbs-div').css('top',defaultBreadCrumbsTop + 'px');
			}
			else
			{
				jQuery('#bread-crumbs-div').css('position','absolute');
				jQuery('#bread-crumbs-div').css('top',scrollTop + 'px');
			}
		}
		else
		{
			if (scrollTop < defaultBreadCrumbsTop)
			{
				//scrollTop = defaultBreadCrumbsTop;
				jQuery('#bread-crumbs-div').css('position','absolute');
				jQuery('#bread-crumbs-div').css('top',defaultBreadCrumbsTop + 'px');
			}
			else
			{
				jQuery('#bread-crumbs-div').css('position','fixed');
				jQuery('#bread-crumbs-div').css('top','0px');
			}
		}
		//jQuery('#bread-crumbs-div').css('top',scrollTop + 'px');
	}
}

function initCommentSlider()
{
	jQuery('#respond div.form-container').attr('id','comment-slider');
	if (jQuery('#comment-slider').attr('id') != undefined)
	{
		jQuery('div.maxmindiv a').click(function() {
			if(jQuery(this).hasClass('minimize'))
			{
				var coords = jQuery('#comment-slider').getCoordinates();
				sliderSmoothHideBlock('comment-slider', true, coords.top - jQuery(window).height());
				jQuery.cookie('bottomSlider', 'off', {expires: 30, path: '/'});
				jQuery(this).removeClass('minimize');
				jQuery(this).addClass('maximize');
			}
			else
			{
				var coords = jQuery('#respond').getCoordinates();
				jQuery('#comment-slider').attr('_height',337);
				sliderSmoothShowBlock('comment-slider', true, coords.top);
				jQuery.cookie('bottomSlider', 'on', {expires: 30, path: '/'});
				jQuery(this).removeClass('maximize');
				jQuery(this).addClass('minimize');
			}
			return false;
		});
	}
}

var initExternalLinks = function()
{
	jQuery('a[rel*="external"]').attr('target','_blank');
	
	var allLinks = jQuery('#pagewidth a[rel="external"]');
	jQuery.each(allLinks, function(index, item) {
		if (jQuery(item).children().length == 0)
		{
			jQuery(item).addClass('out');
		}
	});
	
	jQuery('a[rel*="popup"]').click(function(link) {
		window.open(jQuery(this).attr('href'),'_blank','resizable=yes,titlebar=no,location=no,status=no,menubar=no,toolbar=no');
		return false;
	});
	
}

function initSearchBox()
{
	jQuery('input.txt').DefaultValue(defaultSearchBoxContent);
}

var initBookmark = function()
{
	if (jQuery('#fader').attr('id') != undefined)
	{
		jQuery('a.bookmark').click(function() {
			jQuery('#fader').css('width',jQuery(document).width() + 'px');
			jQuery('#fader').css('height',jQuery(document).height() + 'px');
			jQuery('#bookmark').css('display','block');
			jQuery('#fader').css('display','block');
			
			var newTop = jQuery(window).scrollTop() + jQuery(window).height() / 2 - jQuery('#bookmark').height() / 2;
			var newLeft = jQuery(window).scrollLeft() + jQuery(window).width() / 2 - jQuery('#bookmark').width() / 2;
			
			if (newTop < 0 ) newTop = 0;
			jQuery('#bookmark').css('top',newTop);
			jQuery('#bookmark').css('left',newLeft);
			return false;
		});
	}	
	
	var buttons = jQuery('#bookmark div.submit input');
	jQuery.each(buttons, function(index, item) {
		if (jQuery(item).attr('alt') == 'cancel')
		{
			jQuery(item).click(function() {
				jQuery('#fader').css('display','none');
				return false;
			});
		}
		if (jQuery(item).attr('alt') == 'submit')
		{
			jQuery(item).click(function() {
				jQuery('#fader form').attr('id','bookmark-form');
				var inputs = jQuery('#fader form input');
				var bookmarkLink = '';
				jQuery.each(inputs, function(index, item) {
					if (jQuery(item).attr('checked') == true)
					{
						bookmarkLink = bookmarkLinks[bookmarkIds.indexOf(jQuery(item).attr('id'))];
					}
				});
				var fakeForm = jQuery.create('form', {
					'styles': {
						'display': 'none'
					},
					'method': 'post',
					'action': bookmarkLink,
					'target': '_blank'
				});
				
				jQuery(fakeForm).insertAfter('#bookmark-form');
				jQuery('#fader').css('display','none');
				jQuery(fakeForm).submit();
				return false;
			});
		}
	});
}

function initContactFields(){
	jQuery(".contact input.field").each(function() {
		var def = jQuery(".contact label[for='" + jQuery(this).attr('id') + "']").text();
		jQuery(this).focus(function() {
			if (jQuery(this).val() == def) {
				jQuery(this).val('');
			};
		}).blur(function() {
			if (jQuery(this).val() == '') {
				jQuery(this).val(def);
			};
		}); 
	});
	
	jQuery(".contact textarea").each(function() {
	 var def = jQuery(".contact label[for='" + jQuery(this).attr('id') + "']").text();
	 jQuery(this).focus(function() {
		 if(jQuery(this).val()== def) {
			 	jQuery(this).val('');
			 };
			 }).blur(function() {
				 if (jQuery(this).val() == '') {
					 jQuery(this).val(def);
					 };
			}); 
	});
	
	jQuery('#submit-form').click(function() {
		var inputs = jQuery(".contact input.field");
		var currid = ''; var currvalue = '';
		jQuery.each(inputs, function() {
			currid = jQuery(this).attr('id');
			currvalue = jQuery(".contact label[for='" + currid + "']").html();
			if (jQuery(this).val() == currvalue) jQuery(this).val('');
		});
		currvalue = jQuery(".contact label[for='your-message']").html();
		if (jQuery('#your-message').val() == currvalue) jQuery('#your-message').val('');
	});
	
	jQuery('#clear-form').click(function() {
		eraseContactFields();
	});
}

function eraseContactFields(){
	var inputs = jQuery(".contact input.field");
	var currid = ''; var currvalue = '';
	jQuery.each(inputs, function() {
		currid = jQuery(this).attr('id');
		currvalue = jQuery(".contact label[for='" + currid + "']").html();
		jQuery(this).val(currvalue);
	});
	currvalue = jQuery(".contact label[for='your-message']").html();
	if (jQuery('#your-message').val() == '') jQuery('#your-message').val(currvalue);
}

function repairContactFields(){
	var inputs = jQuery(".contact input.field");
	var currid = ''; var currvalue = '';
	jQuery.each(inputs, function() {
		if (jQuery(this).val() == '') {
			currid = jQuery(this).attr('id');
			currvalue = jQuery(".contact label[for='" + currid + "']").html();
			jQuery(this).val(currvalue);
		}
	});
	if (jQuery('#your-message').val() == '') {
		currvalue = jQuery(".contact label[for='your-message']").html();
		if (jQuery('#your-message').val() == '') jQuery('#your-message').val(currvalue);
	}
}

function initFormInputs()
{
	var inputs = jQuery('form.comment-form input, form.comment-form textarea, form.order input, form.order textarea');
	jQuery.each(inputs, function(index, item) {
		jQuery(item).focus(function() {
			jQuery(this).addClass('focus');
		});
		jQuery(item).blur(function() {
			jQuery(this).removeClass('focus');
		});
	});	
}

function initRowsHighlighting()
{
	var spans = jQuery('pre span');
	jQuery.each(spans, function(index, item) {
		jQuery(item).hover(
      function () {
        jQuery(this).addClass('hover');
      }, 
      function () {
        jQuery(this).removeClass('hover');
      }
    );
	});
}

function initProductHovers()
{
	var lis = jQuery('#pagewidth ul.list li, div.some-case');
	jQuery.each(lis, function(index, item) {
		jQuery(item).hover(
      function () {
        jQuery(this).addClass('hover');
      }, 
      function () {
        jQuery(this).removeClass('hover');
      }
    );
	});
}


function initBackToTop()
{
	var toTopLinks = jQuery('a.back-to-top');
	if (jQuery(toTopLinks).length > 0) {
		jQuery.each(toTopLinks, function(index, item){
			jQuery(item).click(function(){
				jQuery(window).scrollTo({top:0}, 500);
				return false;
			});
		});
	}
}

function initLocalLinks()
{
	var allLinks = jQuery('a');
	var location = window.location.href.match(/^[^#]*/)[0] + '#';
	jQuery.each(allLinks, function(index, link){
		if (link.href.indexOf(location) != 0) return;
		var anchor = link.href.substr(location.length);
		if (anchor && ( jQuery("#"+anchor).attr('id') != undefined || anchor == 'main')) addLocalLinkHandler(link, anchor);
	});
	
	var location = window.location.href.match(/^[^#]*/)[0];	
	var anchor = window.location.href.substr(location.length + 1);
	if (anchor && jQuery("#"+anchor).attr('id') != undefined ) {
		var coords = jQuery("#"+anchor).getCoordinates();
		if (coords) {
			if (jQuery('#bread-crumbs-div').attr('id') != undefined ) {
				var bcCoords = jQuery('#bread-crumbs-div').getCoordinates();
				jQuery(window).scrollTo({top: coords.top - jQuery('#bread-crumbs-div').height() - 5}, 500);
			} else {
				jQuery(window).scrollTo({top: coords.top}, 500);
			}
		}
	}
}

function addLocalLinkHandler(link,anchor)
{
	jQuery(link).click(function(){
		if (anchor == 'main') {
			jQuery(window).scrollTo({top: jQuery('#header').height() + 2}, 500);
			return false;
		} else {
			if (jQuery("#"+anchor).attr('id') != undefined)
			{
				var coords = jQuery("#"+anchor).getCoordinates();
				if (coords) {
					if (jQuery('#bread-crumbs-div').attr('id') != undefined ) {
						jQuery(window).scrollTo({top: coords.top - jQuery('#bread-crumbs-div').height() - 5}, 500);
					} else {
						jQuery(window).scrollTo({top: coords.top}, 500);
					}
				}
			}
			return false;
		}
	});
}

function initLoginBox()
{
	var login = jQuery('form.login-form input[name=login]');
	var passwd = jQuery('form.login-form input[name=password]');
	if (jQuery(login).attr('id') != undefined && jQuery(passwd).attr('id') != undefined)
	{
		defaultLogin = jQuery(login).attr('value');
		defaultPassword = jQuery(passwd).attr('value');
		
		jQuery(login).val('');
		jQuery(passwd).val('');

		jQuery(login).DefaultValue(defaultLogin);
		jQuery(passwd).DefaultValue(defaultPassword);
	}
}

function initAjaxSorting()
{
	
	var ajaxThumbs = jQuery('#ajax-thumbs');
	if (jQuery(ajaxThumbs).attr('id') != undefined)
	{
		
		jQuery('#products-detail').css('visibility', 'visible');
		
		jQuery('#sort-filter a.view_by_tag').click(function(e){
			e.preventDefault();
			jQuery.cookie('taxonomy', 'project_tags', {path: '/'});
			
			jQuery('.project_tags').show();
			jQuery('.project_desciplines').hide();
		});
		
		jQuery('#sort-filter a.view_by_discipl').click(function(e){
			e.preventDefault();
			jQuery.cookie('taxonomy', 'project_disciplines', {path: '/'});
			jQuery('.project_tags').hide();
			jQuery('.project_desciplines').show();
		});
		
		
		var sortFilterLinks = jQuery('.filter_c a');
		sortFilterLinks.click(function(link){
			
			jQuery('.filter_c a').removeClass('active');
			jQuery(this).addClass('active');
			
			
			var newHTML = '';
			jQuery('ul.thumbs').attr('id','thumbs-slider');
			if (jQuery('#thumbs-slider').attr('id') != undefined && jQuery('#thumbs-slider').css('visibility') == 'visible'){
				opened = 'false';
			} else if (jQuery('#thumbs-slider').attr('id') != undefined && jQuery('#thumbs-slider').css('visibility') == 'hidden') {
				opened = 'true';
			}
			
			
			
			//Slider state
			if (jQuery.cookie('topSlider') == 'on') { opened = 'true'; } else { opened = 'false'; }
			
			
			jQuery.ajax({
				type: "POST",
				url: jQuery(this).attr('href'),
				data: 'ajax=thumbs&opened='+opened, 
				success: function(req){
					
					jQuery(ajaxThumbs).html(req);
					jQuery('#products-detail div.content').attr('id','products-slider');
					jQuery('ul.thumbs').attr('id','thumbs-slider');
					var productDetails = jQuery('#products-detail');
					if (jQuery(productDetails).attr('id') != undefined)
					{
						jQuery('#products-detail').css({
							'visibility':'visible'
						});
					}
					
					
					initTopSlider();
					initHintPager();
					
					if (jQuery('#products-slider').attr('id') != undefined)
					{
						if (opened == 'false')
						{
							if (jQuery('#thumbs-slider').attr('id') != undefined)
							{
								//jQuery('#thumbs-slider').attr('_height',32);
								sliderShowBlock('thumbs-slider');
							}
							sliderHideBlock('products-slider');
							jQuery.cookie('topSlider', 'off', {expires: 30, path: '/'});

							jQuery('#min-max-button').removeClass('minimize');
							jQuery('#min-max-button').addClass('maximize');
						}
						else if (opened == 'true')
						{
							jQuery('#products-slider').attr('_height',211);
							if (jQuery('#products-slider').hasClass('no-pager')){
								//jQuery('#products-slider').attr('_height',175);
							}
							/* Lite slider */
							if (jQuery('#products-slider').hasClass('lite')){
								//jQuery('#products-slider').attr('_height',90);
							}
							if (jQuery('#thumbs-slider').attr('id') != undefined)
							{
								sliderHideBlock('thumbs-slider');
							}
							sliderShowBlock('products-slider');
							jQuery.cookie('topSlider', 'on', {expires: 30, path: '/'});
							jQuery('#min-max-button').removeClass('maximize');
							jQuery('#min-max-button').addClass('minimize');
						}
					}
				}
			});
			return false;
		});
	}
}

function sliderHideBlock(id)
{
	jQuery('#'+id).css({
	//	'height':'0',
	//	'min-height':'0',
		'overflow':'hidden',
		'opacity':'0',
		'visibility':'hidden',
		'display':'none'
	});
}

function sliderShowBlock(id)
{
	jQuery('#'+id).css({
		//'height':jQuery('#'+id).attr('_height') + 'px',
	//	'min-height':'0',
		'overflow':'visible',
		'opacity':'1',
		'visibility':'visible',
		'display':'block'
	});
}



/******************tools and extends**************/

jQuery.fn.extend({

  getCoordinates: function() {
	  coords = {};
		var coords = jQuery(this).offset()
		coords.bottom = coords.top + jQuery(this).height();
		coords.right = coords.left + jQuery(this).width();
		return coords; 
  },
  
  DefaultValue: function(text) {
  	return this.each(function(){
		//Make sure we're dealing with text-based form fields
		if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
			return;	
		//Store field reference
		var fld_current=this;	
		//Set value initially if none are specified
        if(this.value=='') {
			this.value=text;
		} else {
			//Other value exists - ignore
			return;
		}	
		//Remove values on focus
		jQuery(this).focus(function() {
			if(this.value==text || this.value=='') {
				this.value='';
			}
		});	
		//Place values back on blur
		jQuery(this).blur(function() {
			if(this.value==text || this.value=='') {
				this.value=text;
			}
		});	
		//Capture parent form submission
		//Remove field values that are still default
		jQuery(this).parents("form").each(function() {
			//Bind parent form submit
			jQuery(this).submit(function() {
				if(fld_current.value==text) {
					fld_current.value='';
				}
			});
		});
    }); 	
  },
  
  clearForm: function() {
	  return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form') {
		  return jQuery(':input',this).clearForm();
		}
		if (type == 'text' || type == 'password' || tag == 'textarea') {
		  this.value = '';
		}
		/*
		else if (type == 'checkbox' || type == 'radio')
		  this.checked = false;
		else if (tag == 'select')
		  this.selectedIndex = -1;
		*/
	  });
	}
	
});

jQuery.extend({
	create: function() {
    if (arguments.length == 0) return [];
    var args = arguments[0] || {}, elem = null, elements = null;
    var siblings = null;

    // In case someone passes in a null object,
    // assume that they want an empty string.
    if (args == null) args = "";
    if (args.constructor == String) {
      if (arguments.length > 1) {
        var attributes = arguments[1];
        if (attributes.constructor == String) {
          elem = document.createTextNode(args);
          elements = [];
          elements.push(elem);
          siblings = jQuery.create.apply(null, Array.prototype.slice.call(arguments, 1));
          elements = elements.concat(siblings);
          return elements;
        } 
				else {
          elem = document.createElement(args);

          // Set element attributes.
          var attributes = arguments[1];
          for (var attr in attributes)
            jQuery(elem).attr(attr, attributes[attr]);

          // Add children of this element.
          var children = arguments[2];
          children = jQuery.create.apply(null, children);
          jQuery(elem).append(children);

          // If there are more siblings, render those too.
          if (arguments.length > 3) {
            siblings = jQuery.create.apply(null, Array.prototype.slice.call(arguments, 3));
            return [elem].concat(siblings);
          }
          return elem;
        }
      } 
			else return document.createTextNode(args);
    } 
		else {
      elements = [];
      elements.push(args);
      siblings = jQuery.create.apply(null, (Array.prototype.slice.call(arguments, 1)));
      elements = elements.concat(siblings);
      return elements;
    }
	},
	
	toInt: function(value) {	
		var intVal = 0;
		if (!isNaN(parseInt(value)))
		{
			intVal = parseInt(value);
		} 	
  	return intVal;
  }

});


function initSidebarDropdowns(){
	jQuery('.dropdown').mouseleave(function(){
		setTimeout(function(dropdown) { 
	    	if (jQuery('#topics-dropdown').hasClass('hidden')) {} else jQuery('#topics-dropdown').addClass('hidden');
			if (jQuery('#archive-dropdown').hasClass('hidden')) {} else jQuery('#archive-dropdown').addClass('hidden');
	  },4000);
	});
}

function pa( obj )
{
   if(!obj || typeof(obj) != 'object') {
      var _type = typeof(obj);
      if(_type == 'undefined') {
         alert("["+_type+"]");
      }
      else {
         var _output = "[" + _type + "] => `" + obj + "`";
         alert(_output);
      }
      return;
   }
   
   var
      _output = "\t OBJECT PROPERTIES: \n";
      separator = "";
      spacer = "";
      i = 0;
   
   try{
      for( var _key in obj ) {
         if((i % 2) == 1){ separator = "\n"; spacer = "\t\t"; }
         else{ separator = ""; spacer = ""; };
         
         _output += spacer + "[" + _key + '] => `' + obj[_key] + '`' + separator;
         ++i;
      }
   }
   catch(err){
      _output += "Can't get properties for: " + obj;
   }
   
   alert(_output);
}

function initCommentTabs(){
	jQuery('.comment-tabs li a[rel!="external"]').click(function(){
		jQuery('.comment-tabs li').each(function(){
			jQuery(this).removeClass('active');
		});
		jQuery('.tabs-content').height(jQuery('.tabs-content').height());
		jQuery('.tabs-content .tab').each(function(){
			jQuery(this).hide();
		});
		jQuery('#' + jQuery(this).attr("rel")).show();
		jQuery('.tabs-content').animate({ height: jQuery('#' + jQuery(this).attr("rel")).height() }, 1000);
		jQuery(this).parent('li').addClass('active');
		return false;
	});
	jQuery('.comment-tabs li a').hover(function(){
		jQuery(this).parent('li').addClass('hover');
	}, function(){
		jQuery(this).parent('li').removeClass('hover');	
	});
}
function initCommentTabsID(){
	jQuery('.comment-tabs li a[rel="comments"]').click(function(){
		jQuery('.comment-tabs li').each(function(){
			jQuery(this).removeClass('active');
		});
		jQuery('div[id^="IDTrackback"]').hide();
		jQuery('div[id^="IDThread"]').show();
		jQuery(this).parent('li').addClass('active');
		return false; });
	
	jQuery('.comment-tabs li a[rel="mentions"]').click(function(){
		jQuery('.comment-tabs li').each(function(){
			jQuery(this).removeClass('active');
		});
		jQuery('div[id^="IDThread"]').hide();
		jQuery('#idc-container .idc-tb').show();
		jQuery('div[id^="IDTrackback"]').show();
		jQuery(this).parent('li').addClass('active');
		return false; });
			
	jQuery('.comment-tabs li a').hover(function(){
		jQuery(this).parent('li').addClass('hover');
	}, function(){
		jQuery(this).parent('li').removeClass('hover');	
	});
}

function initTestimonials(id){
	if ( jQuery('#testimonials-list') !== 'undefined' ){
		var old_height = jQuery('#testimonial-' + id).height();
		jQuery("#testimonials-list").height(old_height);
		jQuery('#testimonial-' + id).fadeOut(1000);
		var count = jQuery('#testimonials-list li').length;
		if (id == count) { id = 1; } else { ++id; }
		setTimeout(function(){ jQuery('#testimonial-' + id).fadeIn("slow"); }, 1000);
		
		setTimeout(function(){ var new_height = jQuery('#testimonial-' + id).height(); jQuery("#testimonials-list").animate({ 
	       height: '' + new_height + 'px',
	      }, 1500 ); }, 2000);
		setTimeout(function(){ initTestimonials(id); }, 10000);
	}
}