jQuery(document).ready(function(){
	jQuery("#content div.infobox div.info div.value a").parent().css('opacity', 1.0);

	jQuery("#content div.infobox div.info div.value a").hover(
		// hover state
		function() {
			jQuery(this).parent().css('opacity', 0.7);
		},
		// back to off
		function() {
			jQuery(this).parent().css('opacity', 1.0);
		}
	);

	var IdLocale = parseInt($('body div.hidden_data div.IdLocale').text());
	var IdLocaleOld = parseInt(getCookie('IdLocale'));
	IdLocaleOld = (((typeof IdLocaleOld == "undefined") || isNaN(IdLocaleOld)) ? 0 : IdLocaleOld);
	if (IdLocale != IdLocaleOld) {
		setCookie('#scroll_txt_localepersist', '');
		setCookie('#scroll_eventi_localepersist', '');
		setCookie('#scroll_news_localepersist', '');
		setCookie('#scroll_menu_localepersist', '');
		setCookie('#scroll_ricette_localepersist', '');
		setCookie('#scroll_rassegna_localepersist', '');
		setCookie('tabpanel_number_persist_locale', '');
	}
	setCookie('IdLocale', IdLocale);

	$('img').bind("contextmenu",function(e){
		return false;
	});

	jQuery("#content_locale div.cnt1 div.row1 div.right div.icon").css("opacity", 0.8);

	jQuery("#content_locale div.cnt1 div.row1 div.right div.icon").hover(
		// hover state
		function() {
			jQuery(this).css({opacity: 1.0});
		},
		// back to off
		function() {
			jQuery(this).css({opacity: 0.8});
		}
	);

	jQuery("#thumbbuttonleft").click(function(){
		if (stepcarousel) {
			var configholder_id = $('div.thumbpanels').attr('id');
			var config = stepcarousel.configholder[configholder_id];
			if (typeof config != "undefined"){
				if (config.currentpanel > 0) stepcarousel.stepBy(configholder_id, -1);
			}
		}
	});

	jQuery("#thumbbuttonright").click(function(){
		if (stepcarousel) {
			var configholder_id = $('div.thumbpanels').attr('id');
			var config = stepcarousel.configholder[configholder_id];
			if (typeof config != "undefined"){
				if (config.currentpanel < config.lastvisiblepanel) stepcarousel.stepBy(configholder_id, 1);
			}
		}
	});

	jQuery("div.thumbpanels .thumb").click(function(){
		var url = $(this).find('.hidden_data .url').text();
		var urlbig = $(this).find('.hidden_data .urlbig').text();
		var title = $(this).find('.hidden_data .title').text();

		if (url) {
			jQuery("#content_locale div.cnt2 div.row1 div.left div.imgbig img:first").attr('src', url);
			jQuery("#content_locale div.cnt2 div.row1 div.left div.imgbig img:first").attr('title', title);

			if (urlbig) {
				jQuery("#content_locale div.cnt2 div.row1 div.left div.imgbig a").attr('href', urlbig);
				jQuery("#content_locale div.cnt2 div.row1 div.left div.imgbig a").attr('title', title);
			}
		}
	});

	var tab_elements = jQuery("#content_locale div.cnt3 div.rowtab div.intab");
	var tab_num_elements = tab_elements.length;
	for(var i = 0; i < tab_num_elements; i++) {
		$tab = $(tab_elements[i]);
		if ($tab.hasClass('tab')) {
			$tab.css('cursor', 'pointer');
			$tab.click(
				function () {
					select_tab($(this));
				}
			);

			$(tab_elements[i - 1]).css('cursor', 'pointer');
			$(tab_elements[i - 1]).click(function() {select_tab($(this).next('.tab'))});

			$(tab_elements[i + 1]).css('cursor', 'pointer');
			$(tab_elements[i + 1]).click(function() {select_tab($(this).prev('.tab'))});

			if ((i - 5) > 0) {
				if ($(tab_elements[i - 5]).hasClass('tab')) {
					$(tab_elements[i - 2]).css('cursor', 'pointer');
					$(tab_elements[i - 2]).click(function() {select_tab($($(this).prev().prev().prev()))});
				}
			}

			if ((i + 5) < tab_num_elements) {
				if ($(tab_elements[i + 5]).hasClass('tab')) {
					$(tab_elements[i + 2]).css('cursor', 'pointer');
					$(tab_elements[i + 2]).click(function() {select_tab($($(this).next().next().next()))});
				}
			}
		}
	}

	var gotomdn = $('.hidden_data .gotomdn').text();
	if (gotomdn) {
		select_tab($(jQuery("#content_locale div.cnt3 div.rowtab .tab").get(gotomdn - 1)));
	}
});

jQuery(window).bind('unload', function(){ //clean up
})

/*
------------------ HELPER FUNCTIONS ------------------------
*/
function select_tab(tab) {
	if (tab && tab.hasClass('tab')) {
		reset_tab();

		tab.prev().prev().removeClass("intab1_closed").addClass("intab1_opened");
		tab.prev().removeClass("intab2_closed").addClass("intab2_opened");
		tab.removeClass("tab_closed").addClass("tab_opened");
		tab.next().removeClass("intab1_closed").addClass("intab1_opened");
		tab.next().show();
		tab.next().next().removeClass("intab2_closed").addClass("intab2_opened");
		tab.next().next().show();

		var left1 = tab.next().next().next();
		var left2 = tab.next().next().next().next();
		if (left1 && left2) {
			left1.addClass("prev_opened");
			left2.addClass("prev_opened");
		}

		var panelnumber = tab.find('.hidden_data .panelnumber').text();
		setCookie('tabpanel_number_persist_locale', panelnumber);
		if (stepcarousel) {
			var configholder_id = $('div.tabpanels').attr('id');
			var config = stepcarousel.configholder[configholder_id];
			if (typeof config != "undefined"){
				stepcarousel.stepTo(configholder_id, panelnumber);
			}
		}
	}
}

function reset_tab() {
	jQuery("#content_locale .cnt3 .rowtab .prev_opened").each(function(){
		$(this).removeClass("prev_opened");
	});

	jQuery("#content_locale div.cnt3 div.rowtab div.intab1_opened").each(function(){
		$(this).removeClass("intab1_opened").addClass("intab1_closed");
	});

	jQuery("#content_locale div.cnt3 div.rowtab div.intab2_opened").each(function(){
		$(this).removeClass("intab2_opened").addClass("intab2_closed");
	});

	jQuery("#content_locale div.cnt3 div.rowtab div.tab_opened").each(function(){
		$(this).removeClass("tab_opened").addClass("tab_closed");
	});

	jQuery("#content_locale div.cnt3 div.rowtab div.right").each(function(){
		$(this).parent().hide();
	});
}
