// <div class="jqzoom">
// 	<img class="threeSixty" width="360" height="270" />
// </div> <!-- small image -->

var gotoDetailsState;

(function($) {
	
	// setup inquire about this item:
	/*
	var inquiryForm = new VarienForm ( 'inquiry_form' );
  inquiryForm.submit = function () {
      if ( this.validator.validate () ) {
          this.form.submit ();
      }
  }.bind(inquiryForm);
	*/
	
	$( "div#inquire a" ).click ( function ( e ) {		
		$( "div#friend" ).slideUp ( "slow" );
		$( "div#inquiry" ).slideDown ( "slow" );		
	} );
	
	
	// setup inquire form:
	
	var SERVER_LOC = "/inquire/";
	var ERROR_MESSAGE = "Please enter a valid email address";
	var THANK_YOU_MESSAGE = "Thank you for inquiring about this item.";
	var SERVER_ERROR_MESSAGE = "There was an error sending your inquiry.  Please try again, or contact support@medusa-art.com";
	
	function validEmail ( email ) {
	  var emailReg = "^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$";
	  var regex = new RegExp ( emailReg );
	  return regex.test ( email );
	}
	
	$( "form#inquiry-form" ).submit ( function ( e ) {		
		e.preventDefault ();		
		
		var title = $("#product-name").text();
		var link = window.location.href;
		
		var email = $( "input#email" ).val ();
		var message = $( "textarea#message" ).val ();
		//var product_id = $( "input#product_id" ).val ();
		if ( validEmail ( email ) ) {
			
			$( "form#inquiry-form div#advice-required-entry-sender_email" ).hide ();
			$( "input#email" ).removeClass ( "validation-failed" );

			$.post ( SERVER_LOC, { email: email, title: title, link: link, message: message },
			  function ( data ) {
			    if ( data ) {
						alert ( THANK_YOU_MESSAGE );
						$( "div#inquiry" ).slideUp ( "slow" );
					} else {
						alert ( SERVER_ERROR_MESSAGE );
					}					
			} );
			
		} else {
			// alert ( ERROR_MESSAGE );
			$( "input#email" ).addClass ( "validation-failed" );
			$( "form#inquiry-form div#advice-required-entry-sender_email" ).show ();
		}
		
	} );
	
	
	
	if ( $( "div#friend ul.messages" ).length > 0 ) {
		//$( "div#friend" ).slideDown ( "slow" );
		alert ( $( "div#friend ul.messages li ul li" ).html () );
	}
	
	
	
	$( "div#send-to-friend a" ).click ( function ( e ) {		
		$( "div#inquiry" ).slideUp ( "slow" );
		$( "div#friend" ).slideDown ( "slow" );
	} );
		
		
		
	$( "a#close-friend" ).click ( function ( e ) {		
		$( "div#friend" ).slideUp ( "slow" );
	} );
		
	
	
	$( "a#close-inquiry" ).click ( function ( e ) {		
		$( "div#inquiry" ).slideUp ( "slow" );
	} );
	
		
		
	// setup thumbs scroller:
	$( "div#thumbs-scroller li a, div#thumbs-scroller-zoom li a" ).each ( function () { 
		
		if ( imgList[ "i_" + ($(this).attr('href')).replace(__SERVER_ROOT,"/") ].isFullscreenable == "true" ) {
			$(this).addClass( 'has_fullscreen' );
			$(this).append("<div class='png' />");
		} else {
			$(this).addClass( 'no_fullscreen' );
		}
				
	} );
	
	var zoom = $( "div#thumbs-scroller" ).clone().attr('id',"thumbs-scroller-zoom").hide();
	
	zoom.appendTo('#views');

	$("#thumbs-scroller li a, div#thumbs-scroller-zoom li a").click(scrollerClick);
	
	function loadNormalScroller () {
		var normLength = $( "div#thumbs-scroller" ).children('ul').children('li').length;

		if (normLength > 4 ) {
		
			$( "div#thumbs-scroller" ).scrollable({
					    size: 4, 
					    vertical: false, 
					    speed: 300, 
					    prev:'.prev',     
					    next:'.next', 
					    navi:'.navi', 
					    loop: true, 
					    naviItem:'li',     
					    activeClass:'active', 
					    items: '.items', 
					    onSeek: null 
					});
				
		} else {
			$( "div#thumbs-scroller" ).children('a.control').remove();
		}
	};
	loadNormalScroller();
	
	function loadZoomScroller () { 
		$( "div#thumbs-scroller-zoom" ).children('ul').children('li').children('a.no_fullscreen').parent().remove();
		var zoomLength = $( "div#thumbs-scroller-zoom" ).children('ul').children('li').length;

		if (zoomLength > 2 ) {
	
			$( "div#thumbs-scroller-zoom" ).scrollable({
			    size: 2, 
			    vertical: false, 
			    speed: 300, 
			    prev:'.prev',     
			    next:'.next', 
			    navi:'.navi', 
			    loop: true, 
			    naviItem:'li',     
			    activeClass:'active', 
			    items: '.items', 
			    onSeek: null 
			});

		} else {
			$( "div#thumbs-scroller-zoom" ).children('a.control').remove();
		}
	};
	loadZoomScroller();
	
	
	
	// scroller image swap
	// function loadZoomer () {
	// 		$( ".jqzoom" ).jqueryzoom ( {
	// 			xzoom: ( $( document ).width () ),
	// 			yzoom: ( $( document ).height () - 95 )
	// 		} ) ;
	// 	}
	
	function loadZoomBtn ( fullScreen ) {
		if (fullScreen && fullScreen.length > 0) {
			var isFullscreenable = true;
		}
		else {
			var isFullscreenable = false;
		}
		
		$( '#preview').removeClass('has_fullscreen');
		if ( !isFullscreenable ) { //if (!isFullscreenable) {
			if ($('#full-screener').length > 0) {
				$('#full-screener').remove();
			}
		} else {
			if ( $( '#full-screener' ).length == 0 ) {

				$( '#preview').addClass('has_fullscreen');
				//$( '#preview img' ).before( '<div id="full-screener" class="png"></div>' ).click ( gotoZoomState );
				// (below, so that the corner is clickable) 
				$( '#preview img' ).before( '<div id="full-screener" class="png"></div>' );
				$( '#preview' ).click ( gotoZoomState );
			}
		}
		
	}
	
	loadZoomBtn( $('.fourEighty').attr('rel') );
	
	function gotoZoomState ( e ) {
		$( "div#inquiry" ).slideUp ( "fast" );
		$( "div#friend" ).slideUp ( "fast" );
		
		$( "#preview" ).unbind ( "click" );
		$("#thumbs-scroller li a").unbind("click");
		var zoomedImage = $('div#fader img').attr('rel');
		$( 'p#product-description, .text-side dl,div.footer-container, div#inquire, div#send-to-friend' ).fadeOut ( 'slow' );
		$('div#fader').fadeOut('slow',function() {
			$('#full-screener').remove();
		});
		//$('#product-name').clone().attr('id','product-id-shadow').insertBefore('#product-name');
		
		$( 'div#info-buttons' ).fadeOut ( 'slow', function ( e ) {			
			$( "body" ).addClass ( 'zoomed' );
		} );
		
		$( 'div#views' ).fadeOut ( 'slow', function ( e ) {
			document.getElementById('product-view').style.display='none';
			document.getElementById('product-description').style.display='none';
			document.getElementById('reserve-item').style.display='none';
			
			$( "div#thumbs-scroller li" ).hide ();
			
			$( "div#thumbs-scroller" ).hide();
			
			$( "ul.breadcrumbs" ).hide ( 'fast' );
			
			$( "div#back-to-results" ).hide ( 'fast' );

			$( "div#thumbs-scroller-zoom" ).show(); // zoom view scroller
			
			$( 'div#views' ).fadeIn ();
			
			$( 'div#info-buttons' ).fadeIn ( 'slow', function ( e ) {

				$("#thumbs-scroller-zoom li a.has_fullscreen").click(scrollerClick);
				
				$( 'div#fader img' ).removeClass ( 'fourEighty' ).addClass ( 'threeSixty' ).unbind ( 'click' );
				
				$( 'div#fader').fadeIn ( 'slow', function ( e ) {	
				
					$( 'img.threeSixty' ).attr ( 'rel', zoomedImage );
					
										
					$( this ).addClass ( 'jqzoom' ).jqueryzoom( {
						xzoom: ($("body").width() ),
						yzoom: ($("body").height() - 75),
						autoload: 1
					} );
					
				} );
				
			} );
			
		} );
		
	}
	
	gotoDetailsState = function  ( e ) {
		$( '#fader' ).unload();
		setTimeout(function() {
			$('#full-details-button').unbind("click");
			$("#thumbs-scroller-zoom li a").unbind("click"); // ***
			$( 'div#fader, div#info-buttons' ).fadeOut ( 'slow' );
			//$('h2#product-id-shadow').remove();
			$( 'div#info-buttons' ).fadeOut ( 'slow', function ( e ) {			
				$( "body" ).removeClass ( 'zoomed' );
			} );
			
			$( 'div#views' ).fadeOut ( 'slow', function ( e ) {
				
				$( "div#thumbs-scroller-zoom" ).hide();
				$( "div#thumbs-scroller" ).show();
				$( "ul.breadcrumbs" ).show ( 'fast' );
				$( "div#back-to-results" ).show ( 'fast' );
				$('div#views').fadeIn();
				document.getElementById('product-view').style.display='block';
				document.getElementById('product-description').style.display='block';
				document.getElementById('reserve-item').style.display='block';
			

				$( 'ul.breadcrumbs' ).fadeIn ( 'slow', function ( e ) {
					$("#thumbs-scroller li a").click(scrollerClick);
					$( 'div#fader img' ).removeClass ( 'threeSixty' ).addClass ( 'fourEighty' );
					// loadNormalScroller();
					loadZoomBtn($('.fourEighty').attr('rel'));
					
					$( 'div#fader, p#product-description, .text-side dl, div.footer-container, div#info-buttons, div#inquire, div#send-to-friend, div#back-to-results').fadeIn ( 'slow');
				});
			});

		}, 800);

	}

	function scrollerClick(e) {

		e.preventDefault();
		
		
		var imageList = imgList["i_" + ($(this).attr('href')).replace(__SERVER_ROOT,"/")];
		var isFullscreenable = (imageList.isFullscreenable == 'true' ? true : false);
		var newImg = new Image();
		var zoomed = $('body').hasClass('zoomed') ? true : false;

		$( '#fader' ).fadeOut ( 100, function () {

			$(newImg).attr('rel','');

			if (!zoomed) {
				newImg.src = imageList['fourEighty'];
			} else {
				newImg.src = imageList['threeSixty'];
				$(newImg).attr('rel', __SERVER_ROOT+'skin/frontend/default/default/img/unavailable-in-hi-res.jpg').addClass ( 'threeSixty' );
			}

			if (isFullscreenable) {
				$(newImg).attr('rel', imageList['fullImage']);
			}
			
			$( '#fader' ).html ( newImg );
			
			if (!zoomed) {			
				loadZoomBtn($(newImg).attr('rel'));
			}
			
			$( '#fader' ).fadeIn ( 200 );
			
			if (zoomed) {
				$('#no-fullscreen').remove();
				$('#fader').unload();
				if (isFullscreenable) {
					setTimeout(function() {
						$( '#fader' ).addClass('jqzoom').jqueryzoom({ 
							xzoom: ($("body").width()),
							yzoom: ($("body").height() - 75),
							autoload: 1
						});
					}, 1000);
				} else {
					$( '#preview img' ).before( '<div id="no-fullscreen">NO fullscreen</div>' );
				}
				
			}
			
		});
		
		return false;
	}	
	
})(jQuery);
