var $e=jQuery.noConflict();
$e(document).ready(function(){
	//================================================== shared-related
	var url=window.location.href;
	var url=window.location.href;
	sub_url=url.split('/');
	sub_url=sub_url[sub_url.length-1];
	if (sub_url=='viso'||sub_url=='corpo'||sub_url=='cerotti-athena'||sub_url=='integratori'){
		url=url.split('/');
		url[url.length-1]='prodotti';
		url=url.join('/');
	}
	$e('.headbar-bottom .menu a').each(function(){
		href=$e(this).attr('href');
		if (href==url){
			$e(this).css({'background-color':'#c30e7c'});
		}
	});

	if (jQuery.browser.msie){
		$e('.highlight').each(function(){
			$e(this).remove();
		});
	}

	if ($e('.slider').length){
		$e('.slider .slides').cycle({
			fx:'uncover',
			timeout:5000,
			speed:2000,
			slideExpr:'.slide'
		});
		$e('.slider .slides').click(function(){
			window.location.href='clinica-del-derma';
		});
	};

	if ($e('.content .product-info.to-slide .overflower').length){
		$e('.content .product-info.to-slide .overflower').cycle({
			fx:'scrollHorz',
			timeout:0,
			speed:2000,
			slideExpr:'.placeholder',
			prev:'.content .product-info.to-slide .pager.left',
			next:'.content .product-info.to-slide .pager.right'
		});
		$e('.sections li a').click(function(){
			$e('.content .product-info.to-slide .overflower').cycle(parseInt($e(this).attr('lang')));
			return false;
		});
	}

	if ($e('.sections.clinica').length){
		$e('.sections.clinica a').click(function(){
			smallPicSrc=$e(this).children('img').attr('src');
			bigPicSrc=$e('.bigpic').attr('src');
			$e('.bigpic').attr('src',smallPicSrc);
			$e(this).children('img').attr('src',bigPicSrc);
			return false;
		});
	};

	$e('a').mouseenter(function(){
		if ($e(this).find('.highlight').length)
			$e(this).find('.highlight').fadeIn(100);
	}).mouseleave(function(){
		if ($e(this).find('.highlight').length)
			$e(this).find('.highlight').fadeOut(100);
	});

	//the following set the height of various elements to their respective siblings'
	if ($e('.content .block.right .sub-block.small').length && $e('.content .block.right .sub-block.tall p.more').length){
		heightBlockSmall=$e('.content .block.right .sub-block.small').height()*2+5;
		heightBlockTall=$e('.content .block.right .sub-block.tall').height();
		heightToSet=(heightBlockSmall-heightBlockTall+3)+'px';
//		$e('.content .block.right .sub-block.tall p.more').css('padding-bottom',heightToSet);
	};
	
	$e('input[type="text"]').each(function(){
		readonly=$e(this).attr('readonly');
		if (readonly!=true){
			$e(this).clearField();
		}
	});
	
	if ($e('.datepicker').length){
		$e('.datepicker').datepicker();
	};

	if ($e('.jcarousel-skin-css').length){
		$e('.jcarousel-skin-css').jcarousel({
			auto:0,
			scroll:1,
			visible:5,
			animation:'slow',
			wrap:'both'
		});
	};
	
	$e('.product-info .semi-block img.small').mouseenter(function(){
		$e(this).css('border-color','#7a1452');
	}).mouseleave(function(){
		$e(this).css('border-color','#939393');
	}).click(function(){
		changeSmall=$e('.product-info .semi-block img.big').attr('src');
		changeBig=$e(this).attr('src');
		$e(this).attr('src',changeSmall);
		$e('.product-info .semi-block img.big').attr('src',changeBig);
	});
	//================================================== /shared-related
	//================================================== auto-compelete country-related
	if ($e('#ac_country').length){
		var ac_country='#ac_country';
		//options are the same as the JQuery Autocomplete plugin
		$e(ac_country).autocomplete(countries,{
			minChars:1,
			width:320,
			matchContains:true,
			scroll:true,
			max:0,
			formatItem:function(row,i,max,term){
				return '<img src="../wp-content/themes/derma/resources/images/flags/'+row.code.toLowerCase()+'.gif" /> '+row.name;
			},
			formatResult:function(row){
				return row.name;
			},
			formatMatch:function(row,i,max){
				return row.name;
			}
		});

		$e(ac_country).after($e(ac_country).clone().attr('value','').attr('id',$e(ac_country).attr('id')+'_hidden'));
		$e(ac_country).removeAttr('name','').clearField();
		$e(ac_country).result(function(event,data,formatted){
			if(data){
				$e(ac_country+'_hidden').val(data.code.toLowerCase());
			}
			var src='../wp-content/themes/derma/resources/images/flags/'+data.code.toLowerCase()+'.gif';
			$e(ac_country).css('backgroundImage','url('+src+')');
		});
	};
	//================================================== /auto-compelete country-related
	//================================================== store-locator [Google Maps]-related
	if ($e('.store-locator').length){
		$e('#map_search').parent('.nav').append('<div class="block half left"><div class="sub-block small"></div></div>').append('<div class="block half right"></div>');
		$e('.block.half.left .sub-block.small').append('<h3><span class="fc-purple">Dermaclinique Beauty Center</span></h3>').append($e('#map_search')).append($e('#results'));
		$e('.block.half.right').append($e('#map'));
		$e('input#addressInput').before('<span class="fc-purple cufon-thin">CHI / COSA</span><br />').after('<br />');
		$e('select#radiusSelect').before('<span class="fc-purple cufon-thin">RAGGIO</span> ');
		$e('input#addressSubmit').val('TROVA');
		$e('form#searchForm p').before('<br /><br />').addClass('fs-small').html('Si prega di inserire un indirizzo o un termine di ricerca nella casella in alto.');

//		resizeSiblings('.block.half');
		Cufon.replace('.store-locator h3');
		Cufon.replace('.cufon-thin');
	}
	//================================================== /store-locator [Google Maps]-related
	//================================================== forms submit-related
	if ($e('form').length){
		$e('#academySubmit').click(function(){
			if (validateForm()){
				ct_name=$e('input[name="ct-name"]').val();
				ct_email=$e('input[name="ct-email"]').val();
				ct_phone=$e('input[name="ct-phone"]').val();
				ct_course=$e('select[name="ct-course"]').val();
				ct_message=$e('textarea[name="ct-message"]').val();
				$e.ajax({
					type:'post',
					dataType:'json',
					data:'ct-name='+ct_name+'&ct-email='+ct_email+'&ct-phone='+ct_phone+'&ct-course='+ct_course+'&ct-message='+ct_message,
					url:'../wp-content/themes/derma/includes/shared/snippets/ajax-academy-mail.php',
					success:function(data){
						$e('p#message').html(data.msg);
						$e('p#message').slideDown(500);
					},
					error:function(data){
						$e('p#message').html(data.msg);
						$e('p#message').slideDown(500);
					},
					complete:function(){
						return false;
					}
				});
			return false;
			}
		});
		$e('#newsletterSubmit').click(function(){
			ct_mail=$e('input[name="ct-mail"]').val();
			if ((ct_mail=='Inserisci la tua email')||(ct_mail=='')||(! ct_mail))
				return false;
			$e.ajax({
				type:'post',
				dataType:'json',
				data:'ct-mail='+ct_mail,
				url:'../wp-content/themes/derma/includes/shared/snippets/ajax-newsletter-store-mails.php',
				beforeSend:function(){
					$e('p.popup').html('Sottoscrizione...');
					$e('p.popup').slideDown(500);
				},
				success:function(data){
					$e('p.popup').html('GRAZIE PER ISCRIVERTI ALLA NEWSLETTER DI DERMACLINIQUE'+'<br />Email: '+ct_mail);
					$e('p.popup').slideDown(500);
				},
				error:function(data){
					$e('p.popup').html(data.msg);
					$e('p.popup').slideDown(500);
				},
				complete:function(){
				}
			});
			return false;
		});

		$e('form[id="f_search"]').submit(function(){
			window.location.href='http://www.dermaclinique.com/?s='+$e(this).children('input#search').val();;
			return false;
		});
	};
	//================================================== /forms submit-related
	//================================================== cornering-related
	$e('.content .block.full .sub-block .product-info p.details').corner('bottom 8px');
	$e('.content .block.full .sub-block .product-info p.details.first-line').uncorner('bottom');
	$e('.content .block.full .sub-block .product-info p.details.first-line').corner('top 8px');
	$e('.content .block .column.double .sub-block.special table.calendar').corner('5px');
	$e('.content .block .column.double .sub-block.special table.calendar th.first').corner('3px tl');
	$e('.content .block .column.double .sub-block.special table.calendar th.last').corner('3px tr');
	//================================================== /cornering-related
});
//================================================== functions-related
function swapVars(arr){
	var arrLength=arr.length;
	var rarr=new Array();
 	for (i=(arrLength-1);i>=0;i--){
		rarr.push(arr[i]);
	}
	return rarr;
};
function resizeBlocks(obj,size){
	if (! obj.length)
		return false;
	if (size){
		heightToSet=size;
	}
	else{
		heightToSet=$e(obj).parents('.nav').outerHeight();
		if (window.location.href=='http://www.dermaclinique.com/')
			heightToSet+=17.5;
	}
	$e(obj).each(function(){
		$e(this).height(heightToSet);
	});

};
function resizeSiblings(obj){
	height=new Array();
	$e(obj).each(function(){
		height.push($e(this).outerHeight());
	});
	$e(obj).each(function(){
		heightToSet=Math.max.apply(Math,height);
		$e(this).height(heightToSet);
	});
};
$e.fn.clearField=function(){
    return this.focus(function(){
        if(this.value==this.defaultValue){
            this.value='';
        }
    }).blur(function(){
        if(!this.value.length){
            this.value=this.defaultValue;
        }
    });
};
function validateDisclaimer(){
	selected=$e('input[name="disclaimer"]:checked').val();
	if (selected=='false')
		selected=false;
	return selected;
}
function validateForm(){
	obl_check=0;
	$e('input[lang="obligatory"]').each(function(index){
		if (!$e(this).val())
			obl_check++;
	});
	if (obl_check>0){
		alert('* I campi contrassegnati sono obbligatori');
		return false;
	}
	else
		return true;
}
function translateMonths(){
	italian=new Array('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre');
	english=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	for (i=0;i<(english.length);i++){
		$e('.column.right.thin ul.langs li').each(function(){
			value=english[i];
			toMatch=$e(this).find('a').html().split(' ');
			toReplace=italian[i];
			if (toMatch[0].match(value)){
				$e(this).find('a').html(toReplace+' '+toMatch[1]);
				$e(this).find('a').attr('title',toReplace+' '+toMatch[1]);
			}
		});
		$e('span.date').each(function(){
			value=english[i];
			toMatch=$e(this).html().split(' ');
			toReplace=italian[i];
			if (toMatch[0].match(value)){
				$e(this).html(toReplace+' '+toMatch[1]+' '+toMatch[2]);
			}
		});
	}
};
//================================================== /functions-related

