var lock_click = 0; // for slideshow


// JavaScript Document
jQuery(document).ready(function(){

	/* SIDE NAV SLIDERS */
	jQuery('.drawer-handle:not(.open)').siblings('.drawer-content').slideUp('fast');
	jQuery('.drawer-handle').click(function(e){
		var drawer = jQuery(this);
		if (jQuery(this).hasClass('open')){
		}else{
		jQuery('.drawer-handle.open').siblings('.drawer-content').slideUp('slow', function(){
			jQuery(drawer).siblings('.drawer-content').slideDown('slow').siblings('.drawer-handle').addClass('open');
			}).siblings('.drawer-handle').removeClass('open');
		}
	});


	jQuery('.dropdown_link').toggle(function(){
		 jQuery(this).addClass('active');
		 jQuery(this).siblings('.filter_list').removeClass('closed');
		 jQuery(this).parent('.filter').removeClass('closed');
		 jQuery('.filter_list.closed').animate({ height: 'hide', opacity: 'hide' }, 'slow');
		 jQuery('.filter.closed').animate({ height: 'hide', opacity: 'hide' }, 'slow');
		 jQuery(this).siblings('.filter_list').animate({ height: 'show', opacity: 'show' }, 'slow');
		 jQuery(this).css('backgroundPosition', '0px -19px');
		}, function(){
		 jQuery('.filter').addClass('closed').animate({ height: 'show', opacity: 'show' }, 'slow');
		 jQuery('.filter_list').addClass('closed');
		 jQuery(this).siblings('.filter_list').animate({ height: 'hide', opacity: 'hide' }, 'slow');
		 jQuery(this).removeClass('active');
		 jQuery(this).css('backgroundPosition', '0px 0px');
		});

		jQuery('.filter_list a').click(function(){
		 jQuery('.filter').addClass('closed').animate({ height: 'show', opacity: 'show' }, 'slow');
		 jQuery('.filter_list').addClass('closed');
		 jQuery(this).parents('.filter_list').animate({ height: 'hide', opacity: 'hide' }, 'slow');
		 var link_text = jQuery(this).text();
		 jQuery(this).parent().parent().siblings('.dropdown_link').removeClass('active').text(link_text).trigger('click');
		 jQuery(this).parent().parent().siblings('.filter_field').val(link_text);
		});


	jQuery(function()
		{
			jQuery('.scroll-pane').jScrollPane({scrollbarWidth:22, scrollbarMargin:7, dragMinHeight: 76, dragMaxHeight: 76});
		});

	jQuery('.jScrollPaneDrag').mouseover(function(){
			  jQuery(this).addClass('jScrollPaneDrag_hover');
				}).mouseout(function(){
			  jQuery(this).removeClass('jScrollPaneDrag_hover');
			  });

	jQuery('.leaderThumbnails a').click(function(e){
			e.preventDefault();
			var new_img = jQuery(this).children('img').attr('src');
			var old_img = jQuery('#main_image a').children('img').attr('src');
			jQuery(this).children('img').attr('src', old_img);
			jQuery('#main_image a').children('img').attr('src', new_img);


			var new_img_rel = jQuery(this).attr('rel');
			var new_img_id = jQuery(this).attr('id');
			var old_img_rel = jQuery('#asset_main').children('a').attr('href');
			var old_img_id = jQuery('#asset_main').children('a').attr('id');
			//console.log(old_img_id);
			jQuery(this).attr('rel', old_img_rel).attr('id', old_img_id);
			jQuery('#asset_main').children('a').attr('href', new_img_rel).attr('id', new_img_id);
			//jQuery('.btn_art_zoom').attr('href', new_img_rel).attr('id', new_img_id);
		});


	// art detail - horizontal centering
	//setTimeout("verticalCenterMainArt();", 500);
	verticalCenterMainArt();



	// art thumbs
	jQuery('#art_detail .normal_view .thumbBox a').click(function(e){
			e.preventDefault();
			var new_img_rel = jQuery(this).attr('rel');
			var new_img_id = jQuery(this).attr('id');
			var old_img_rel = jQuery('#asset_main').children('a').attr('href');
			var old_img_id = jQuery('#asset_main').children('a').attr('id');
			jQuery(this).attr('rel', old_img_rel).attr('id', old_img_id);
			jQuery('#asset_main').children('a').attr('href', new_img_rel).attr('id', new_img_id);
			jQuery('.btn_art_zoom').attr('href', new_img_rel).attr('id', new_img_id);
			verticalCenterMainArt();

		});

// OBSOLETE:
// scale view start
//	jQuery('#lnk_show_to_scale').click(function(e){
//		var link_text = jQuery(this).text();
//		if(link_text == 'Show to scale'){
//			jQuery(this).text('Zoom view').removeClass('scale_view').addClass('zoom_view');
//			jQuery('#zoom_controls, #pagination, #art_overlay .itemUtilitiesContainer').hide();
//		}else{
//			jQuery(this).text('Show to scale').removeClass('zoom_view').addClass('scale_view');
//			jQuery('#zoom_controls, #pagination, #art_overlay .itemUtilitiesContainer').show();
//		}
//	});

	jQuery('#asset_main a, .btn_art_zoom').click(function(e){
			e.preventDefault();
			var large_img_href = jQuery(this).attr('href');
			var large_img_id = jQuery(this).attr('id');
			jQuery('#lb_main_asset').attr('src', large_img_href).parent().attr('id', large_img_id);
		});

	jQuery('.lb_view .thumbBox a').click(function(e){
			e.preventDefault();
			var new_img = jQuery(this).attr('href');
			var new_img_id = jQuery(this).attr('id');
			jQuery('#lb_main_asset').attr('src', new_img).parent().attr('id', new_img_id);
		});


	jQuery('#lb_next').click(function(e){
			e.preventDefault();
			updateLbMainAsset('next');
		});

	jQuery('#lb_prev').click(function(e){
			e.preventDefault();
			updateLbMainAsset('prev');
		});

	function updateLbMainAsset(dir){
		var total_thumbs = jQuery('.lb_view').children().size();
		//console.log(total_thumbs);
		var current_img_id = parseInt(jQuery('#lb_main_asset').parent().attr('id'));
		//console.log(current_img_id);
		if(dir == 'next'){
			if (current_img_id < total_thumbs){
				var new_img_id = current_img_id + 1;
			}else{
				var new_img_id = 1;
			}
		}
		if(dir == 'prev'){
			if (current_img_id == 1){
				var new_img_id = total_thumbs;
			}
			else{
				var new_img_id = current_img_id - 1;
			}
		}
		var new_img_path = jQuery('.lb_view .thumbBox a#' +new_img_id).attr('href');
		//console.log(new_img_path);
		jQuery('#lb_main_asset').attr('src', new_img_path).parent().attr('id', new_img_id);
	}



	/* SLIDESHOW EXHIBITIONS */

	if (jQuery('#exhibition_id').length > 0){
		jQuery('#popup_area').click(function(){
			clearInterval(slideshow);
		});
	}


	jQuery('#btn_next').click(function(){
		playNextSlide();
	});

	jQuery('#btn_prev').click(function(){
		playPrevSlide();
	});


	jQuery('.exhibit_photo').each(function(i) {
		var main_img_height = jQuery(this).find('img').attr('height');
		//jQuery(this).children('img').css('paddingTop', '0px');
		if (main_img_height < 478){
			var margin_top = (478 - main_img_height) / 2 ;
			//jQuery(this).children('img').css('marginTop', margin_top);
		}
		});


	jQuery('#exhibit_thumbs .thumbBox a').click(function(e){
		if (lock_click == 0){
			e.preventDefault();
			lock_click = 1;
			var link_id = jQuery(this).get(0).id;
			jQuery('.slide').each(function(i) {
				if(jQuery(this).css('display') != 'none'){
					jQuery(this).fadeOut('slow', function(i){
							jQuery('#image_' + link_id).fadeIn('slow', function(d){
								lock_click = 0;
								});
						});
					clearInterval(slideshow);
				}

				});
		}
		});


});


function playPrevSlide(){
		jQuery('.slide').each(function(i) {
			if (jQuery(this).css('display') != 'none') {
				jQuery(this).find('.prev').trigger('click');
				//console.log('trigger');
			}
		});
	}

function playNextSlide(){
		jQuery('.slide').each(function(i) {
			if (jQuery(this).css('display') != 'none') {
				jQuery(this).find('.next').trigger('click');
				//console.log('trigger');
			}
		});
	}


function readSlideshowCounter(){
	var counter = jQuery('#slideshow_counter').text();
	console.log(counter);
	return counter;
}


function restartSlideshow() {
	//playNextSlide();
	//Exhibition.Open('slideshow_popup');
	slideshow2 = setInterval('playNextSlide();',6000);

}

function verticalCenterMainArt(){
	var main_img_height = jQuery('#art_detail #asset_main').find('img').height();
	if (main_img_height < 370){
		var margin_top = (370 - main_img_height) / 2;
		jQuery('#art_detail #asset_main').children('a').children('img').css('marginTop', margin_top);
	}
}

// OBSOLETE:
//function showScaleView(){
//	//jQuery('#lnk_show_to_scale').text('Zoom view').removeClass('scale_view').addClass('zoom_view');;
//	//jQuery('#zoom_controls, #pagination, #art_overlay .itemUtilitiesContainer').hide();
//	jQuery('#show_to_scale').show();
//	jQuery('#standard_zoom').hide();
//	setTimeout("Art.Zoom('in');", 1000);
//}
//
//function showZoomView(){
//	jQuery('#lnk_show_to_scale').text('Scale view').removeClass('zoom_view').addClass('scale_view');;
//	jQuery('#zoom_controls, #pagination, #art_overlay .itemUtilitiesContainer').show();
//	jQuery('#show_to_scale').hide();
//	jQuery('#standard_zoom').show();
//}



// input field text reset functions
function resetField(field, value){
	if (field.value==value){
		field.value="";
		return false;
	}
}

function startValue(field, text){
			if (field.value==""){
			field.value=text;
			return false;
			}
}


