// Caldwell Snyder Gallery
// Written by Nic Hubbard - Zed Said Studio
// http://www.zedsaid.com

$(document).ready(function() {
						   
	// Check to see if we are in simple edit
	var simple_edit = parseFloat($('#simple_edit_check').val());
	
	//
	// ----- #### Home #### -----
	//
	
	// ----- Homepage Slideshow -----
	if ($('#home_exhibition_hold').length) {
		$('#home_exhibition_hold').cycle({ 
			fx:      'fade', 
			speed:    2000, 
			timeout:  7000,
			pause: 	0,
			pager:   '#slide_pager'
		});	
	}
	if ($('.home_slides').length < 2) {
		$('#slide_pager').hide();
	}
	
	// ----- Remove Text in Pager -----
	$('#slide_pager a').each(function() {
		$(this).text('');							  
	});
	// Add our preview images to the pager
	var i = 0;
	$('#slide_pager a').each(function() {
		$(this).append('<img src="' + pager_arr[i] + '" alt="Exhibition Pager Image" />');
		i++;
	});
	
	
	// Remove reception is start/end is the same
	$('.artistReception').each(function() {
		if ($(this).text() == '12:00 - 12:00 AM') {
			$(this).parent('.receptionParent').hide();
		}
	});
	
	
	// Remove reception is start/end is the same
	$('span.artworkMedium').each(function() {
		$(this).text($(this).text().replace(/12:00 - 12:00 AM/gi, ""));
	});
	
	
	// Special fix
	$('span.artworkId').each(function() {
		$(this).text($(this).text().replace(/11 - 11/gi, "11"));
	});
	
	
	//
	// ----- #### SIMPLE EDIT #### -----
	//
	
	// Re-sort select list of artists
	var image_menu_arr = $('#artist_menu_image option');
	var opts = jQuery.makeArray(image_menu_arr).
		sort(function(a,b) {
			return (a.innerHTML > b.innerHTML) ? 1 : -1;
		});
		$('#artist_menu_image').html(opts);
	  
	  
	// Replace current user text
	$('#logged_in_user').text($('#current_user').val());
	
	// Set size of curret preview image
	$('#current_image').each(function() {
	
		var height = $(this).attr('height');
		var width = $(this).attr('width');
		
		if (width > 200 || height > 200) {
			$(this).attr('height', parseInt(height*.3));
			$(this).attr('width', parseInt(width*.3));
		}
	
	});	
	
	// ----- Don't Allw Double Clicking -----
	$('#sq_commit_button').click(function() {
		$(this).attr('disabled','disabled');								  
	});
	
	
	// ----- Remove Image Editor Button -----
	$('#image_0_use_editor_button').remove();
	
	
	// ----- Set the current date for metadata date fields -----
	var current_full_date = new Date();
	var current_year = current_full_date.getFullYear();
	var current_day = current_full_date.getDate();
	var current_month = current_full_date.getMonth();
	var current_hour = current_full_date.getHours();
	var current_min = current_full_date.getMinutes();
	
	// Check to see if we are in simple edit mode
	if ($('#simple_edit_check').val() == 'FALSE') {
	
		// Set to the current day
		$("select[id*='[d]']").children('option').each(function() { 
			if ($(this).val() == current_day) { 
				$(this).attr('selected', true); 
			} 
		});
		// Set to the current month
		$("select[id*='[m]']").children('option').each(function() { 
			if ($(this).val() == current_month + 1) { 
				$(this).attr('selected', true);
			} 
		});
		// Set to the current hour
		$("select[id*='[h]']").children('option').each(function() { 
			if ($(this).val() == current_hour) { 
				$(this).attr('selected', true);
			} 
		});
		// Set to the current minute
		$("select[id*='[i]']").children('option').each(function() { 
			if ($(this).val() == current_min) { 
				$(this).attr('selected', true);
			} 
		});
		// Set to the current year
		$("input[id*='[y]']").val(current_year);
		
	}//end if
	
	
	// ----- Editing Mode: Set current artist dropdown -----
	var artist_name = $("input[id*='folder'][type='text']").val();
	$('#artist_edit').children('option').each(function() { 
        if ($(this).text() == artist_name) { 
			$(this).attr('selected', true);
        } 
	});
	
	
	// ----- Editing Mode: Dynamically load artist artwork listing -----
	$('#artwork_edit_front').change(function() {
		var current = $(this).children('option:selected').val();
		$.ajax({
		  url: current,
		  cache: false,
		  beforeSend: function() {
			  $('#artworkEditLoad').show();
		  },
		  success: function(html){
			$('#artwork_edit_artist').html(html);
			$('#artworkEditLoad').hide();
		  }
		});		
	});//end change
	
	
	// ----- Delete Mode: Dynamically load artist artwork listing -----
	$('#artwork_delete_front').change(function() {
		var current = $(this).children('option:selected').val();
		$.ajax({
		  url: current,
		  cache: false,
		  beforeSend: function() {
			  $('#artworkEditLoad').show();
		  },
		  success: function(html){
			$('#artwork_edit_artist').html(html);
			$('#artworkEditLoad').hide();
		  }
		});		
	});//end change
	
	
	// ----- Clear Cache -----
	$('#clear_cache').change(function() {
		var current = $(this).children('option:selected').val();
		$.ajax({
		  url: current,
		  cache: false,
		  beforeSend: function() {
			  $('#artworkEditLoad').show();
		  },
		  success: function(html){
			$('#clear_cache_target').html(html);
			$('#artworkEditLoad').hide();
		  }
		});		
	});//end change
	
	$('.matrixCache').live('click', function(){
		var current = $(this).attr('href');
		var obj = $(this);
		$.ajax({
		  url: current,
		  type: 'GET',
		  cache: false,
		  beforeSend: function() {
			  obj.parent().append('<img src="/__data/assets/image/0018/2277/ajax-loader.gif" class="cache_load" />');
		  },
		  success: function(html){
			  obj.parent().children('.cache_load').remove();
		  }
		});		
		
		return false;
		
	});// End live click
	
	
	$('.add_aquisition').live('click', function(){
		var current = $(this).attr('href');
		var obj = $(this);
		$.ajax({
			url: current + '&action=add_acquisition',
			type: 'POST',
			cache: false,
			beforeSend: function() {
				obj.parent().append('<img src="/__data/assets/image/0018/2277/ajax-loader.gif" class="aquisition_load" />');
			},
			success: function(html){
				obj.parent().children('.aquisition_load').remove();
			}
		});		
		
		return false;
		
	});// End live click


	// ----- New Acquisitions: Dynamically load artist artwork listing -----
	$('#artwork_acquisition').change(function() {
		var current = $(this).children('option:selected').val();
		$.ajax({
		  url: current,
		  cache: false,
		  beforeSend: function() {
			  $('#artworkEditLoad').show();
		  },
		  success: function(html){
			$('#artwork_edit_artist').html(html);
			$('#artworkEditLoad').hide();
		  }
		});// End Ajax
		
		// Add click to links
		$('.matrixAdd').click(function () {
			
			// Grab our attributes
			var item_name = $(this).attr('rel');
			var item_href = $(this).attr('href');
			
			// Confirm they want to add
			var add_item = confirm('Are you sure you want to add " + item_name + " to New Acquisitions?');
			if (add_item) {
				$.ajax({
					url: item_href,
					success: function(html){
						$(this).parent().append('Added!');
					}
				});// End Ajax

			}// End if
			
			return false;
			
		});
		
	});//end change
	
	
	//
	// ----- #### ARTISTS #### -----
	//
	
	// ----- Artists Menu: Load Up Artists Menu Dynamically
	if (simple_edit === 1) {
		$('a#Artists').hover(function() {
			menu_disabled();
		});
	} else {
		var artist_menu_url = $('#artistsMenuLink').attr('href');
		$.ajax({
		  url: artist_menu_url,
		  cache: true,
		  success: function(html){
				$('#artistsBoxHold').html(html);
				
				load_artist_menu();
			
			}// End Success
		  
		});// End ajax	
		
	}// End else
	
	// Load artists menu thumbnail view
	$('#artistsBoxHeader a').live('click', function() {
		var obj = $(this);
		if (obj.hasClass('selected')) {
			return false;
		} else {
			$('#artistsBoxHeader a').removeClass('selected');
			obj.addClass('selected');
		}
		
		$.ajax({
			url: obj.attr('href'),
			cache: true,
			beforeSend: function () {
				$('<div id="artist_menu_over"><img src="/__data/assets/image/0018/2277/ajax-loader.gif" alt="Load" /></div>').appendTo('#artistsBoxLoad');
			},
			success: function(html){
				// Add our content
				$('#artistsBoxLoad').html(html);
				if (obj.text() == 'Artist List') {
					load_artist_menu();
				}

				// Hide our loader
				$('#artist_menu_over').remove();
				
				// ----- Exhibition exhibitionsBoxInner Bars -----
				$ebi = $('#artistsThumbs');
				
				//$eb.show();
				$ebi.jScrollPane({
					showArrows:false,
					scrollbarWidth: 6
				});
				
			}
		
		});//end ajax
		
		return false;
	});
	
	// Change on mouse over
	$('#artistsThumbs a').live('mouseover', function(){
		// Set artist name
		$('#artistsBoxHeader span').text($(this).attr('title'));
	});
	
	
	// ----- Artists Menu: Prevent the user from visiting the real page
	$('a#Artists, a#Exhibitions').click(function() {
		return false;			  
	});//end click
	
	
	// ----- Artists: Check to see if artwork is sold -----
	$('.artworkSold').each(function() {
		if ($(this).attr('alt') == 'Yes') {
			$(this).show();	
		}
	});
	
	
	// ----- Artists Artwork: Preload Slideshow
	$('#artistCycleParent img').preload();
	
	
	// ----- Artists: Don't let our browser have nothing to click -----
	if ($('#artworkNext').attr('href') === '') {
		$('#artworkNext').attr('href', '#');
	} else if ($('#artworkPrev').attr('href') === '') {
		$('#artworkPrev').attr('href', '#');
	}
	
	
	// ----- Artists Artwork: Inquiry Form -----
	$('.inquiry_link').click(function(){
		var inquiry_id = $(this).attr('rel');
		var current_artist = $('.artistInfoHead h3:first').text();
		
		// Set the inquiry value
		$('#q3713_q3').val(current_artist + ' - ' + inquiry_id);
		$('#artwork_id').text(current_artist + ' - ' + inquiry_id);
		
		// Pause our slideshow
		$('#artistCycleParent').cycle('pause');
		$('#pauseButton').hide();
		$('#resumeButton').show();
		
		// Show our hidden Form
		$('#nested_artwork_inquiry').fadeIn('fast');
		// Show our invisible div
		var blanket = $('#site_blanket');
		blanket.show();
		blanket.click(function(){
			$('#nested_artwork_inquiry').fadeOut('fast');			   
			$(this).hide();	
			
			// Resume slideshow
			$('#artistCycleParent').cycle('resume');
			$('#pauseButton').show();
			$('#resumeButton').hide();
			
		});
		
		return false;
		
	});//end click
	
	
	//
	// ----- #### Exhibitions #### -----
	//
	
	// ----- Exhibitions Menu: Load Up Exhibitions Menu Dynamically
	if (simple_edit === 1) {
		$('a#Exhibitions').hover(function() {
			menu_disabled();
		});
	} else {
		var exhibit_menu_url = $('#exhibitMenuLink').attr('href');
		$.ajax({
		  url: exhibit_menu_url,
		  cache: true,
		  success: function(html){
			$('#exhibitionsBoxHold').html(html);
			// Fix some special instances
			$('span.artworkMedium').each(function() {
				$(this).text($(this).text().replace(/Dec 11 - Dec 11/gi, "Dec 11"));
			});
			// ----- Show Exhibitions Menu When Link moused over ------
			var onMenu = false;
			var onImg = false;
			$('a#Exhibitions').hover(
				 function() {
				// Show Our Artists Box
				var onImg = true;
				$('#exhibitionsBox').fadeIn('fast');
				 },
				 function() {
				// Show Our Artists Box
				var onImg = false;
					setTimeout(function() {
						if (!onMenu) $('#exhibitionsBox').fadeOut('fast');
					},
					50);
				});
				$('#exhibitionsBox').hover(
				function() {
					onMenu = true;
				},
				function() {
					onMenu = false;
					setTimeout(function() {
						if (!onImg) $('#exhibitionsBox').fadeOut('fast');
					},
					50);
				});
				
			// ----- Remove Receptions -----
			// Check to see if we should remove the receptions
			$('.time_check').each(function () {
				if ($(this).text() === '12:00 - 12:00 AM') {
					$(this).hide();
				}
			});
			
			// -- Add our schollers
			$eb = $('#exhibitionsBoxInner').parent().parent();
			$eb.show();
			$('#exhibitionsBoxInner').jScrollPane({
				showArrows:false,
				scrollbarWidth: 6
			});
			$eb.hide();
				
		  }// End Success
		  
		});// End ajax
		
		// Load up our other menu options
		$('#exhibit_future, #exhibit_archive').live('click', function() {
			// Current object
			var obj = $(this);
			// Set our var for already visited
			var visited = true;
			
			var future_url = $(this).attr('href');
			$('#exhibition_options a').removeClass('current');
			$(this).addClass('current');
			
			if (!$(this).hasClass('visited')) {
				$(this).addClass('visited');
				$.ajax({
				  url: future_url,
				  cache: true,
				  beforeSend: function () {
					$('<img id="load_img" src="/__data/assets/image/0018/2277/ajax-loader.gif" alt="Load" /><div id="menu_over"></div>').appendTo('#exhibitionsBox');
				  },
				  success: function(html){
					  // Remove our loading stuff
					  $('#menu_over, #load_img').remove();
					  
					// Hide the current content
					$('#exhibitionsBoxInner').parent().hide();
					// Show our new stuff
					if (obj.attr('id') === 'exhibit_archive') {
					  $('#exhibit_holder_arc').html(html).show();
					  $('#exhibit_holder').parent('.jScrollPaneContainer').hide();
					  
						// ----- Remove Receptions -----
						// Check to see if we should remove the receptions
						$('.time_check').each(function () {
							if ($(this).text() === '12:00 - 12:00 AM') {
								$(this).hide();
							}
						});
						
						// ----- Exhibition Scroll Bars -----
						$ebi = $('#exhibit_holder_arc');
						$ebi.jScrollPane({
							showArrows:false,
							scrollbarWidth: 6
						});
						
					} else {
					  $('#exhibit_holder').html(html).show();
					  $('#exhibit_holder_arc').parent('.jScrollPaneContainer').hide();
					  
						// ----- Remove Receptions -----
						// Check to see if we should remove the receptions
						$('.time_check').each(function () {
							if ($(this).text() === '12:00 - 12:00 AM') {
								$(this).hide();
							}
						});
						
						// ----- Exhibition Scroll Bars -----
						$ebi = $('#exhibit_holder');
						$ebi.jScrollPane({
							showArrows:false,
							scrollbarWidth: 6
						});
					}
					  
				  }// End success
				  
			   });// End ajax
			
			} else {
			  // Show our new stuff
			  if (obj.attr('id') === 'exhibit_archive') {
				  // Hide the current content
				  $('#exhibitionsBoxInner').parent('.jScrollPaneContainer').hide();
				  $('#exhibit_holder').parent('.jScrollPaneContainer').hide();
				  $('#exhibit_holder_arc').parent('.jScrollPaneContainer').show();
			  } else {
				  // Hide the current content
				  $('#exhibitionsBoxInner').parent('.jScrollPaneContainer').hide();
				  $('#exhibit_holder_arc').parent('.jScrollPaneContainer').hide();
				  $('#exhibit_holder').parent('.jScrollPaneContainer').show();
			  }
			  
			}
			
			return false;
											
		});// End click
		
		// Show our current exhibitions
		$('#exhibit_current').live('click', function() {
			$('#exhibition_options a').removeClass('current');
			$(this).addClass('current');
			$('#exhibit_holder_arc').parent('.jScrollPaneContainer').hide();	
			$('#exhibit_holder').parent('.jScrollPaneContainer').hide();
			$('#exhibitionsBoxInner').parent('.jScrollPaneContainer').show();
			
			return false;
			
		});// End click
		
	}// End else
	
	//
	// ----- #### About #### -----
	//
	
	// Add our scrollpane
	$('#body_text_about, #fair_scroll').jScrollPane({
		showArrows:false,
		scrollbarWidth: 6
	});
	
});//end ready function

function menu_disabled() {
	
	if ($('#disabled').length === 1) {
		// Do nothing
	} else {
		$('<div id="disabled">Menu is disabled in Editing Mode</div>').prependTo('#headerHolderSub');
		
		setTimeout(function() {
			$('#disabled').remove();
				},
		5000);
	}
	
}// End menu_disabled

function load_artist_menu() {
	
	// ----- Artists Menu: Create Columns -----
	var total_artists = $('#artistsColumns p').length;
	var total_artists_mod = total_artists % 3;
	var total_artists_col = total_artists / 3;
	$('#artistsColumns p').slice(0 , total_artists_col).addClass('first');
	$('#artistsColumns p').slice(total_artists_col , total_artists_col * 2).addClass('second');
	$('#artistsColumns p').slice(total_artists_col * 2 , (total_artists_col * 3) + total_artists_mod).addClass('third');
	
	// Now lets wrap those in some divs
	$('p.first').wrapAll('<div class="colFirst"></div>');
	$('p.second').wrapAll('<div class="colSecond"></div>');
	$('p.third').wrapAll('<div class="colThird"></div>');
	
	// ----- Show Artists Menu When Link moused over ------
	var onMenu = false;
	var onImg = false;
	$('a#Artists').hover(
		 function() {
		// Show Our Artists Box
		var onImg = true;
		$('#artistsBox').fadeIn('fast');
		 },
		 function() {
		// Show Our Artists Box
		var onImg = false;
			setTimeout(function() {
				if (!onMenu) $('#artistsBox').fadeOut('fast');
			},
			50);
		});
		$('#artistsBox, #artistsThumbs a, #artistsThumbs img').hover(function() {
			onMenu = true;
		},
		function() {
			onMenu = false;
			setTimeout(function() {
				if (!onImg) $('#artistsBox').fadeOut('fast');
			},
			50);
	});// End Hover
		
	// ----- Artists Menu: Show Artist Preview Image -----
	var $links = []; 
	$.each($('#artistsColumns a'), function() { 
		$links.push($(this).attr('rel')); 
	}); 
	var $preview = $('#artist_menu_artwork');
	
	$.preload($links, 
		{ 
			threshold: 2
		} 
	);
	
	// Set first preview image and text
	$('#artist_menu_artwork').attr('src', $('#artistsColumns a:first').attr('rel'));
	$('#artistsBoxHeader span').text($('#artistsColumns a:first').text());
	
	// Change on mouse over
	$('#artistsColumns a').mouseover(function(){
		$preview.attr('src', $(this).attr('rel'));
		// Set artist name
		$('#artistsBoxHeader span').text($(this).text());
		
	});
	
}//end load_artist_menu