window.onload = function()
{
	/**
	 * ZEBRA TABLE
	 */
	 $( '.zebra' ).zebra();
	
	$( 'a' ).click( function() { if( $( this ).attr('href') == '#' ) return false; }  );
	
	$( 'a.cart_delete' ).click( function() { if( confirm( 'Are you sure ?' ) ){ return true; } else { return false; } } );
	$( '#vip_code' ).focus( function() { if( $( this ).val() == '*******' ) { $( this ).val(''); } } );
	$( '#vip_code' ).blur( function() { if( $( this ).val() == '' || $( this ).val() == ' '  ) { $( this ).val('*******'); } } );
	
	$( '#captcha' ).click( function() {  this.src= '/captcha.php?id=' + Math.random(); } );
	$( 'div.div_preview_photos a' ).click( function() { $( '#current_photo' ).src( '/pt2.php?file=' + $( this ).attr( 'rel' ) ); return false; } );
	
	var i = 0;
	var max = parseInt( $( '#max' ).val() );
	var addedimages = getNoPhotos();
	
	$( 'img.selected_image' ).click( function() { 
		if( confirm( 'Are you sure you want to remove this picture?' ) )
		{
			$( this ).Shrink( 500, function() { removeImage( $( this ).attr( 'alt' ) ); $( this ).remove(); addedimages--; } );
		}
	} );
	
	loadPage( '/ajaxgallery.html' );
	
	function loadPage( url )
	{
		$( '#gallery_loading' ).show();
		$( '#gallery' ).load( url, clickToLoad );
	}
	
	function clickToLoad()
	{
		$( 'table#galleries tr td table tr td a' ).click( function( e )
		{
			loadPage( this.getAttribute( 'rel' )  );
			if( ! e )
			{
				e = window.event;
			}
			
			e.cancelBubble = true;
			if( e.stopPropagation )
			{
				e.stopPropagation();
			}
			
			return false;
		});
		
		$( '#galleryList' ).click( function( e ) {
			loadPage( '/ajaxgallery.html' );
			if( ! e )
			{
				e = window.event;
			}
			
			e.cancelBubble = true;
			if( e.stopPropagation )
			{
				e.stopPropagation();
			}
			
			return false;
		} );
		
		$( 'td.ajaxpagination a' ).click( function( e )
		{
			loadPage( this.getAttribute( 'rel' )  );
			if( ! e )
			{
				e = window.event;
			}
			
			e.cancelBubble = true;
			if( e.stopPropagation )
			{
				e.stopPropagation();
			}
			
			return false;
		});
		
		$( 'a.select' ).click( function( e )
		{
			if( addedimages >= max )
			{
				alert( 'You already selected ' + max + ' photos' );
				if( ! e )
				{
					e = window.event;
				}
				
				e.cancelBubble = true;
				if( e.stopPropagation )
				{
					e.stopPropagation();
				}
				
				return false;
			}
			
			addedimages++;
			i++;
			
			var id  = this.getAttribute( 'rel' );
			var img = $( '#' + id ).clone();
			
			var newid = id + '' + i;
			
			img.attr( 'id', 'image_' + newid );
			img.attr( 'class', 'selected_image' );
			img.attr( 'title', 'Click to remove it' );
			
			$( '#selected_photos' ).append( img );
			
			$( '#' + id ).TransferTo( { to: 'image_' + newid, className: 'transferer', duration: 400 } );
			
			addImage( id );
			
			$( '#' + 'image_' + newid ).click( function()
			{
				if( confirm( 'Are you sure you want to remove this picture?' ) )
				{
					$( this ).Shrink( 500, function() { removeImage( id ); $( this ).remove(); addedimages--; } );
				}
			});
			
			if( $( this ).attr('href') == '#' ) return false;
			
		});
		$( 'a.thickbox' ).click( function(){
			
			genericPopup(  $( this ).attr('href') );
			return false;
			
		});
		$( '#gallery_loading' ).hide();
	}	
	
	$( 'a.thickbox' ).click( function(){
		
		genericPopup(  $( this ).attr('href') );
		return false;
		
	});
	
	$( '#empty' ).click( function()
	{
		$( '#images' ).val( '' );
		$( '#selected_photos' ).html( '' );
	});
}

/**
 *	Add image
 */
function addImage( id )
{
	var tmp = $( '#images' ).val();
	
	tmp = tmp ? tmp + ',' + id : id;
	
	$( '#images' ).val( tmp );

	getNoPhotos();
}

/**
 *	Remove image
 */		
function removeImage( id )
{
	var images = $( '#images' ).val();
	var tmp    = images.split( ',' );
	
	var r = new Array();
	
	var j = 0;
	var removed = false;
	for( i in tmp )
	{
		if( tmp[i] == id &&
		    ! removed )
		{
			removed = true;
			continue;
		}
		
		r[j] = tmp[i];
		j++;
	}
	
	$( '#images' ).val( implode( ',', r ) );
	getNoPhotos();
}

/**
 * 	Implode function
 */
function implode( separator, array )
{
	var str = '';
	var l = array.length;
	
	for( var i in array )
	{
		str += array[i];
		if( i < l - 1 )
		{
			str += separator;
		}
	}
	
	return str;
}

function getNoPhotos()
{
	if( $( '#images' ).val() == '' )
	{
		$( '#selected_photos' ).append( "<p>No photos selected</p>" );
	}
	else
	{
		$( '#selected_photos p' ).remove();
	}
	return $('#added').val();
}

function genericPopup( url )
{
	var ref = window.open('','','location=no,status=no,resizable=1,width=50,height=50');
	ref.document.open();
	ref.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>View Photo</title><s'+'cript t'+'ype="text/javas' + 'cript">');
	ref.document.write('var NS = (navigator.appName=="Netscape")?true:false;function fitPic() {	iWidth = (NS)?window.innerWidth:document.body.clientWidth;iHeight = (NS)?window.innerHeight:document.body.clientHeight;iWidth = document.images[0].width;iHeight = document.images[0].height-10;window.resizeBy(iWidth, iHeight);self.focus();};');
	ref.document.write('window.onload=function(){ fitPic(); }');
	ref.document.write('</s' + 'cript></head><body style="padding: 0; margin:5px; "><img align="middle" src="'+url+'" alt="0" border="0" /><br /><p style="text-align: center;"><a href="#" style="font-size: 12px; color: #000;" onclick="window.close();">Close window</a></p></body></html>');
	ref.document.close();
	return ref;
}

function submitPayment()
{
	document.getElementById('payment_form').submit();
}

function redirectToHome()
{
	document.location='/';
}

