$(document).ready(function() {
/*	$('.review-content').hide();
	$('.review-container h3').hover(function(){
		if( $(this).next().is(':hidden') ) {
			$('.review-container h3').removeClass('active').next().slideUp();
			$(this).toggleClass('active').next().slideDown();
		}
		return false;
	});*/
	
	if ($('#submenu').find('ul li').length < 1) {
		$('#wrapper').removeClass('inner');
		$('#sub').css('border','none');
	}
	
	if ($('.review-container').length) {
		$('.review-container').hoverAccordion({ 
		  keepHeight: false,
		  speed: 'fast' 
		});
		$('.review-container ul').each(function(j) {
			//alert ($(this).html());
			$(this).click(function() {
				window.location.href = $(this).parent().find("a").attr('href');
			});
			$(this).hover(function() {
				$(this).css('cursor','pointer');
			});
		});
	}

	
	$(window).load(function() {
		$(":input").each(function(i) {
			type = this.type;
			if (type == "submit" || type == "button") {
				this.className = "button";
			}
			if (type == "radio") {
				this.className = "no-border";
			}
			if (type == "checkbox") {
				this.className = "no-border";
			}
		 });
	 });
	
	
	if ($('#callout-askoffer').length) {
		$('#callout-askoffer').hover(
		  function () {
			$(this).css('background-position','bottom left');
			$(this).css('cursor','pointer');
		  }, 
		  function () {
			$(this).css('background-position','top left');
		  }
		);
		$('#callout-askoffer').click(function() {
			window.location.href = $(this).find("a").attr('href');
		})
	}

	var set_artists = function() {
		$('div.artist').hover(
		  function () {
			$(this).css('cursor','pointer');
			$(this).addClass('mhover');
		  }, 
		  function () {
			$(this).removeClass('mhover');
		  }
		);
		$('div.artist').click(function() {
			window.location.href = $(this).find("a").attr('href');
		})
		
		if ($('#artist-review').length) {
			act_artist = $('input[name=active_artist]').val();
			if ($('#'+act_artist).length) {
				$('#'+act_artist).addClass('active');
			}
			if ($('.artist:first').hasClass('active')) {
				$('#arrow-next-artist').fadeIn('slow');
			} 
			else if ($('.artist:last').hasClass('active')) {
				$('#arrow-previous-artist').fadeIn('slow');
			} else {
				$('#arrow-next-artist').fadeIn('slow');	
				$('#arrow-previous-artist').fadeIn('slow');
			}
			
			
			$('#arrow-next-artist').hover(
			  function () {
				$(this).css('cursor','pointer');
				$(this).addClass('mhover');
			  }, 
			  function () {
				$(this).removeClass('mhover');
			  }
			);
			
			$('#arrow-previous-artist').hover(
			  function () {
				$(this).css('cursor','pointer');
				$(this).addClass('mhover');
			  }, 
			  function () {
				$(this).removeClass('mhover');
			  }
			);
			
			$('#arrow-next-artist').click(function() {
				$('#artists-list-data').fadeTo(100,0);
				$('#artist-review').css('position','absolute').css('left','9px');
				$('#artist-container').css('height',$('#artist-review').outerHeight());
				$('#artist-review').animate({ left: '-960px' }, 500, function() {
					window.location.href = $('.artist.active').parent().next().find('a').attr('href');
				});
			});
			
			$('#arrow-previous-artist').click(function() {
				$('#artists-list-data').fadeTo(100,0);
				$('#artist-review').css('position','absolute').css('left','9px');
				$('#artist-container').css('height',$('#artist-review').outerHeight());
				$('#artist-review').animate({ left: '960px' }, 500, function() {
					window.location.href = $('.artist.active').parent().prev().find('a').attr('href');
				});
			});
			
		}
		
	}
	
	if ($('.artist').length) {
		set_artists();
	}


	if ($('#callout-text-data').length) {
		$('#callout-text-data').fadeTo(0, 0);
		get_ajax_content ('#callout-text-data','',false);
	}
	if ($('#artists-list-data').length) {
		$('#artists-list-data').fadeTo(0, 0);
		get_ajax_content ('#artists-list-data','_c2',false,set_artists);
	}
	
	function get_ajax_content (contentDiv,col,showLoading,func) {
		var pid = $('input[name=pid]').val();
		if (showLoading) {
			$(contentDiv).append('<div class="loading"></div>');
			$(contentDiv+" .loading").ajaxStart(function(){
			   $(this).show();
			});
		}
		$.ajax({ url: '/?id='+pid+'&get_page_html'+col+'=1',
			success: function(data) {
				$(contentDiv).html(data);
				$(contentDiv).fadeTo('fast',1);
				if (func) {
					func();
					$('#artists-list-data').css('height','auto');
				}
			}
		 });
		 if (showLoading) {
			 $(contentDiv+" .loading").ajaxStop(function(){
			   $(this).hide();
			});
		 }
	}
	
	
	if ($('input[name=article-id]').length) {
		act_article = $('input[name=article-id]').val();	
		if ($('#'+act_article).length) {
			$('#'+act_article).addClass('active_article');
		}
	}
	
	
	if ($(".image_ticker").length) {
		var ticker_images = 0;
		var tickerImageItems = [];
		$('.image_ticker').shuffle();
		$(".image_ticker li").each(function(j) {
			tickerImageItems[j] = ($(this).html());
		});
		imageTicker();
	}
	function imageTicker(){
		if( ticker_images == tickerImageItems.length ){
			ticker_images = 0;
		}
		$('#show_image_ticker').fadeOut("slow", function(){
			$(this).html(tickerImageItems[ticker_images]).fadeIn("slow");
			ticker_images++;
		});
		if (tickerImageItems.length > 0) {
			setTimeout(function() { imageTicker() }, 6000);
		}
	}
	
	/*
	if ($('#main input').length) {	$('#main input').corner("round 5px"); }
	if ($('.review-container').length) {	$('.review-container').corner("round 5px"); }
	
	*/
	
});
(function($){
  $.fn.shuffle = function() {
	return this.each(function(){
	  var items = $(this).children();
	  return (items.length)
		? $(this).html($.shuffle(items))
		: this;
	});
  }
 
  $.shuffle = function(arr) {
	for(
	  var j, x, i = arr.length; i;
	  j = parseInt(Math.random() * i),
	  x = arr[--i], arr[i] = arr[j], arr[j] = x
	);
	return arr;
  }
})(jQuery);

em = function(name1,name2,text,subject,domain) {
  if (!domain) {domain='ohjelmakauppa.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>');}
}
