jQuery.noConflict();

(function($) { // Compliant with jquery.noConflict()
	
	$.fn.extend({

		tabs : function(buts, tabs){
			
			$(tabs).hide();
			$(tabs).eq(0).show();
			$(buts).eq(0).addClass("active");
			
			$(this).click(function(){
				$(buts).removeClass("active");
				$(this).addClass("active");
				var index = $(buts).index(this);
				$(tabs).hide();
				$(tabs).eq(index).show();
				$(this).blur();
				return false;
			});
		},
		
		imgButton : function(w,h){
			$(this).each(function(){
				var jThis = $(this);
				var img = jThis.find("img").eq(0);
				img.hide();
				jThis.css("height", h+"px");
				jThis.css("width", w+"px");
				jThis.css("display", "block");
				jThis.css("background-image", "url("+img.attr("src")+")");
				
				jThis.mouseover(function(){
					jThis.css("background-position","0 -"+h+"px");
				});
				jThis.mouseout(function(){
					jThis.css("background-position","0 0");
				});
			});
		},
		
		mainmenu : function(){
			
			var main = $(this);
			
			main.find("li").find("a").click(function(){
				var ul = $(this).parents("li").eq(0);
				if(ul.find("ul").size() > 0 && !( ul.find("a").eq(0).hasClass("ultimo") || ul.find("a").eq(0).hasClass("primo") ) )
					return false; 
				else
					return true;
			});
			
			main.find("li").find("a").mouseover(function(){
				var ul = $(this).parents("li").eq(0);
				
				ul.siblings().find("ul").hide();
				
				ul.siblings().find("a").removeClass("sopra");
				ul.find("a").eq(0).addClass("sopra");
				
				if(ul.find("ul").size() > 0 && !( ul.find("a").eq(0).hasClass("ultimo") || ul.find("a").eq(0).hasClass("primo") ) ) {
					ul.find("a").eq(0).css("cursor","default");
					ul.show();
					ul.find("ul").eq(0).show();
				}
			});
			
			main.mouseleave(function(){
				$(this).find("ul.menu2, ul.menu3").hide();
				$(this).find("a").removeClass("sopra");
			});
			
		},
		
		briciole : function(menu){
			
			$(this).find("a.br").click(function(){
				
				var menuitem = $(menu).find("a[href='"+$(this).attr("href")+"']");
				var topitem = menuitem.parents("ul.menu li").find("a.active").eq(0);
				
				topitem.trigger("mouseover");
				menuitem.trigger("mouseover");
				
				return false;
			});
		},
		
		saveAndShare : function(div){
			
			$(this).click(function(){
				$(this).blur();
				$(div).removeClass("sasItem");
				$(".sasItem").hide(200);
				$(div).addClass("sasItem");
				$(div).css("height","auto");
				$(div).toggle( 300, function(){} );
				return false;
			});
		},
		
		tellAFriend : function() {

			jForm = $(this);
			
			jForm.find("*").hide();
			jForm.find(".waitfor").show();
			
			var msg = jForm.find("textarea[name='message']").attr('value');
			if(typeof msg == 'undefined')
				msg="";
			
		    var params = { 
	    		lang:jForm.find("input[name='lang']").attr('value'),
	    		email:jForm.find("input[name='email']").attr('value'),
	    		url:location.href,
	    		name:jForm.find("input[name='name']").attr('value'),
	    		message:msg
		    };
		    
		    
			$.getJSON( jForm.attr("action") +"?"+ $.param(params), function(data){
				jForm.find(".waitfor").hide();
				jForm.find(".done").show();
				jForm.find("textarea[name='message']").val('');
				jForm.find("input[name='email']").val('');
				jForm.find("input[name='name']").val('');
			});
			
			return false;
			
		},
		voipDemoRequest : function() {

			jForm = $(this);
			
			jForm.find("*").hide();
			jForm.find(".waitfor").show();
			
		    var params = { 
		    	lang:jForm.find("input[name='lang']").attr('value'),
		    	nome:jForm.find("input[name='nome']").attr('value'),
	    		email:jForm.find("input[name='email']").attr('value'),
	    		cognome:jForm.find("input[name='cognome']").attr('value'),
	    		cellulare:jForm.find("input[name='cellulare']").attr('value'),
	    		imei:jForm.find("input[name='imei']").attr('value')
		    };
		    
			$.getJSON( "/voipDemoRequest.html?"+ $.param(params), function(data){
				jForm.find(".waitfor").hide();
				jForm.find(".done").show();
		    	jForm.find("input[name='nome']").val(''),
	    		jForm.find("input[name='email']").val(''),
	    		jForm.find("input[name='cognome']").val(''),
	    		jForm.find("input[name='cellulare']").val(''),
	    		jForm.find("input[name='imei']").val('')
			});
			
			return false;
			
		},
		
		bookmarkpage : function() {
			
			// add a "rel" attrib if Opera 7+
			if(window.opera) {
				if ($(this).attr("rel") != ""){
					$(this).attr("rel","sidebar");
				} 
			}
	
			$(this).click(function(event){
				event.preventDefault();
				var url = this.href;
				var title = this.title;
				
				if (window.sidebar) { // Mozilla Firefox Bookmark
					window.sidebar.addPanel(title, url,"");
				} else if( window.external ) { // IE Favorite
					window.external.AddFavorite( url, title);
				} else if(window.opera) { // Opera 7+
					return false; // do nothing
				} else { 
					 alert('Unfortunately, this browser does not support the requested action,'
					 + ' please bookmark this page manually.');
				}
			
			});

		}
		
	});
	


})(jQuery); // Compliant with jquery.noConflict()

function getParameter ( parameterName ) {
	   
	   var queryString = window.top.location.search.substring(1);

	   // Add "=" to the parameter name (i.e. parameterName=value)
	   var parameterName = parameterName + "=";
	   if ( queryString.length > 0 ) {
	      // Find the beginning of the string
	      begin = queryString.indexOf ( parameterName );
	      // If the parameter name is not found, skip it, otherwise return the value
	      if ( begin != -1 ) {
	         // Add the length (integer) to the beginning
	         begin += parameterName.length;
	         // Multiple parameters are separated by the "&" sign
	         end = queryString.indexOf ( "&" , begin );
	      if ( end == -1 ) {
	         end = queryString.length
	      }
	      // Return the string
	      return unescape ( queryString.substring ( begin, end ) );
	   }
	   // Return "null" if no parameter has been found
	   return "null";
	   }
	}
