$(document).ready(function() {
	if(!$('html').hasClass('ie6'))
    	{
			$('.small_gallery_frame img').scaleImage({center:true, scale:'fit'}); 	
    	}  
	$(window).resize(function(){
		if(!$('html').hasClass('ie6'))
    	{
			$('.small_gallery_frame img').scaleImage({center:true, scale:'fit'}); 	
    	}
	});    
	
	

	
    var count=0;


	$(window).load(function(){
		
		var ie=false;
		if($('html').hasClass('ie7'))
		{
			ie=true;
		} 
		if($('html').hasClass('ie6'))
		{
			ie=true;
		}
		if(ie==false)
		{
			
			if($(window).width()>480)
			{
				desktopGallery();	
			}  
			if($(window).width()<481)
			{
				mobileGallery();	
			} 
		}

		
			

	});
	
	function desktopGallery()
	{

		$('a.small_gallery').click(function(event){
			

			event.preventDefault();             

			ScrollPosition=$(window).scrollTop();
            
			//add an incrementing number to the end of the request to prevent caching issues in IE.
            count=count+1;  
			src=$(this).attr('rel')+'?v'+count;
			url=$(this).attr('href')+'/json';


			$.getJSON(url, function(data) {
				windowX=$(window).width();
				//windowY=$(window).height();      
				
				//windowX=window.innerHeight ? window.innerHeight : $(window).height();
				windowY=window.innerHeight ? window.innerHeight : $(window).height();
				
				
				contentWidth=windowX+300;
				

				imgContainerY=windowY-40;
				imgContainerX=windowX-20;
				

				contentHeight=windowY;


				$('body').prepend('<div id="content" style="width:'+contentWidth+'px;height:'+contentHeight+'px; top:'+ScrollPosition+'px;"><div id="info_panel" style="height:'+windowY+'px;"></div><div id="window_box" style="width:'+imgContainerX+'px; height:'+imgContainerY+'px; margin:20px 0px; "></div></div>'); 

				$('#info_panel').prepend('<h1>'+data.name+'</h1><p>'+data.short_description+'</p>'); 
				if(data.price)
				{
					$('#info_panel').append('<p>'+data.price+'</p>');
				} 
				if(data.description)
				{
					$('#info_panel').append('<p>'+data.description+'</p>');
				} 
				if(data.paypal)
				{
					$('#info_panel').append(data.paypal);
				} 
				if(data.purchase_info)
				{
					$('#info_panel').append('<p>'+data.purchase_info+'</p>');
				}
				if(data.sold)
				{
					$('#info_panel').append('<p>This item is sold.</p>');
				}

				
				
				var preloaded = $("<img/ id=\"b_img\" >").attr("src", src).load(function() 
				{

				},callback);

				 function callback()
				{

					/*
					bg_width=this.width;
					bg_height=this.height;





					Iheight=windowY-100;
					Iwidth=windowX-100; 



					ratio1=Iheight / bg_height;
					ratio2=Iwidth / bg_width;


					if(ratio1 <= ratio2)
					{
						optimumRatio=ratio1;
					} else {
						optimumRatio=ratio2;
					} 


					newHeight= bg_height * optimumRatio;
					newWidth = bg_width * optimumRatio;
					topMargin=0 - (newHeight * 0.5);

					
					 $('#window_box').prepend(this);    

					
					//$(this).css({'margin-top': topMargin, 'width':newWidth, 'height':newHeight});
					*/
					$('#window_box').prepend(this);  
					$(this).scaleImage({center:false, scale:'fit'}); 
					$(this).animate({opacity:1});
				}


				$('#window_box').prepend('<div id="gal_controls"><a class="info_button info_closed" onclick="" id="info">Info</a><a class="info_button" onclick="" id="close">Close</a></div>'); 

				show_details();
			});  
			
			
			
		 

			$('a.info_closed').live('click', function(event) {
				$('#content').animate({left:0});
				$(this).removeClass('info_closed').addClass('info_open'); 
			});
			$('a.info_open').live('click', function(event) {
				$('#content').animate({left:-300});
				$(this).removeClass('info_open').addClass('info_closed'); 
			});


			$('#close, #b_img').live('click', function(event) {
				elementHide();
			});



			$(window).scroll(function() {
			  elementHide();
			}); 
			$(window).resize(function(){
            	 elementHide();
			});

		});  

	}
	
   
	
	
	
   
	
	
	 function show_details()
		{
			$('.info_closed').live('click', function(event) {
				$('#content').animate({left:0});
				$(this).removeClass('info_closed').addClass('info_open'); 
			});
		}
		
		
		function elementHide()
		{
 
			$('#content').fadeOut(500, elementsRemove);
		} 
		
		function elementsRemove()
		{
			$('#content').remove();
		}
		


      
      function mobileGallery()
      {
      		

      		$('a.small_gallery').click(function(event){
      			event.preventDefault();
      			$('body').contents().wrap('<div class="hide" />');
      			$('.hide').slideUp();
      			


      			windowX=window.innerWidth ? window.innerWidth : $(window).width();
      			windowY=window.innerHeight ? window.innerHeight : $(window).height();

      			ScrollPosition=$(window).scrollTop();
      			
      			

      			src=$(this).attr('rel');
				url=$(this).attr('href')+'/json';

				$('body').prepend('<div id="phone_gallery" style="top:0px; width:'+(windowX - 40 )+'px;"></div>');
				$('body').prepend('<div id="phone_controls" style="top:0px;"><a href="#phone_controls" title="">BACK</a></div>');

				$(window).scrollTop(0);
				var preloaded = $("<img/>").attr("src", src).load(function(){},callback2);
				function callback2()
				{
					$('#phone_gallery').prepend('<div id="item_content" />');
					$('#item_content').prepend(this);  
					$(this).scaleImage({center:false, scale:'fit'}); 
					$('#item_content img').attr('onclick', " ");
					$('#item_content img').click(function(){
					removeViewer(ScrollPosition);	
					});

					$.getJSON(url, function(data) {
					

					if(data.name)
					{
						$('#item_content').append('<p class="phone_gal_name">'+data.name+'</p>');	
					}

					if(data.short_description)
					{
						$('#item_content').append('<p>'+data.short_description+'</p>');	
					}

					if(data.description)
					{
						$('#item_content').append('<p>'+data.description+'</p>');	
					}
					if(data.price)
					{
						$('#item_content').append('<p>'+data.price+'</p>');	
					}

					if(data.sold)
					{
						$('#item_content').append('<p class="phone_gal_sold">SOLD</p>');	
					}
					if(data.paypal)
					{
						$('#item_content').append('<p>'+data.paypal+'</p>');	
					}
					
					//$('#item_content').append('<p>'+data.price+'</p>');
					//$('#item_content').append('<p>'+data.paypal+'</p>');
					panelHeight=$('#item_content').height();
					pageHeight=$('body').height();
					$('#item_content').fadeIn();
					$('#phone_gallery').css({'background-image':'none'});
				});

				}
				removeViewer(ScrollPosition);
                
			   

      		}); 

            



      		function removeViewer(prevScroll)
      		{
      			$('#phone_controls a, #phone_gallery img').click(function(event){
      				event.preventDefault();
      				
      				$(window).trigger('touchend');
      				$('#phone_gallery').fadeOut(500, function(){
      					$(this).remove();
      				});
      				$('#phone_controls').fadeOut(500, function(){
      					$(this).remove();
      				});
      				$('.hide').show(0, function(){
      					$(this).contents().unwrap();
      					$(window).scrollTop(prevScroll);
      				});

      				

      			});
      			

      		}  

      }	



    
});
     


