jQuery(function($){

	$.fn.unwrap = function() {
		this.parent(':not(body)')
		.each(function(){
	  		$(this).replaceWith( this.childNodes );
		});
		return this;
	};

	function auto_size(selector) {
		var target_h = 0;
		$(selector).each(function() {
			
			if($(this).height() > target_h) {
				target_h = $(this).height();
			}
			if($(this).find('.artworks-cnt').height() > target_h) {
				target_h = $(this).find('.artworks-cnt').height();
			}
		});
		$(selector).height(target_h);
	}

	function change_home_slide() {
		var current = $('.slider ul li').index($('.slider ul li:visible'));
		var total = $('.slider ul li').length;
		var next;
		next = (current < total - 1) ? current + 1 : next = 0;
		$('.slider ul li:eq(' + next + ')').fadeIn(4000);
		$('.slider ul li:eq(' + current + ')').fadeOut(4000);

	}
	var interval = setInterval(change_home_slide, 8000);
	

	// Homepage menu 
	$('#menu .hide').click(function(){
		var exp = $(this).parent().find('.expander');
		var wrapper = $(this).parents('.wrapper');
		var speed = 200;
		if (parseInt(exp.css('width')) == 0 && !exp.is(':animated')) {
			wrapper.animate({'width': '199px'}, speed);
			exp.animate({'width': '159px'}, speed)
		} else {
			wrapper.animate({'width': '40px'}, speed);
			exp.animate({'width': 0}, speed);
		}
		$(this).toggleClass('show');
		return false;
	});
	
	// Hover on Artists (Homepage)
	
	$('.list-artists td').hover(
		function() { 
			$(this).addClass('hover'); 
		},
		function() { 
			$(this).removeClass('hover'); 
		}
	);
	
	// Hover on Upcoming Events(Homepage)
	$('.upcoming-events td').hover(
		function() { 
			$(this).parent().addClass('hover'); 
		},
		function() { 
			$(this).parent().removeClass('hover'); 
		}
	);
	
	$('.upcoming-events td, .list-artists td').click(function() {
		window.location.href=$(this).find('a').attr('href');
	});
	
	// Hover on each artist (artists landing page)
	$('.artists .column ul li a, .more-artists ul li a').hover(
		function() {
			var rel = $(this).attr('rel');
			var target_img = $('.artist-images img.' + rel);
			if (target_img) {
				target_img.show();
			}
		},
		function() {
			var rel = $(this).attr('rel');
			var target_img = $('.artist-images img.' + rel);
			if (target_img) {
				target_img.hide();
			}
		}
	);

	
	function author_slider_slide(step) {
		var cont_w = $('.artist-slider ul').width();
		var visible_w = $('.artist-slider-cnt').width();
		if($('.artist-slider-scroller:animated').length == 0 && cont_w > visible_w) {
			if (Math.abs(step) > cont_w - visible_w) {
				step += Math.abs(step) - (cont_w - visible_w) - 50;
			} 
			if(step > 0) {
				step = 0;
			}
			$('.artist-slider-scroller').animate({'left': step + 'px'}, 500);
		}
	}
	
	$('.artist-slider-controls a').click(function() {
		var step = 292;
		var curr_left = parseInt($('.artist-slider-scroller').css('left'));
		if($(this).hasClass('prev')) {
			curr_left += step;
		} else if($(this).hasClass('next')) {
			curr_left -= step;
		}
		author_slider_slide(curr_left);
		return false;
	});

	function artist_auto_size() {
		var h = 0;
		$('.wide-content .auto-size-cnt').each(function() {
			if($(this).outerHeight() > h) {
				h = $(this).outerHeight();
			}
		});
		
		$('.wide-content .auto-size').each(function() {
			$(this).height(h);
		});
	}
	
	function extract_url(input) {
		return input.replace(/"/g,"").replace(/url\(|\)$/ig, "");
	}
	
	var thin_column_width = 350;
	var full_column_width = 624;
	var image_wrap = '<div class="column-left auto-size"><div class="artworks-cnt auto-size-cnt"></div></div>';
	var details_wrap = '<div class="column-right auto-size"><div class="about-artist auto-size-cnt"></div></div>';
	var full_image_wrap = '<div class="fullwide-container artist-image-container artworks-cnt"></div>';
	var full_details_wrap = '<div class="fullwide-container artist-details-container"></div>';
	
	$('.artist-slider-cnt ul li a').live('click', function() {
		var idx = $('.artist-slider-cnt ul li a').index($(this));
		var visible = $('.artist-slider-cnt ul li a').index($('.artist-slider-cnt ul li a.active'));

		//if(idx != visible) {
			var current = $('.artwork-content:eq(' + visible + ')');
			var next = $('.artwork-content:eq(' + idx + ')');
			
			
			if ($(this).parents('.auto-change-markup').length > 0) {
				var current_img = new Image();
				var next_img = new Image();
				
				current_img.src = extract_url(current.find('.artist-main-image img').css('background-image'));
				
				next_img.src = extract_url(next.find('.artist-main-image img').css('background-image'));
				
				var current_w = current_img.width;
				var next_w = next_img.width;
				
				if (next_w > thin_column_width) {
					if (current.parents('.fullwide-container').length == 0) {
						$('.artwork-wrapper').unwrap().unwrap().wrap(full_image_wrap);
						$('.author-details-wrapper').unwrap().unwrap().wrap(full_details_wrap);
					}
					
				} else {
					if (current.parents('.fullwide-container').length > 0) {
						$('.artwork-wrapper').unwrap().wrap(image_wrap);
						$('.author-details-wrapper').unwrap().wrap(details_wrap);
					}
				}				
			}
			
			
			$('.artwork-content:eq(' + visible + ')').hide();
			$('.artist-slider-scroller ul li a.active').removeClass('active');
			$('.artist-slider-scroller ul li:eq(' + idx + ') a').addClass('active');
			next.show();
			var target_h = next.height();
			$('.artworks-cnt').height(target_h);
			artist_auto_size();
		//}
		return false;
	});
	
	$('.video-content').click(function() {
		$.fancybox({
			'padding' : 0,
			'autoScale' : false,
			'title' : this.title,
			'width' : 680,
			'height' : 495,
			'href' : this.href,
			'type' : 'swf',
			'swf' : {
			   	'wmode' : 'transparent',
				'allowfullscreen': 'true'
			}
		});
		
		return false;
	});

	$("a.fancybox").fancybox({});
	
	$('a.fancybox-video').fancybox({
		'padding': 0
	});
	
	
	$('.event-details .more a').click(function() {
		$(this).parents('.event-details:eq(0)').find('.hidden-content').show();
		$(this).parent().hide();
		return false;
	});
	
	$('.simple-text .text:last').addClass('last-text');
	
	$('.main-content .post:last').addClass('last-post');

	
	auto_size('.home .columns .auto-size');
	$(window).load(function() {
		artist_auto_size('.wide-content .auto-size');
		auto_size('.adjust-height');
		$('.artist-slider-cnt ul li a:eq(0)').addClass('active').click();
	});
	
	$('.home-artists-navigation a').click(function() {
		if ($('.home-artists-table-slider:animated').length > 0) {
			return false;
		}
		var curr_top = parseInt($('.home-artists-table-slider').css('top'));
		var total_h = $('.home-artists-table-slider').height();
		var cont_h = $('.home-artists-table').height();
		var step = 44;
		
		if ($(this).hasClass('nav-top')) {
			if (curr_top * -1 < step) {
				curr_top = 0;	
			} else {
				curr_top += step;
			}
		} else if($(this).hasClass('nav-bottom')) {
			if (curr_top * -1 + step > total_h - cont_h) {
				curr_top = (total_h - cont_h) * -1;
			} else {
				curr_top -= step;
			}
		}
		
		$('.home-artists-table-slider').animate({'top': curr_top + 'px'}, 500);
		return false;
	});
	
});

