var host    = 'http://www.cellulare.net';
var locale  = 'it';

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 openPopup(url, width, height) {
window.open(url+'?just_content=S','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')
}

var fancybox_options = {
    'zoomSpeedIn'			: 600,
    'zoomSpeedOut'			: 500,
    'easingIn'				: 'easeOutBack',
    'easingOut'				: 'easeInBack',
    'hideOnContentClick'                : false,
    'hideOnOverlayClick'                : false,
    'overlayShow'			: true,
    'imageScale'			: true,
    'centerOnScroll'                    : true,
    'callbackOnStart'                   :
        function () {
            if(jQuery('#breadcrumb').size() > 0) jQuery('#mainmenu, #breadcrumb, #floating_brands').hide(); if(jQuery('.barra').size() > 0) jQuery('.barra').hide(); jQuery('iframe').hide(); /* jQuery('#picture').cycle('pause'); */
        },
    'callbackOnClose'                   :
        function () {
            if(jQuery('#breadcrumb').size() > 0) jQuery('#mainmenu, #breadcrumb, #floating_brands').show(); if(jQuery('.barra').size() > 0) jQuery('.barra').show(); jQuery('iframe').show() /* jQuery('#picture').cycle('resume'); */
        }
};

var modal_options = { 
opacity					: 30,
closeHTML				: '<a class=”modalCloseImg” title=”X”></a>',
onOpen					: function () {
if(jQuery('#breadcrumb')[0]) jQuery('#breadcrumb').fadeOut('fast'); /* jQuery('#picture').cycle('pause'); */
},
onClose					: function () {
if(jQuery('#breadcrumb')[0]) jQuery('#breadcrumb').fadeIn('slow'); /*jQuery('#picture').cycle('resume'); */
},
overlayCss				: {
backgroundColor: '#000',
cursor: 'wait'
},
containerCss			: {
height: 400,
width: 600,
backgroundColor: '#fff',
border: '3px solid #ccc'
}
};

var fancybox_options_inline  = { 
'zoomSpeedIn'			: 600,
'zoomSpeedOut'			: 500,
'frameWidth'			: 800,
'frameHeight'			: 600,
'easingIn'				: 'easeOutBack',
'easingOut'				: 'easeInBack',
'overlayShow'			: true,
'hideOnContentClick'                : true,
'centerOnScroll'                    : true,
'callbackOnStart'                   : function () {
if(jQuery('#mainmenu')[0]) jQuery('#mainmenu, #breadcrumb, #floating_brands').hide();  if(jQuery('.barra').size() > 0) jQuery('.barra').hide(); jQuery('.barra').hide(); jQuery('iframe').hide(); /* jQuery('#picture').cycle('pause'); */
},
'callbackOnClose'                   : function () {
if(jQuery('#mainmenu')[0]) jQuery('#mainmenu, #breadcrumb, #floating_brands').show(); if(jQuery('.barra').size() > 0) jQuery('.barra').show(); jQuery('iframe').show(); /* jQuery('#picture').cycle('resume'); */
}
};

var products_images_slider_options = {
fx			: 'shuffle',
easing		: 'backout',
random		: 0,
pause		: 1,
speed		: 750,
timeout		: 0,
pager		: '#pictures_pager' ,
speedIn		:  500,
speedOut	: 500,
delay		:   -2000
}

function toggleSql(id) {
var id = id != '' ? id : 'query_debug';
jQuery("#"+id).toggleFade();
}


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 = text.toUpperCase();
else
text = text;

text = text.replace(" ","_");
text = text.replace("\#","");
text = text.replace("@","");
text = text.replace("'","");
text = text.replace(";","");
text = text.replace(",","");
return text;
} 

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

location.reload();
}

function toggleCacheLabels(enable) {
if(enable == true)
createCookie('show_cache_labels',true,1);
else
createCookie('show_cache_labels',true,-1);

location.reload();
}

function toggleSqlLabels(enable) {
if(enable == true)
createCookie('show_queries',true,1);
else
createCookie('show_queries',true,-1);

location.reload();
}

function toggleCDNSession(enable) {
if(enable == true)
createCookie('cdn_refresh',true,1);
else
createCookie('cdn_refresh',true,-1);

location.reload();
}

var user_screen_width;
var user_screen_height;

function popup(testo) {
alert(testo);
}

function popupStatic(testo) {
notifyPersistent(testo);
}

// with notification
function notifyFading(testo) {
notifyTransient(testo,5000);
}

function notifyTransient(testo,tempo_ms) {
if((tempo_ms + '') == '')
tempo_ms = 5000;

//    jQuery(window).humanMsg(testo, {
//        autoHide: tempo_ms,
//        // addClass: 'my-class-name',
//        speed: 500
//    });
jQuery().message(testo);
}


var message_options = 
{
	id: 'message_from_top',
position: 'top',
size: 70,
backgroundColor: '#E6F3FF',
delay: 5500,
speed: 500,
fontSize: '35px'
};	


function notify(testo) {
jQuery.showMessage(testo, message_options);
}

function notifyPersistent(testo) {
//    jQuery(window).humanMsg(testo, {
//        autoHide: 35000,
//        // addClass: 'my-class-name',
//        speed: 500
//    });
jQuery().message(testo);
}

// override della funzion alert
//  var alert_proxied = window.alert;
//
//  function alert(message) {
//    if(jQuery.message) {
//       jQuery.message(message);
//    }
//    else {
//        return alert_proxied.apply(this, arguments);
//    }
//  };

function switchLanguage(current_language_code, new_language_code) {
var desctination = document.location.href == '' ? 'index.php' : document.location.href;

if(document.location.href == '') {
document.location.href = '/home/'+new_language_code;
}
else if(document.location.href.indexOf("/"+current_language_code+"/") == -1) {

var new_loc = document.location.href;
new_loc = new_loc.replace(host+'/',host+'/'+new_language_code+'/');
document.location.href = new_loc;
}
else {
var new_loc = document.location.href;
new_loc = new_loc.replace('/'+current_language_code+'/','/'+new_language_code+'/');
document.location.href = new_loc;
}
}

function changeCurrency(code) {
//    document.location.href =  '/'+locale+ '/curr/'+jQuery('#currency').val();
var date = new Date();
date.setTime(date.getTime() + (31 * 24 * 60 * 60 * 1000));
if(code != undefined && code != 'undefined' && code != '') {
   jQuery.cookie('currency', code, { path: '/', expires: date });
}
else {
   jQuery.cookie('currency', jQuery('#currency').val(), { path: '/', expires: date });
}
location.reload(true);
return false;
}

function toggleProfiler() {
if(jQuery('#facebox_profiler').is(':visible')) {
jQuery('#facebox_profiler').toggle();
}
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;
popup('<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 {
popup('<div align="center">page load time: '+jQuery('#page_load_time').html()+' sec.</div>');
}
}

}

function showPageSize() {

popup('<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 != '') {
popup(data);
}
else {
// gestione errori
}
}
});
}


///* 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();

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("#fancy_bg #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')
}
);
}
);
}

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

function submitSearch() {
if(jQuery('#searchform')[0])
jQuery('#searchform').submit();
}

function crossToggleSmooth(id_tohide, id_toshow) {
if(jQuery(id_tohide)[0] && jQuery(id_toshow)[0]) {
//        jQuery(id_tohide).hide("slow", {
//            direction: "vertical"
//        }, 250, function() {
//            jQuery(id_toshow).show("slow", {
//                direction: "vertical"
//            }, 250);
//        });
jQuery(id_tohide).hide("slow");
jQuery(id_toshow).show("blind");
}
}

function toggleSmooth(ids) {
if(jQuery(ids)[0]) {
jQuery(ids).toggle('slow');
/*

var visibles 	= jQuery(ids).filter(':visible');
var hiddens 	= jQuery(ids).filter(':hidden');

if(visibles[0]) {
visibles.hide("blind", {
direction: "vertical"
}, 250,
function() {
if(hiddens[0]) {
hiddens.show("blind", {
direction: "vertical"
}, 250);
}
}
);
}
else {
if(hiddens[0]) {
hiddens.show("blind", {
direction: "vertical"
}, 250);
}
}
*/
}
}

function showSmooth(id_toshow) {
if(jQuery(id_toshow)[0]) {
//        jQuery(id_toshow).show("slow", {
//            direction: "vertical"
//        }, 250);
jQuery(id_toshow).show("slow");
}
}

function hideSmooth(id_tohide) {
if(jQuery(id_tohide)[0]) {
//        jQuery(id_tohide).hide("slow", {
//            direction: "vertical"
//        }, 250);
jQuery(id_tohide).hide("slow");
}
}

function getContentIframe(name,target, language_code) {
jQuery('#'+target).attr('src','/webservices.php?service=S&action=get_content&language_id='+language_code+'&key_name='+name);
}

function getContent(name,target, language_code) {

jQuery('#'+target).load('/webservices.php?service=S&action=get_content&language_id='+language_code+'&key_name='+name);
}

function refreshShoppingCart() {
jQuery('#headmenu').load('/includes/modules/shopping_cart.php');
}

function straightBuyUp(element_id) {
//		jQuery('#quick_buy_products_id').val(element_id);
//		jQuery('#cart_quick_buy').submit();
jQuery.post("/product_info.php",
{
service         : "S",
action          : "add_product",
skip_redirect   : true,
products_id     : element_id
},
function(data){
refreshShoppingCart();
}
);
	}

function googleTranslateElement(element_page_id, from, to) {
if(jQuery(element_page_id)) {
jQuery(element_page_id).translate(
from, to, {
fromOriginal:true
}
);
}
}

var name_index = 0;
function removeScatteringFakeAnchors() {

jQuery('a[href="#"]').each(
function() {
var el = jQuery(this);
if(el.attr('name') != '')
el.attr('href','#'+el.attr('name'));
else
el.attr('href','#'+name_index++);
}
);
}

jQuery(document).ready(
function() {

jQuery('form').removeAttr("disabled");

removeScatteringFakeAnchors();

writeResolutionCookie();

// 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.browser.msie) {
}

// binds key labels on click
if (jQuery('.keylabel')[0]) {
bindKeyLabels();
}

// administrative contect menu...
if(jQuery('#admin_panel_menu')[0]) {
}

if(jQuery('.debug_msg')[0])
showNotificationMessages();

if(jQuery('select[multiple]')[0]) {
jQuery('select[multiple]').asmSelect(
{
highlight				: true,
animate					: true,
removeLabel				: 'X',
highlightAddedLabel		: '+',
highlightRemovedLabel	: '-'
}
);
}

if(jQuery('#filtro:not(:visible)')[0])
jQuery('#filtro').fadeIn('slow');

if(jQuery('#picture')[0]) {
jQuery('#product_picture_0').fadeIn();
jQuery('#picture').fadeIn();
}


		//if(jQuery('input:password')[0] && jQuery('input:password').dPassword)
		//	jQuery('input:password').dPassword();

jQuery("a[href]").filter('a[rel]').each(
function() {
var an = jQuery(this);
if(an.attr('rel') == 'external')
an.attr("target","_blank");
}
);

}
);

function dumpObject(obj) {
    var output = '';
    for (property in obj) {
        output += property + ': ' + obj[property]+'; ';
    }
    return output;
};

function breakJS() {
    jQuery.makeanhorriblemistaketotestgracefuldegradation();
}

function getStates(countryID, div_element) {


        $.post("/create_account.php",
            {
                action: "getStates",
                country: countryID
            },
            function(data) {
                if(jQuery.browser.msie) {
                    document.getElementById(div_element).innerHTML = data;
                }
                else {

                    var target_obj = jQuery('#'+div_element) || jQuery('[name="'+div_element+'"]');

                    if(target_obj) {

                        target_obj.html(data);
                        target_obj.children("select").focus();

                    }
                }
            }
        );
    }