$( document ).ready( function(){
	$('#navigation li').hover(
		function(){
			var sub = $(this).find('.sub');
			if( sub.length == 0 ) return;
			
			sub.show();
			$(this).addClass('hover');
		},
		function(){
			var sub = $(this).find('.sub');
			sub.hide();
			$(this).removeClass('hover');
		}
	);
	
	var h = $( 'body' ).height() > $( window ).height() ? $( 'body' ).height() : $( window ).height();
	$('#black-screen').css({ 'height': h });
	
	
	
	$('.add-site-link').click(function(){
		var popup_path = PATH + '/ajax/add-site-popup.php';
		
		$('#popup-holder').load( popup_path, function(){
			
			open_window( '#add-site-popup', function(){ $('#name-field').focus(); } );
			
			
			
			$('#submit-site').click(function(){
				var has_error = false;
				
				$('#add-site-popup .required').each(function(){
					var t = $(this);
					var v = t.attr('value');
					
					if( t.hasClass('valid-mail') ) {
						if( !v.is_valid_email() ) {
							alert('Please, enter valid email address.');
							t.focus();
							has_error = true;
							return false;
						}
					}
					if( t.hasClass('valid-verification') ) {
						if( v.toLowerCase() != 'cssmayo' ) {
							alert('Please, enter CSSMAYO in the field below.');
							t.focus();
							has_error = true;
							return false;
						}
					}
					if( v.is_empty() ) {
						alert('Please, enter required information.');
						t.focus();
						has_error = true;
						return false;
					}
					
					has_error = false;
					
				});
				
				if( has_error ) return false;
				
				
				var data = {};
				$('#add-site-popup .field').each(function(){
					data[$(this).attr('id').replace('-field', '')] = $(this).attr('value');
				});
				
				var url = PATH + '/ajax/add-site.php';
				
				$('#add-site-popup .form').fadeOut('dev', function(){ $('#add-site-popup .thanks').fadeIn(); });
				$.post( url, data );
				
				return false;
			});
			
			
			$( '#add-site-popup .close-popup' ).click(function(){
				close_window( '#add-site-popup' );
				return false;
			});
		});
		
		$( '#black-screen' ).click(function(){
			close_window( '#add-site-popup' );
			return false;
		});
		
		return false;
	});
	
	
	
	
	
	/** PRELOAD **/
	var cache = ['bul.gif', 'cats.gif', 'add-site-h.gif', 'submit-site.gif', 'li-hover.gif'];
	
	for(var i = 0; i < cache.length; i++) {
		var image = new Image();
		image.src = PATH + '/images/' + cache[i];
	}
	
	
	
	/** Lightbox **/
	
	$('.add-to-lightbox').click(function(){
		add_to_lightbox( $(this) );
		return false;
	});
	
	
	$('#top').click(function(){
		
		if( !top_enabled ) return false;
		
		window.setTimeout(function(){
			top_enabled = true;
		}, 1000)
		
		if ( $('.open-lightbox').hasClass('close-lightbox') ) {
			close_lightbox();
			return false;	
		} 
		var cb = new Date().getTime() / 1000;
		
		$('#top-loading').fadeIn('fast');
		$('#top-holder').addClass('top-holder-absolute');
		
		$('#lightbox').load( LIGHTBOX_PAGE + '?cb=' + cb  + ' #lightbox-entry' , function(){
			open_window('#lightbox', function(){
				window.setTimeout(function(){
					$('#lightbox').animate({'height': 350}, function(){
						/** Carousel **/
						$('#top-holder-place #lightbox-carousel ul').jcarousel({ scroll:1 });
						$('#top-loading').fadeOut();
						$('.open-lightbox').addClass('close-lightbox');
						
						$( '#black-screen' ).click(function(){
							//alert(top_enabled);
							if( !top_enabled ) return false;
							close_lightbox();
							return false;
						});
					});
				}, 500);
			});
			
			$('.remove-from-lightbox').click(function(){
				remove_from_lightbox( $(this) );
				return false;
			});
			top_enabled = true;
		});
		
		top_enabled = false;
		return false;
	});
	
	
	$('.remove-from-lightbox').click(function(){
		remove_from_lightbox( $(this) );
		return false;
	});
	
	$('.fix-last > *:last-child').addClass('last');

});

var _carousel = null;
var top_enabled = true;

function remove_from_lightbox( link ) {
	var link = $(link);
	var post_id = link.attr('title').replace('Remove Post ID ', '');
				
	remove_lightbox_cookie( post_id );
	
	var new_num = parseInt($('#num_sites').html() - 1 );
	$('#num_sites').html( new_num );
	
	link.parent().parent().fadeOut(function(){
		var li = $(this).parent();
		var ul = li.parent();
		var index = li.attr('jcarouselindex');
		
		var max_index = ul.find('li').length;
		
		for ( var i=index; i<=max_index; i++ ) {
			var li = ul.find('li:eq(' + (i-1) + ')');
			var item = ul.find('li:eq(' + i + ') .lightbox-entry:eq(0)');
			li.append( item.remove() );
			
			item.find('.remove-from-lightbox').click(function(){
				remove_from_lightbox( $(this) );
				return false;
			});
		}
		$(this).remove();
		
		
		$('#lightbox-carousel li').each(function(){
			if($(this).find('.lightbox-entry').length == 0)
				$(this).remove();
		})
		/** Carousel **/
		//_carousel.reset(); //$('#lightbox-carousel ul').jcarousel({ scroll:1 });
	});
	
	$('#post-' + post_id + ' .in-your-lightbox')
		.addClass('add-to-lightbox').removeClass('in-your-lightbox')
		.click(function(){
			add_to_lightbox( $(this) );
			return false;
		});
		
		
	
}



function add_to_lightbox( link ) {
	link = $(link);
	if( link.hasClass('in-your-lightbox') ) {
		return false;
	}
	
	var post_id = link.parent().parent().parent().parent().attr('id').replace('post-', '');
	
	set_lightbox_cookie(post_id);
	link.animate({ 'opacity': 0 }, function(){
		link.addClass('in-your-lightbox').removeClass('add-to-lightbox').animate({ 'opacity': 1});
	});
	
	$('#num_sites').html( parseInt($('#num_sites').html()) + 1 );
}

function close_lightbox(){
	$('#lightbox').animate({'height': 0}, function(){
		$('#lightbox').html('').hide();
		window.setTimeout(function(){
			close_window( '#lightbox', function(){ 
				$('#top-holder').removeClass('top-holder-absolute');
				$('.open-lightbox').removeClass('close-lightbox');
			});	
		}, 500)
	}); 
}


function open_window( win, callback ) {
	var callback = callback || function(){};
	
	$( '#black-screen' ).show().css({ 'opacity': 0 });
	$( '#black-screen' ).fadeTo( 'dev', 0.8, function(){
		$( win ).show();
		callback();
	});
}
function close_window( win, callback ) {
	var callback = callback || function(){};
	$( win ).hide();
	$( '#black-screen' ).fadeTo( 'dev', 0, function(){
		$( this ).hide();
		callback();
	});
	return false;
}


String.prototype.is_valid_email = function() { return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(this)); }
String.prototype.is_empty = function() { return this == ''; }




function set_cookie( cookie_name, value, expire_days ) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expire_days);
	document.cookie = cookie_name+ "=" + escape(value) + ((expire_days==null) ? "" : ";path=/;expires="+exdate.toGMTString());
}

function get_cookie( cookie_name ) {
	
	if ( document.cookie.length > 0 ) {
		c_start=document.cookie.indexOf(cookie_name + "=");
		
		if ( c_start != -1 ) {
			c_start = c_start + cookie_name.length+1;
			c_end = document.cookie.indexOf(";", c_start);
			
			if ( c_end==-1 ) 
				c_end=document.cookie.length;
			
			return unescape( document.cookie.substring( c_start, c_end ) );
		}
  	}
  	
	return "";
}

function set_lightbox_cookie( post_id ) {
	var old_value = get_cookie( COOKIE_NAME );
	if( old_value.indexOf(post_id) != -1)
		return;
	
	
	var new_value= old_value == "" ? post_id : old_value + '|' + post_id;
	
	//alert('OLD: ' + old_value);
	//alert('NEW: ' + new_value);
	
	
	set_cookie( COOKIE_NAME, new_value, 365 );
	
}

function remove_lightbox_cookie( post_id ) {
	var old_value = get_cookie( COOKIE_NAME );
	var new_value = old_value.replace(post_id, '');
	
	set_cookie( COOKIE_NAME, new_value, 365 );
	
	
}