
$.browser.msie6 = function(){
	return ($.browser.msie && typeof document.body.style.maxHeight == 'undefined') ? true : false;
}

var Site = {

	init:function(page, subIndex){
		
		if(page.match(/gallery/)){
			this.galleryInit();
		}
		
		$('ul>li:last-child').addClass('last-child');	
		$('ul>li:first-child').addClass('first-child');

		$('a').each(function(){
			var atag = $(this);
			if(atag.attr('href').match(/^http/i) || atag.is('.new-window')){
				atag.click(function(){ window.open($(this).attr('href'), '_newWin'); return false;})
			}
		});
		
		this.sideNavInit(page, subIndex ,function(){$('.banner-annc img').animate({top: 0}, 500).fadeIn(1250)});	
		this.mainNavInit(page);		
		this.flashInit(page);
		this.menusInit(page);
	},
	flashInit:function(page){

		switch(page){
			
			case '/projects/fanpier/site-plan':
			return;
			break;
			
			case 'home':
				var so = new SWFObject("/flash/FallonApplication.swf", "flash-header", "953", "470", "9", "#FFFFFF");
				so.addParam("wmode", "transparent");
				so.write("home-flash");
			break;
			
			default:			
			var page = page.replace(/^\//,'');
			if($('.copy-image').css('display') != 'none'){
				if($('.copy-image').width() == 640){
					var so = new SWFObject("/flash/640x212.swf?dataPath=/lib/xml/templatedata.xml&sizes=horizontal&linkRef="+page, "flash-header", "640", "212", "8", "#FFFFFF");	
					so.addParam("wmode", "transparent");
					so.write("copy-image-flash");					
				}else{
					var so = new SWFObject("/flash/300x400.swf?dataPath=/lib/xml/templatedata.xml&sizes=vertical&linkRef="+page, "flash-header", "300", "400", "8");
					so.addParam("wmode", "transparent");
					so.write("copy-image-flash");	
				}
			}			
			break;		
		}		
		
		
	},
	menusInit:function(page){
		
		$('#nav>ul>li>a').each(function(){
			var me = $(this);

			if(me.next('ul').length > 0){
				var menu  = me.next('ul');
				menu.css({zIndex:5000});
				me.get(0).menuTimer = null;
				me.get(0).menuActive = false;
			
				menu.bind('mouseover',function(e){
					me.get(0).menuActive = true;
					if(!me.is('.active')) me.addClass('active');
					clearTimeout(me.get(0).menuTimer);
				}).bind('mouseout',function(e){
					me.get(0).menuActive = false;
					me.get(0).menuTimer = setTimeout(function(){
						if(me.get(0).menuActive == false){
							menu.slideUp('fast',function(){
								me.removeClass('active');	
							});							
						}
					},250);
				});
			
				me.bind('mouseover',function(e){
					var me = $(e.target);
					var offset = me.offset();
					me.get(0).menuActive = true;
					if(!me.is('.active')) me.addClass('active');
					clearTimeout(me.get(0).menuTimer);
					if(offset.left + menu.width() > $(window).width()){
						offset.left = offset.left + me.get(0).offsetWidth - menu.width();
					}
					
					menu.css({left:offset.left,top:offset.top+me.height()});				
					menu.slideDown('fast');
				})
				.bind('mouseout',function(e){
					var me = $(e.target);					
					me.get(0).menuActive = false;
					me.get(0).menuTimer = setTimeout(function(){
						if(me.get(0).menuActive == false){
							menu.slideUp('fast',function(){
								me.removeClass('active');	
							});							
						}
					},100);				
				});			
			}			
		});		
		
	},
	galleryInit:function(){
		
		if (!$.browser.msie) $('body').css('opacity', 0.999999); // Firefox opacity flicker fix
		
		$('.photo-gallery .photo').each(function(){
			var np = this; // Native
			var p = $(this); // jQuery
			
			
			// Use native <div class="photo"> for storing data
			np.hoverActive = null;
			np.hoverTimer = null;			
			
			// Stop <a> link from executing nad re-route to overlay
			p.find('a').bind('click',function(e){
				Site.loadGalleryImage(p);
				e.stopPropagation();
				return false;				
			});
			p
			.css({position:'relative',cursor:'pointer'})
			.mouseover(function(){			
				var o = p.offset();
				
				// Firefox 3 adds a pixel on absolute elements when the window is an odd number				
				var pixelFix = navigator.userAgent.match(/Firefox\/3\.\d/i) ? ($(window).width()%2 > 0 ? 1 : 0) : 0;
				
				clearTimeout(np.hoverTimer);
							
				if(np.hoverActive == null){
					var d = $('<div>')
					.addClass('gallery-hover').css({
						width:'180px',
						height:'120px',
						position:'absolute',
						top:o.top,
						opacity:.75,
						left:o.left + pixelFix,
						border:'10px solid #FFF',
						backgroundColor:'transparent',
						zIndex:1000
					}).mouseover(function(){
						$(this).css({cursor:'pointer'});
						clearTimeout(p.get(0).hoverTimer);
						np.hoverActive = true;
					}).bind('click',function(e){
						Site.loadGalleryImage(p);
						e.stopPropagation();
					}).mouseout(function(){
						np.hoverActive = false;
						np.hoverTimer = setTimeout(function(){
							if(np.hoverActive === false){
								$(np.hoverDiv).remove();
								np.hoverActive = null;
							}
						},10);
					});
					$('body').append(d);
					np.hoverDiv = d.get(0);					
				}
				np.hoverActive = true;
				
			})
			.mouseout(function(){
				var p = $(this);
				np.hoverActive = false;
				np.hoverTimer = setTimeout(function(){
					if(np.hoverActive === false){
						$(np.hoverDiv).remove();
						np.hoverActive = null;
					}
				},10);
			});
		});	
		
		$('.gallery-paginate a').bind('click',function(e){
			e.stopPropagation();
			e.preventDefault();
			Site.loadGallery($(this).attr('href'));
		});	
		
	},
	galleryCurrent:0,
	galleryCount:0,
	loadGalleryImage:function(p){
		var imgPath = p.find('a').attr('href');
		
		// Create Overlay Div for Large Image
		
		var pos = $('.photo-gallery').offset();
		
		$('body').append(
			$('<div>').attr({id:'gallery-full'}).css({
				position:'absolute',
				top:pos.top,
				left:pos.left,
				width:640,
				padding:'10px',
				background:'#FFF',
				border:'1px solid #999',
				height:385,
				zIndex:10000,
				display:'none'					
			}).append(
				// Add image
				$('<img>').attr({id:'gallery-full-image',src:imgPath}), 
				
				// Add Close Button
				$('<img>').attr('src','/images/gallery-close.gif').css({position:'absolute',bottom:10,right:10,cursor:'pointer'}).click(function(){
					$(this).parent().stop().fadeOut('slow',function(){
						$(this).remove();
					});
				})								
			)							
		);
		
		var pics = $('.photo-gallery .photo');
		Site.galleryCount = 0;
		Site.galleryCurrent = 0;
		pics.each(function(){
			if(this == p.get(0)){
				Site.galleryCurrent = Site.galleryCount;
			}
			Site.galleryCount++;
		});
				
						
		// Add Prev/Next Button
		
		if(Site.galleryCurrent < Site.galleryCount-1){
			Site.galleryAddNext();
		}
		
		if(Site.galleryCurrent > 0){
			Site.galleryAddPrev();
		}		
		
		var loading = $('<img src="/images/loadingAnimation.gif" />').css({
			position:'absolute',
			top:170,
			left:200,
			zIndex:3000
		});
		
		$('.photo-gallery').append(loading);		
		
		// Preload Image
		$.ajax({url: imgPath, async:false, cache:true, complete:function(){
			loading.remove();
			$('#gallery-full').fadeIn();
		}});

		
	},
	galleryAddNext:function(){
		var nextbtn = $('<img>').addClass('galleryNext').attr('src','/images/gallery-next.gif').css({opacity:.35,position:'absolute',right:10,top:175,cursor:'pointer'});

		$('#gallery-full').append(
			nextbtn
				.hover(
					function(){$(this).css('opacity',.65)},function(){$(this).css('opacity',.35)}
				)
				.click(
					function(){
						var next= $(this);
						$('#gallery-full-image').fadeOut(function(){
							var grp = $('.photo-gallery .photo:eq('+(Site.galleryCurrent+1)+') a');
							if(grp.length ==0){
								$('#gallery-full').fadeOut('slow',function(){
									$(this).remove();
									return;
								});
							}else{
								if(Site.galleryCurrent < Site.galleryCount-1  && $('.galleryPrev').length==0){
									Site.galleryAddPrev();
								}
								$(this).attr('src', grp.attr('href')).fadeIn(function(){
									Site.galleryCurrent++;
								});
							}
						});					
					}
				)
		);		
		
	},
	galleryAddPrev:function(){
		var prevbtn = $('<img>').addClass('galleryPrev').attr('src','/images/gallery-prev.gif').css({opacity:.35,position:'absolute',left:10,top:175,cursor:'pointer'});
		$('#gallery-full').append(
			prevbtn
				.hover(
					function(){$(this).css('opacity',.65)},function(){$(this).css('opacity',.35)}
				)				
				.click(function(){
					var prev= $(this);
					$('#gallery-full-image').fadeOut(function(){
						var grp = $('.photo-gallery .photo:eq('+(Site.galleryCurrent-1)+') a');
						if(grp.length ==0){
							$('#gallery-full').fadeOut('slow',function(){
								$(this).remove();
								return;
							});
						}else{
							if(Site.galleryCurrent > 0 && $('.galleryNext').length==0){
								Site.galleryAddNext();
							}
							$(this).attr('src', grp.attr('href')).fadeIn(function(){
								Site.galleryCurrent--;
							});
						}
					});					
				}
			)
		);		
	},
	mainNavInit:function(page){
		var page = page.replace(/^\//,'').split(/\//).slice(0,1);
		page = page == '' ? 'home' : page;
		var re = new RegExp(page, 'gi');
		$('#nav>ul>li>a').each(function(){	
			var atag = $(this);		
			if(atag.attr('href').match(re)){
				atag.addClass('active');
			}
		});
		
	},
	sideNavInit:function(page, menuIndex, callBack){
		var origpage = page;
		var path = page.replace(/^\//,'').split(/\//);
		var page = path.slice(0,1);
		var subpage = path.slice(1,1);
		var menuIndex = typeof menuIndex == 'undefined' ? 1 : isNaN(menuIndex) ? 1 : menuIndex;		
		$('.side-nav li.'+page).slideDown(1250,function(){;
			var atag = $(this).find('ul>li:nth-child('+menuIndex+')>a');
			var atag = $(atag.get(0));			
			atag.addClass('active');
			if(atag.next('ul').length > 0){
				atag.next('ul').slideDown(1000,function(){
					var subtag  = $(this);
					subtag.find('a').each(function(){
						if($(this).attr('href') == origpage){
							$(this).addClass('subactive');
						}
					});
					if(typeof callBack == 'function'){
						callBack.apply(this,[this]);
					}
				});			
			}else{
				if(typeof callBack == 'function'){
					callBack.apply(this,[this]);
				}				
			}
		});		
	},
	loadGallery:function(path){
		var galleryParams = path.substr(path.indexOf('?')+1);
		var params = 'ajax=loadGallery&'+galleryParams;
		var loading = $('<img src="/images/loadingAnimation.gif" />').css({
			position:'absolute',
			top:170,
			left:200
		});

		var hgt = $('.photo-gallery').height();
		
		$('.photo-gallery').css({height:hgt});
		
		$.blockUI.defaults.baseZ = 12000;
		$.blockUI.defaults.fadeOut = 100; 
		$.blockUI.defaults.css.backgroundColor = 'transparent'; 
	    $.blockUI.defaults.overlayCSS = {  
	        backgroundColor:'#FFF',  
	        opacity: .35
	    };	
		
		$('.photo-gallery').block({message:'<img src="/images/loadingAnimation.gif" />',css:{border:0}});			
		
		$.ajax({
			type	: "POST",
			url		: "/index.php",
			dataType: 'text',
			async	: true,
			data	: params,
			success: function(content){
				$('.photo-gallery').fadeTo('slow',.25,function(){
					$('.photo-gallery').unblock();
					$(this).html(content).fadeTo('slow',1,function(){
						$('.photo-gallery').css({height:'auto'});
						Site.galleryInit();						
					});
				});
			}
		});				
	}
}
