em = function(name1,name2,text,subject,domain) {
  if (!domain) {domain='styl.fi'}
  if (!name2) { name=name1; } else { name=name1+'.'+name2}
  if (!text) {
  	 if (!name2) {text=name1+'&#64;'+domain; }
	 else { text=name1+'.'+name2+'&#64;'+domain; }
  }
  if (!subject) {document.write('<a href="mailto:'+name+'&#64;'+domain+'">'+text+'</a>');}
  else {document.write('<a href="mailto:'+name+'&#64;'+domain+'?subject='+escape(subject)+'">'+text+'</a>');}
}


$(document).ready(function() {

	$("#top-login a").click(function() { 
		$("#top-login-form").fadeIn();
	});
	$("#login-form-close").click(function() { 
		$("#top-login-form").fadeOut();
	});

	// Calendar
	if ($('#kalenteri').length) {
		var calendar = function () {
			$("#eventcalendar-nav").find("a").each(function(i) {
				$(this).click(function() {
					$('#kalenteri').append('<div class="loading"></div>');
					 $("#kalenteri .loading").ajaxStart(function(){
					   $(this).show();
					});
					 $.ajax({ url: this.href+'&get_page_html=1',
						success: function(data) {
							$('#kalenteri').html(data);
							calendar();
							happen();
						}
					 });
					$("#kalenteri .loading").ajaxStop(function(){
					   $(this).remove();
					});
					 return false;
				});										 
			});
			/*
			$("#eventcalendar-caltable").find("a").each(function(i) {
				
			});
			*/
		}
		var happen = function () {
				if ($('#cal-wrap').length) {
					$("#eventcalendar-caltable").find("a").each(function(i) {
						$(this).click(function() {
							$('#kalenteri_tapahtumat').append('<div class="loading"></div>');
							 $("#kalenteri_tapahtumat .loading").ajaxStart(function(){
							   $(this).show();
							});
							 $.ajax({ url: this.href+'&get_page_html=1',
								success: function(data) {
									$('#kalenteri_tapahtumat').html(data);
								}
							 });
							 $("#kalenteri_tapahtumat .loading").ajaxStop(function(){
							   $(this).hide();
							});
							return false;
						});										 
					});
				}
			}
		
			if ($('#kalenteri-listaus').length) {
				var happen_list = function () {
					$("#kalenteri-listaus").find("a").each(function(i) {
						$(this).click(function() {
							$('#kalenteri_tapahtumat').append('<div class="loading"></div>');
							 $("#kalenteri_tapahtumat .loading").ajaxStart(function(){
							   $(this).show();
							});
							 $.ajax({ url: this.href+'&get_page_html=1',
								success: function(data) {
									$('#kalenteri_tapahtumat').html(data);
								}
							 });
							 $("#kalenteri_tapahtumat .loading").ajaxStop(function(){
							   $(this).hide();
							});
							return false;
						});										 
					});
				}
				happen_list();
			}
			calendar();
			happen();
	}
	if ($('#products').length) {
		$('table.products tr').each(function (i) {
			$(this).find(".company-info").css('display','none');
			$(this).hover(
			 	function () {
					if ($(this).find(".show").hasClass('arrow-up')) {
						$(this).css("background","#f1f1f1").css("cursor","pointer");
					}
			  	}, 
			 	function () {
					$(this).css("background","#ffffff");
			  	}
			);
			
			
			if ($(this).find(".company-info").length) {
				$(this).click(function () {
					if ($(this).find(".show").hasClass('arrow-up')) {
						$(this).css("cursor","auto").css("background","#ffffff");
						closeOpened();
						$(this).find(".company-info").slideDown();
						el = $(this).find('.show');
						el.removeClass('arrow-up').addClass('arrow-down');
					}
				});	
			}
		});
		// Piilotus
		var hide_rows = function(col_number) {
			$('table.products tr').each(function (i) {
				if (i>0) {
					if (!($(this).find('td').eq(col_number).find('span').hasClass('yes'))) {
						$(this).hide();
					}
				}
			});
		}
		rows_hidden = false;
		active_class = "";
		
		var change_opacity = function (class_name,show) {
			$('#tuoteryhmat').find('img').each(function(i) {
				if (show && class_name!=i) {
					$(this).fadeTo('fast', 0.25);
				} else {
					$(this).fadeTo('fast',1);
				}
			});
			$('#tuoteryhma-names').find('li').each(function(i) {
				if (show && class_name!=i) {
					$(this).fadeTo('fast', 0.25);
				} else {
					$(this).fadeTo('fast',1);
				}
			});
		}
		
		$('#tuoteryhmat').find('img').each(function(i) {
			class_name = $(this).attr('class');
			$('.'+class_name).hover(function () {
				$(this).css('cursor','pointer');
			});
			$('.'+class_name).click(function () {
				if (rows_hidden) {
					$('table.products tr').each(function (i) {
						$(this).show();
					});
					rows_hidden = false;
					change_opacity(i,false);
				} else {
					hide_rows (i+1);
					rows_hidden = true;
					change_opacity(i,true);
				}
			});	
		});
		
		
		
		
		
		
		// Lajittelu
		$(".products").tablesorter();
		//Tooltip
		$(".products .c0-tooltip").tooltip({
			tip: '#c0-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c1-tooltip").tooltip({
			tip: '#c1-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c2-tooltip").tooltip({
			tip: '#c2-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c3-tooltip").tooltip({
			tip: '#c3-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c4-tooltip").tooltip({
			tip: '#c4-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c5-tooltip").tooltip({
			tip: '#c5-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c6-tooltip").tooltip({
			tip: '#c6-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c7-tooltip").tooltip({
			tip: '#c7-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c8-tooltip").tooltip({
			tip: '#c8-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c9-tooltip").tooltip({
			tip: '#c9-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c10-tooltip").tooltip({
			tip: '#c10-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c11-tooltip").tooltip({
			tip: '#c11-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c12-tooltip").tooltip({
			tip: '#c12-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c13-tooltip").tooltip({
			tip: '#c13-tooltip',
			position: 'center right',
			delay: 0
		});
		$(".products .c14-tooltip").tooltip({
			tip: '#c14-tooltip',
			position: 'center right',
			delay: 0
		});
		
	}
	
	function closeOpened() {
		$('table.products tr').each(function (i) {
			if ($(this).find(".show").hasClass('arrow-down')) {
				$(this).find(".company-info").slideUp();
				el = $(this).find('.show');
				el.removeClass('arrow-down').addClass('arrow-up');	
			}
		});		
	}
	
	
	/*
	if ($('#products').length) {
		$('table.products tr').each(function (i) {
			if ($(this).find(".company-info").length) {
				element = $(this);
				$(this).bind("click", function () {
					if ($(this).find(".show").hasClass('arrow-up')) {
						$(this).find(".company-info").slideDown();
						el = $(this).find('.show');
						el.removeClass('arrow-up').addClass('arrow-down');
						$(this).unbind("click");
						el.bind("click", function () {
							$(this).find(".company-info").slideUp();
							el = $(this).find('.show');
							el.removeClass('arrow-down').addClass('arrow-up');
							$(this).bind("click");	
						 });
					}
					else {
						alert ("test");	
					}
				});
			}
		});
	}
	function showInfo(element) {
		if (element.find(".show").hasClass('arrow-up')) {
			element.find(".company-info").slideDown();
			el = element.find('.show');
			el.removeClass('arrow-up').addClass('arrow-down');
			$(this).unbind("click");
		} else {
			element.find(".company-info").slideDown();
			el = element.find('.show');
			el.removeClass('arrow-down').addClass('arrow-up');
			$(this).bind("click", showInfo(element));
		}
	}
	*/
});
