function popupWindow(url, width, height) {
  width = width | '800';
  height = height | '600';
  
  try {
  	Mediabox.open(url, 'www.cellulare.net', width +' '+height);
  }
  catch(e) {
  	window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',screenX=150,screenY=150,top=150,left=150')
  }  
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function writeResolutionCookie() { 
	createCookie('resolution',screen.width +"x"+ screen.height,1); 
} 

function emailValidation(str) {
        var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

/* GOOGLE translation stuff */
//function translate(text, locale_from, locale_to, target) {
//	
//  if(locale_from == '') {
//  	google.language.detect(text, 
//		function(result) {
//		  if(!result.error) {
//		    var language = 'unknown';
//		    for (l in google.language.Languages) {
//		      if (google.language.Languages[l] == result.language) {
//		        language = l;
//		        break;
//		      }
//		    }
//		    locale_from = language;
//		  }
//		  else
//		  	locale_from = 'it';
//		}
//	);
//  }
//	
//  if(locale_to == '')
//  	locale_to = 'en';
//
//  translation = 'default';
//
//  google.language.translate(text, locale_from, locale_to, 
//  	function(result) {
//		  if(!result.error) {
//			jQuery('#'+target).text(result.translation);
//		  }
//		  else {
//		  	alert(result.error.message + ' '+text);
//		  }
//	  }
//  );
//  return;
//}
//  function getTranslation(text, target, from, to) {
//		translate(text, from, to, target);
//		jQuery('#'+target).focus();
//  }
//  
//  function setLanguage(){
//  	var language = jQuery('#language').val() || 'it';
//	window.location = window.location + 'language='+language;
//  }
//  
//  function translateAll(to_locale) {
//		
//		var from_locale = jQuery('#languages_from').val() || 'it';
//		var id_index = 0;
//		jQuery('div, span, p, td').each(
//			function() {
//				element = jQuery(this);
//				
//				if(element.attr('id') == '')
//					element.attr('id',"page_element_"+(id_index++));
//				
//				var source_id = element.attr('id');
//				getTranslation(element.text(), source_id, from_locale, to_locale);
//			}
//		);
//	}
//

//google.load("language", "1");

	// to obtain a uppercase name with no spaces, commas, etc...
	function normalizeValue(text,force_upper) {

		if(force_upper === true)
			text = jQuery.string(text.toUpperCase());
		else
			text = jQuery.string(text);
		
		text = text.strip().gsub(" ","_").gsub("\#","").gsub("@","").gsub("'","").gsub(";","").gsub(",","");
		return text.str;
	 } 

function toggleKeyLabels(enable) {
	if(enable == true)
		createCookie('show_labels',true,1);
	else
		createCookie('show_labels',true,-1);
	
	location.reload();	
}

var user_screen_width;
var user_screen_height;

function popup(testo) {
	jQuery.facebox(testo);	
}


	// with jGrowl
	//function notify(testo) {
	//	jQuery.jGrowl(testo, { sticky: false, life: 20000 });
	//}
	//
	//function notifyTransient(testo,tempo_ms) {
	//	if((tempo_ms + '') == '')
	//		tempo_ms = 20000;
	//	
	//	jQuery.jGrowl(testo, { sticky: false, life: tempo_ms });
	//}
	//
	//function notifyPersistent(testo) {
	//	jQuery.jGrowl(testo, { sticky: true });
	//}


	// with facebox (compatibility mode...)
//	function notifyTransient(testo,tempo_ms) {
//		jQuery.facebox(testo);
//	}
//	
//	function notify(testo) {
//		jQuery.facebox(testo);
//	}
//	
//	function notifyPersistent(testo) {
//		jQuery.facebox(testo);
//	}
	
	// with notification
	function notifyFading(testo) {
		notifyTransient(testo,20000);
	}
	
	function notifyTransient(testo,tempo_ms) {
		if((tempo_ms + '') == '')
			tempo_ms = 20000;

		jQuery.noticeAdd({
		        text: testo,
		        stay: false,
		        stayTime: tempo_ms,
		        fadeOff: true
		});
	}
	
	function notify(testo) {
		jQuery.noticeAdd({
	        text: testo,
	        stay: false
		});
	}
	
	function notifyPersistent(testo) {
		jQuery.noticeAdd({
	        text: testo,
	        stay: true
		});
	}

function switchLanguage(language_code) {
	var desctination = document.location.href == '' ? 'index.php' : document.location.href;
	jQuery('#language_form').attr("action",desctination);
	jQuery('#language_form input[name="language"]').val(language_code);
	jQuery('#language_form').submit();
}

function toggleProfiler() {
	if(jQuery('#facebox_profiler').is(':visible')) {
		jQuery('#facebox_profiler').toggle();
		jQuery.facebox.close();
	}
	else {
		var message = jQuery('#profiler_div').html();
		if(message != null)
			message = message.replace(/profiler_subgroup_/g,"box_profiler_subgroup_");
		
		var box_width = screen.width - 150;
		jQuery.facebox('<div id="facebox_profiler" style="width: '+ box_width +'px">' + message + '</div>');
	}
	
}

function toggleLevel(level_id) {
	if(jQuery('#facebox_profiler #'+level_id).is(':visible')) {
		jQuery('#facebox_profiler #toggler_'+level_id).html('<img src="platform/images/icon_expand1.png" class="icon"/>');
		jQuery('#facebox_profiler #'+level_id).toggle();
	}
	else {
		jQuery('#facebox_profiler #toggler_'+level_id).html('<img src="platform/images/icon_collapse1.png" class="icon"/>');
		jQuery('#facebox_profiler #'+level_id).toggle();
	}
}

function reloadPage() {
	document.location.href = document.location.href;
}

function showPageLoadTime() {
	if(jQuery('#page_load_time')[0]) {
		if(jQuery('#page_load_time_container')[0]) {
			jQuery('#page_load_time_container').html(jQuery('#page_load_time').html());
		}
		else {
			jQuery.facebox('<div align="center">page load time: '+jQuery('#page_load_time').html()+' sec.</div>');
		}
	}
	
}

function showPageSize() {
	
	jQuery.facebox('<div align="center"><img src="platform/images/icon_loading1.gif" border="0"/></div>');
	jQuery.ajax({
		type: "GET",
		url: "includes/general.services.php",
		contentType: "application/html; charset=utf-8",
		data: "service=S&service_call=GET_PAGE_SIZE"+
			  "&URL="+escape(window.location.href),
		success: function(data){
			if(data != '') {
				jQuery.facebox(data);
			}
			else {
				// gestione errori
				jQuery.facebox.close();
			}
		}
	});
}


/* on page load functions... */ 
function goManufacturer() {
	var manuf_id = jQuery('select[name="manufacturers_id"]').val();
	if(manuf_id != '')
		document.location.href = 'it-index-n-_-manufacturers_id-'+manuf_id+'.html';
}

function bindKeyLabels() {
	jQuery('.keylabel').children("img").click(
			   function(event) {
					event.preventDefault();
					
					var key = jQuery(this).parent().attr("title");
					var key_id = jQuery(this).parent().attr("key_id") || '';
					var key_content = jQuery(this).parent().children("span.keylabel_content").html();
					//jQuery.clipboard(key);
					
					popup('<div><b>'+key+'</b><br/><a href="admin/language_keys.php?filter_key='+key+'" target="_blank">edit</a>' +
						  '<br/br/><span border="1">contenuto:<br/><pre><span id="inplace">'+htmlspecialchars(key_content)+'</span></pre></span>' +
						  '</div>');
					if(key_id != '') 
						jQuery("#inplace").editInPlace(
							{
							    url: "admin/language_keys.php",
							    update_value: "content",
							    success: function(data) {
												jQuery('span.keylabel[key_id="'+key_id+'"] span.keylabel_content').html(data);
										 },	
								show_buttons: true,
								value_required: true,
								field_type: "textarea",
							    params: "service=S&action=update_key_value&languages_keys_id="+key_id+"&locale="+(jQuery('#current_locale')[0] ? jQuery('#current_locale').val() : '4')  
							}
						);
				}
			).tooltip();
}

function showNotificationMessages() {
	if(jQuery('.debug_msg_dirty')[0])
		jQuery('.debug_msg_dirty').each(function() { notifyTransient(jQuery(this).html(),15000) }).removeClass("debug_msg_dirty");
}

jQuery(document).ready(
		function() {

			writeResolutionCookie();
			
			if(jQuery('a[rel*=lightbox]')[0])
				jQuery('a[rel*=lightbox]').attr("rel","facebox");
			
			if(jQuery('a[rel*=facebox]')[0])
				jQuery('a[rel*=facebox]').facebox(); 
			
			// create in-page cross selling slider elements
			if (jQuery('.news_slider')[0]) {
				jQuery('.news_slider').each(function(){
					jQuery(this).accessNews({
						newsHeadline: this.title,
						newsSpeed: "normal"
					});
					
				});
				
				if (jQuery(".cross_selling_container")[0]) { 
					jQuery(".cross_selling_container > ul").tabs({
						fx: {
							opacity: 'toggle',
							duration: 'fast'
						}
					});
					jQuery(".cross_selling_container").fadeIn(500);
				}
				
			}
		
		/*
		 * insert here behaviours that Internet Explorer cannot fit...
		 */
		if(!jQuery.browser.msie) {

		}
		
		// binds key labels on click
		if (jQuery('.keylabel')[0]) {
			bindKeyLabels();
		}
		
		// administrative contect menu...
		if(jQuery('#admin_panel_menu')[0]) {
			jQuery(".bodyTd").contextMenu({
		        menu: 'admin_panel_menu'
		    },
		        function(action, el, pos) {
			       // put here post-click actions...
			       eval(action);
		    });
		}
		
		jQuery('textarea.elastic').autogrow();
		
		if(jQuery('.debug_msg')[0])
			showNotificationMessages();
		
	  }
	);
