$(function(){
	$.fn.safeHover = function(inFunction, outFunction, inDelay, outDelay, connection){
		
		var hovering = new Object();
		hovering.items = $(this);

		hovering.startOut;
		hovering.hasRun = false;
		hovering.startIn;
	    
        hovering.changeState = function(){
            hovering.hasRun = true;
        };
		
	    $(hovering.items).each(function(){
	    	
	    	hovering.item = $(this);
	        
	        hovering.item.bind("mouseenter", function(){

	            $(this).delay(inDelay,"safein");
	            
	            $(this).queue("safein",function(next){
	            	if (connection == 'linked') {
		                $(this).clearQueue("safeout");
		                $(this).stop();		                
		            }   
	            	hovering.changeState();
	            	inFunction.call(this);
	            	next();
	            });	        	
	            
	            $(this).dequeue("safein");

	        });		
			
			hovering.item.bind("mouseleave", function(){
				
				$(this).queue("safein",[]);
				$(this).stop();
				
                $(this).delay(outDelay,"safeout");
	            
	            $(this).queue("safeout",function(next){
	            	hovering.changeState();
	            	outFunction.call(this);
	            	next();
	            });	        	
	            
	            $(this).dequeue("safeout");

	        });
	        
	    });
	    
	};
	
	$.customAlert=function(options,callback){
		var alert = new Object();
		
		alert.effects = ($.browser.msie && parseFloat($.browser.version) < 8) ? false : true;
		
		alert.veil = $('<div></div>').fadeTo(0,0.4).css({
			position:'fixed',
			top:'0px',
			left:'0px',
			width:$(window).width()+'px',
			height:$(window).height()+'px',
			backgroundColor:'white'
		});
		
		alert.holder = $('<div></div>').css({
			width:'250px',
			position:'fixed',
			left:'50%',
			marginLeft:'-200px',
			padding:'54px 42px 50px 108px',
			color:'black',
			fontWeight:'normal',
			fontFamily:'Verdana',
			fontSize:'11px',
			top:($(window).height() / 2)-100+'px',
			display:'none'
		}).html('<div>'+options.message+'</div>');
		
		$('body').append(alert.veil).append(alert.holder);
		
		if(alert.effects){
			alert.holder.fadeTo(300,1.0,function(){
				$(this).css('filter','');
			});
		}else{
			alert.holder.show();
		}
		
		alert.holder.find('img').each(function(){
			if($(this).width() > 250){
				$(this).width(250);
			}
		});
		
		alert.src = (!options.src) ? 'img/alert.png' : options.src ;
		
		alert.bottom = $('<div></div>').css({
			position:'absolute',
			bottom:'0px',
			left:'0px',
			background:'transparent url("'+alert.src+'") bottom left no-repeat',
			width:'400px',
			height:(alert.holder.outerHeight() - 90)+'px'
		});
		
		alert.top = $('<div></div>').css({
			position:'absolute',
			top:'0px',
			left:'0px',
			background:'transparent url("'+alert.src+'") -400px 0px no-repeat',
			width:'400px',
			height:'90px'
		});
		
		alert.mark = $('<div></div>').css({
			position:'absolute',
			top:'54px',
			left:'33px',
			padding:'3px 0px 0px 0px',
			margin:'0px',
			textAlign:'center',
			fontSize:'17px',
			width:'27px',
			height:'24px',
			fontWeight:'bold'
		}).text(options.mark);
		
		alert.close = $('<div></div>').css({
			position:'absolute',
			left:'318px',
			top:'7px',
			width:'53px',
			height:'21px',
			cursor:'pointer'
		}).bind({
			click:function(){
				alert.veil.fadeTo(300,0.0,function(){
					alert.veil.remove();
				});
				if(alert.effects){
					alert.holder.fadeTo(300,0.0,function(){
						alert.holder.remove();
					});
				}else{
					alert.holder.hide();
					alert.holder.remove();
				}
				if(options.close){
					callback.call(this);
				}
			},
			mouseenter:function(){
				alert.top.css('background-position','-400px -90px');
			},
			mouseleave:function(){
				alert.top.css('background-position','-400px 0px');
			}
		});
		
		alert.holder.prepend(alert.close).prepend(alert.mark).prepend(alert.top).prepend(alert.bottom);
		
	};
	
	$.fn.squareGlow=function(options){
		var glow = new Object();
		glow.target = $(this);
		
		glow.defaults = {
			'size':4,
			'color':'#000000',
			'opacity':70
		};
		
		$.extend(glow.defaults, options);
		
		if(glow.target.css('position') !== 'absolute'){
			glow.target.css('position','relative');
		}
		
		glow.location = glow.target.position();
		
		glow.mem = new Array();
		
		for(glow.count=glow.defaults.size;glow.count>0;glow.count--){
			
			glow.step = 100/glow.defaults.size;
			glow.trend = 100-((glow.step-1)*glow.count);
			
			glow.square = $('<div></div>').css({
				position:'absolute',
				backgroundColor:glow.defaults.color,
				width:glow.target.outerWidth()+(glow.count*2)+'px',
				height:glow.target.outerHeight()+(glow.count*2)+'px',
				left:(glow.location.left - glow.count)+'px',
				top:(glow.location.top - glow.count)+'px',
				opacity:(glow.trend*(glow.defaults.opacity/(100*1.3)))/100			
			});
			
			glow.target.before(glow.square);
			
			glow.mem.push(glow.square);
		}
		
		glow.target.data('glow',glow.mem);
		return glow.target;
	};
	
	$.fn.removeGlow=function(){
		var glow = $(this).data('glow');
		if(glow){
			$(glow).each(function(){
				$(this).remove();
			});
		}
		return $(this);
	};
		
	$.fn.cleanForm=function(options){
		var clean = new Object();
		clean.called = $(this);
		clean.group = clean.called.find('[default]');
		
		if(clean.called.css('position') !== 'absolute'){
			clean.called.css('position','relative');
		}
		
		$.fn.strip=function(){
			$(this).css({
				backgroundColor:'',
				borderColor:''
			});
			return $(this);
		};
		
		clean.parLoc = clean.called.offset();
		
		clean.group.each(function(){
			clean.target = $(this);
			clean.target.val('');
			
			clean.string = clean.target.attr('default');
			clean.location = clean.target.offset();
			
			if(clean.target.is('input')){
				clean.cursor = 'text';
			}else{
				clean.cursor = 'default';
			}
			
			clean.extra = ($(this).is('.cleanRequired') && options.requiredMessage !== false) ? options.requiredMessage : '';
			
			clean.label = $('<div></div>').css({
				position:'absolute',
				top:(clean.location.top-clean.parLoc.top)+'px',
				left:(clean.location.left-clean.parLoc.left)+'px',
				height:(clean.target.outerHeight()-4)+'px',
				fontSize:'10px',
				padding:'2px 3px 2px 3px',
				cursor:clean.cursor,
				color:options.textColor,
				backgroundColor:'transparent'
			}).html(clean.string + clean.extra).data('target',clean.target);
			
			clean.target.data('target',clean.label);
			
			clean.called.append(clean.label);

			clean.target.bind('focus',function(){
				if($(this).attr('id') !== 'activeClean'){										
					if(options.inputActive){						
						clean.group.not('.cleanProblem').each(function(){
							$(this).removeGlow();
						});
						
						clean.group.not('.cleanProblem').strip();						
						
						$(this).removeGlow();
						$(this).strip().css('border','1px solid '+options.inputActive).removeClass('cleanProblem');
//						.squareGlow({
//							size:4,
//							color:options.inputActive
//						});						
					}
					
					clean.toMove = $(this).data('target');
					
					clean.old = $('#activeClean').data('target');
					
					if(clean.old){
						if($('#activeClean').val() == ''){
							clean.old.stop(true,true).css('color',options.textColor).show().fadeTo(300,1.0,function(){
								$(this).css('filter','');
							});
						}
					}
					
					clean.toMove.stop(true,true).css('color',options.textColor).fadeTo(300,0.0,function(){
						clean.toMove.hide();
					});
					
					$('#activeClean').removeAttr('id');
					$(this).attr('id','activeClean');
				}
			});
			
			clean.target.bind('blur',function(){
				$('#activeClean').removeAttr('id');
				
				clean.group.not('.cleanProblem').each(function(){
					$(this).strip().removeGlow();
				});

				clean.group.each(function(){
					if($(this).val() == ''){
						$(this).data('target').stop(true,true).show().fadeTo(300,1.0,function(){
							$(this).css('filter','');
						});
					}
				});
			});
			
			clean.label.bind('click',function(){
				if($(this).data('target').attr('id') !== 'activeClean'){
					if(options.inputActive){						
						clean.group.not('.cleanProblem').each(function(){
							$(this).removeGlow();
						});
						
						clean.group.not('.cleanProblem').strip();

						$(this).data('target').removeGlow();
						$(this).data('target').strip().css('border','1px solid '+options.inputActive).removeClass('cleanProblem');
//						.squareGlow({
//							size:4,
//							color:options.inputActive
//						});						
					}
					
					clean.toMove = $(this);
					
					clean.old = $('#activeClean').data('target');
					
					if(clean.old){
						if($('#activeClean').val() == ''){
							clean.old.show().stop(true,true).css('color',options.textColor).fadeTo(300,1.0,function(){
								$(this).css('filter','');
							});
						}
					}
					
					clean.toMove.css('color',options.textColor).fadeTo(300,0.0,function(){
						clean.toMove.hide();
					});
					
					$(this).data('target').focus();
					
					$('#activeClean').removeAttr('id');
					$(this).data('target').attr('id','activeClean');
				}
			});
			
		});
		
		clean.called.bind('submit',function(){
			clean.valid = true;
			clean.called.find('.cleanRequired').each(function(){
				if($(this).val() == ''){
					$(this).removeGlow();
					$(this).css({
						border:'1px solid #da5050',
						backgroundColor:'#fcf0f0'
					}).addClass('cleanProblem');
//					.squareGlow({
//						size:4,
//						color:'#da5050'
//					});
					$(this).data('target').css('color','#da5050');
					clean.valid = false;
				}
			});

			if(clean.valid == false){
				if(jQuery.isFunction(jQuery.customAlert)){
					$.customAlert({
						message:'Please fill out all the required fields',
						mark:'!'
					});
				}else{
					alert('Please fill out all the required fields');
				}
			}
			return clean.valid;
		});
	};
	
	$.fn.internalModal=function(setHeight){
		var internal = new Object();
		internal.target = $(this);
		internal.bg = "img/modal/internal_bg.png";
		
		internal.veil = $('<div></div>').css({
			backgroundColor:'#a8a8a8',
			position: 'fixed',
			top: '0px',
			left: '0px',
			width: $(window).width()+'px',
			height: $(window).height()+'px',
			zIndex: '500'
		}).fadeTo(0,0.4);
		
		internal.holder = $('<div></div>').css({
			left:'50%',
			marginLeft:'-412px',
			position: 'fixed',
			width: '794px',
			padding: '15px 26px 26px 15px',
			background :'transparent url("'+internal.bg+'") bottom left no-repeat',
			top: '100px',
			zIndex: '500'
		});
		
		internal.into = $('<div></div>').css({
			width: '794px',
			overflow: 'auto',
			height: (setHeight) ? setHeight+'px' : ($(window).height()*.75)+'px'
		}).addClass("_internalModal");
		
		internal.close = $('<div></div>').css({
			position: 'fixed',
			top: '73px',
			left: '50%',
			color:'#c72d2f',
			fontWeight:'bold',
			marginLeft:'350px',
			fontSize:'15px',
			cursor:'pointer',
			zIndex: '500',
			padding:'5px',
			backgroundColor:'white'
		}).text('CLOSE').bind('click',function(){
			$(internal.veil).add(internal.holder).add(internal.close).remove();
		});
		
		internal.target.clone(true).css('display','block').appendTo(internal.into);
		internal.holder.append(internal.into);
		
		$('body').append(internal.veil);
		$('body').append(internal.holder);
		$('body').append(internal.close);
	};
	
	$.fn.allImagesLoaded=function(e){
		imgTest = new Object();
		imgTest.parent = $(this);
		
		imgTest.count = imgTest.parent.find('img:not(.loadIgnore)').length;
		
		if(imgTest.count > 0){
			imgTest.pageTest = function(){
				imgTest.pageTest.pass = 0;
					
				imgTest.parent.find('img:not(.loadIgnore)').each(function(){
					if($(this).width() > 0 && $(this).height() > 0){
						imgTest.pageTest.pass++;
					}
				});
					
				if(imgTest.pageTest.pass == imgTest.count){
					eval(e);
				}
				else{
					setTimeout('imgTest.pageTest();',200);
				}
			};
				
			imgTest.pageTest();
		}
	};
	
	$.fn.leftAlign=function(){
		
		var lefts = new Array();
		$(this).each(function(){
			var theItem = $(this);
			$(theItem).css('position','relative');
			var theOffset = $(theItem).offset();
			lefts.push(theOffset.left);
		});
		
		var biggestValue = 1;
		for (x=0;x<lefts.length;x++){
			if (lefts[x] > biggestValue){
				biggestValue = lefts[x];
			};
		};
		
		$(this).each(function(){
			var theMoving = $(this);
			var Offset = $(theMoving).offset();
			var calcOffset = biggestValue - Offset.left;
			$(theMoving).css({"left":calcOffset});
		});
			
	};
	
});
