$(document).ready(function(){
	if($.fn.tooltip){
		$('img.jTip').tooltip({showURL: false});		
		$("a.jTip").tooltip({ 
			bodyHandler: function() { 
					return $($(this).attr("href")).html(); 
			}, 
			showURL: false 
		});
	}

													 
 //tabs
 if($.fn.idTabs) $('.jtabs').idTabs();
 
 //coveragebox
 if($.fn.jformajax) $('.jcoverage').jformajax();
 
 
 //hover
	$('.jhover > *').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	
	
	/* cleaning des champs input on focus/blur */
	$.fn.search = function(type) {
		return this.focus(function() {					 
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$('.jclean').search();
	
	
	/* popup link */
	$('.jPopup').click(function(){
		var w = getParam(this.href, 'w') || 600;
		var h = getParam(this.href, 'h') || 400;
		var l = ($(window).width())/2 - w/2;
		var t = ($(window).height())/2 - h/2;
		var s = getParam(this.href, 's') || 1;
		window.open(this.href, '','top='+t+', left='+l+', width='+w+',height='+h+', scrollbars='+s+', resizable=1');
		return false;
	});	
	
	
	/* ul-tabs04 */
	$('a', '.jtabs-input').click(function(){
		$(this).find('input').attr('checked', true);
	});
	$('input', '.jtabs-input').click(function(){
		var element = this;
		setTimeout(function(){ $(element).attr('checked', true); }, 10);
	});
	
	
	/* .. */
	$('.box-hightlight').hover(function(){
		if($(this).hasClass('hover')) return;
		$(this).addClass('hover');
	}, function(){
		if($('input', this).is(':checked')) return;
		$(this).removeClass('hover');
	});
	
	
	/* lists */
	$('.ul-offer-options .item').hover(function(){
		if($(this).hasClass('hover')) return;
		$(this).addClass('hover');
	}, function(){
		if($('input', this).is(':checked')) return;
		$(this).removeClass('hover');
	}).find('input').bind('clickEvent', function(){
		if(this.checked)
		{
			$(this).parent().siblings().filter('.selected').removeClass('selected');
			$(this).parent().addClass('selected');
			var offerVal = $(this).parents('.wp-offer').parent().find('input[name=offerta]').val();
			$.get("/set_option_ajax_do.php", { optionV: this.value, offerV: offerVal });
		}
	}).click(function(){
		$(this).trigger('clickEvent');
	}).trigger('clickEvent');
});


/* parsing of url parameters */
function getParam(url, name){
	var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	if( results == null )
		return "";
	else 
		return results[1];
}


/* flash replacement (title) */
if(typeof window.sIFR == 'function'){
	var neosansbold = { src: '/flash/neosansbold.swf' };
	sIFR.activate(neosansbold);
	sIFR.replace(neosansbold, {
		wmode: 'transparent',
	  selector: '.hf01',
		css: [ '.sIFR-root {color:#00ADED;}' ]
	});
	sIFR.replace(neosansbold, {
		wmode: 'transparent',
	  selector: '.hf02',
		css: [ '.sIFR-root {color:#ffffff;}' ]
	});
	var neosans = { src: '/flash/neosans.swf' };
	sIFR.activate(neosans);
	sIFR.replace(neosans, {
		wmode: 'transparent',
	  selector: '.hf03',
	  css: [
			'.sIFR-root{ color: #434343; }',
			'a { color: #0C73A7; text-decoration: underline; }',
			'a:hover { color: #0C73A7; text-decoration: none; }'			
		]
	});
}


/* instant clear field on focus */
function jclean(element){
	if(!element.binded){
		element.binded=true;
		$(element).blur(function(){
			if(!this.value.length) this.value = this.defaultValue;
		});
	}
	if(element.value == element.defaultValue) element.value = "";
}


(function($){			
	$.fn.jexpand = function(){
		var toggle = function(){
			var img = $('img', this);
			var imageSrc = img.length ? $('img', this).attr('src') : $(this).css('background-image');
			
			if(this.content.is(':visible')){
				this.content.slideUp();	
				imageSrc = imageSrc.replace('-on', '-off');
			} else {
				this.content.slideDown();	
				imageSrc = imageSrc.replace('-off', '-on');
			}
			(img.length) ? img.attr('src', imageSrc) : $(this).css('background-image', imageSrc);
			return false;
		} 
		
		this.each(function(){
			var href = $(this).attr('href');
			this.content = $(href).hide();
		}).bind('click', toggle);
	}
	//Auto run
	$(function(){ $('.jexpand').jexpand(); });
})(jQuery);

function btnAttivaOra(element){
	var inputs=$(element).parents('.wp-btns').parent().find('input[name=promotion]');
	if(!inputs.filter(':checked').length){
		var offer = $(element).parents('.jcoverage').find('input[name=offerta]').val().replace(/\s+/g, "_");

		$('#info-popup-option .ul-offer-options').hide();
		$('#info-popup-option').find("#options_"+offer).show();
		
		$.fn.serialbox.open('?top=auto&left=auto&width=656#info-popup-option'); 
		return false;
	} else {
		return true;	
	}
}