// JavaScript Document
var hideDelayTimer = null;
var hideDelay = 550;
var time = 150;
var waveHeight = 0;

jQuery(function(){  

	jQuery("#share-social-tab,#searchmenu").hide();
	
	jQuery('#logo a').mouseover(function(){
		jQuery('#logo').stop().animate(
			{top: "-20px"}, 
			{duration:200})
		}).mouseout(function(){
		jQuery('#logo').stop().animate(
			{top: "-40px"}, 
			{duration:500})
		});
	
	if (jQuery('#emotion-home').length > 0){
		jQuery('#emotion-home').tabs({ fx: { opacity: 'toggle'}});
		jQuery('#emotion-home').tabs('rotate' , 8000);
	}
	
	jQuery('#wave-amount').animate({height: waveHeight},{duration:1000});
	
	//if (jQuery(".article-list li a,.entries-list div.entry a").length > 0){
		jQuery(".article-list li,.entries-list div.entry").mouseover(function(){jQuery(this).addClass('over');}).mouseout(function(){jQuery(this).removeClass('over');}).click(function() {if (jQuery(this).find("a").length > 0){window.location = jQuery(this).find("a:first").attr('href');}});
	//}
	
	//jQuery("input.stdinput,textarea.stdinput").focus(function(){jQuery(this).addClass("stdinput-sel");}).blur(function(){jQuery(this).removeClass("stdinput-sel");});
	
	jQuery('.easy-form,#searchform').EasyForms();
	
	
	jQuery("a.icon-share,#share-social-tab").mouseover(function(){showElement(jQuery("#share-social-tab"));return false}).mouseout(function(){hideElement(jQuery("#share-social-tab"));return false;});	
	jQuery("#icon-search").click(function(){showElement(jQuery(jQuery("#searchmenu")));return false}).mouseout(function(){hideElement(jQuery("#searchmenu"));return false;});	
	jQuery("#searchmenu").mouseover(function(){showElement(jQuery(jQuery("#searchmenu")));return false}).mouseout(function(){hideElement(jQuery("#searchmenu"));return false;});	
		
	
	collection = jQuery('.text-content a').filter(':linkingToImage(href)');
	collection.fancybox(); 

	
	jQuery(".thickbox").fancybox({
					'fancyLoading'			: true,
					'overlayShow'			: false,
					'zoomSpeedIn'			: 300,
					'zoomSpeedOut'			: 300,	
					'padding'				: 5,	
					'hideOnContentClick'	: true,
					'frameWidth'			: 700,
					'frameHeight'			: 550
    }); 
	
	// Popup	
	/*jQuery('.standard-links-list a[href^="http://"],div.text-format a[href^="http://"],.launch a, a.launch').not('.nopop').click(function(){
		var siteUrl = jQuery(this).attr('href');	
		//console.log(siteUrl.slice(0,29));
		if (!(siteUrl.slice(0,29) == 'http://www.studiohangloose.it')){		
			var videoDescription = jQuery(this).attr('title');	
			var currentLocation = document.location.href;
			window.location = '/popup/?url='+URLEncode(siteUrl)+'&ref='+URLEncode(currentLocation);	 
			return false;
		}		
	});*/
	
	
	
	
});

///
// This will return true if the specified attribute contains a valid link to an image.
///
jQuery.expr[':'].linkingToImage = function(elem, index, match){       
        return !! (jQuery(elem).attr(match[3]) && jQuery(elem).attr(match[3]).match(/\.(gif|jpe?g|png|bmp)$/i));
    };




function hideElement(id){
  if (hideDelayTimer) clearTimeout(hideDelayTimer);
  hideDelayTimer = setTimeout(function () {
										hideDelayTimer = null;
										id.slideUp();;
										}, hideDelay);  
}
  
function showElement(id){ 
  if (hideDelayTimer) clearTimeout(hideDelayTimer);
  id.slideDown();
}

// URL Encode function
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


/*
 * More usable Forms 
 * Copyright (c) 2009  Oliver Astrologo
 * Dual licensed under MIT and GPL.
 * @author  Oliver Astrologo
 * @version 2.3
 */

jQuery.fn.EasyForms=function(){
this.submit( function() {
			var submitForm = this;
			jQuery(submitForm).find("label").not(".radiolabel,div#subscribe-btn label").each(function(){
				var current=jQuery(this);
				var targetForm =  jQuery('#'+current.attr("for"));				
				if (targetForm.val() == current.html()){
				targetForm.val('');
				};																			  
			});
		return true;
	});	
	this.find("label").not(".radiolabel,,div#subscribe-btn label").each(function(){
		var current=jQuery(this);		
		var targetForm = jQuery('#'+current.attr("for"));	   
	   if (targetForm.val() == ''){
			targetForm.val(current.html());		
			}	   
	   targetForm.click(function(){		
		if (jQuery(this).val() == current.html()){
			jQuery(this).val('');			
			}
		});
		targetForm.blur(function(){		
		if (jQuery(this).val() == ''){
			jQuery(this).val(current.html());			
			}
		});
		current.hide();	
	});			
}