var manire = 
{
	currentPage : 'home',
	navPage : '',
	currentWork : 'our_work',
	navWork : '',
	subNav1 : '',
	subNav2 : '',
	navAbout : '',
	queue : new Array(),
	isRunning : false,
	i: 0,
	interval : 0,
	origTitle : document.title,
	aboutCircles : new Array(),
	preloader_show: true,
	portfolio : new Array(),
	portfolioIndex : 0,
	casesOpen : false,
	caseID: -1,
	current_hash : window.location.hash,
	last_call : 0,
	bypass_restriction : false,
	validateCall : function()
	{
		if(manire.bypass_restriction == true)
		{
			manire.bypass_restriction = false;
			return true;
		}

		var d = new Date();
		var current = d.getSeconds();
		var diff = parseInt(current) - parseInt(manire.last_call);
		if(diff < 0)
		{
			manire.last_call = 0;
			return true;
		}

		if(diff >= 0 && diff < 3)
		{
			//alert('not going anywhere ' + diff);
			return false;
		}
		else
		{
			manire.setLastCall();
			return true;
		}
	},
	init : function()
	{
		manire.setLastCall();
		$('#home .content').fadeIn('fast');
		$('#navigation').fadeIn('fast');
		manire.flashBubbles();
		setTimeout(manire.loadHome,250);
		clearInterval(manire.preloader);
		//Check hash and navigate to new page
		if (manire.current_hash != "") 
		{
			manire.navigate(manire.current_hash.substr(2));
		}
	},
	setLastCall : function()
	{
		var d = new Date();
		manire.last_call = d.getSeconds();
	},
	preloadInit : function()
	{
		manire.preloader_show = !manire.preloader_show;
		if(!manire.preloader_show)
			$("#preloader").fadeIn(2000,manire.preloadInit);
		else
			$("#preloader").fadeOut(2000,manire.preloadInit);
	},
	navigate : function(pg)
	{
		$.address.value(pg);
		if(pg != manire.currentPage)
		{
			manire.navPage = pg;
			$.address.title(this.origTitle + " // " + pg.capitalize());
			$("#navigation").fadeOut('fast');

			this.unloadCurrent();
		}
	},
	navigateTo : function()
	{
		$('.testimonial').each(function(){
			var bid = $(this).attr('id');
			var bid_vars = bid.split("_");
			var id = bid_vars[1];

			$('#testimonial_full_' + id).css("display", "none");
			$("#testimonial_"+id).removeClass('isFull');
		});

		$('.news_desc_block').each(function(){
			var e_id = this.id;
			var id = e_id.split("news_article_");
			$('#' + e_id).css("display", "none");
			$('#news_article_short_' + id[1]).css("display", "block");
		});

		manire.currentPage = manire.navPage;
		manire.navPage = '';
		var p = 0;

		var pag = manire.currentPage.split("-");

		switch(pag[0])
		{
			case 'home': p=0; break;
			case 'about': p=-900; break;
			case 'work': p=-1575; break;
			case 'contact' : p=-2660; break;
			case 'news' : 
			case 'careers' :
			case 'privacy' :
			case 'sitemap' :
				p=-2660; break;
		}
		if($("#parent").css('left') != p+"px")
			$("#parent").animate({left: p+"px"},800,manire.navigateFinish);
		else
			manire.navigateFinish();
	},
	unloadCurrent : function()
	{
		var pag = this.currentPage.split("-");

		switch(pag[0])
		{
			case 'home' : manire.navigateTo(); break; 
			case 'about': $("#about .current-content").fadeOut('slow',function(){manire.interval = setInterval(manire.unloadAbout,10);}); break;
			case 'work' : $("#work .current-content").fadeOut(1000,function(){ 
					$("#cases").css('display','none'); 
					manire.interval = setInterval(manire.unloadWork,20);
					var player = $f("player");
					if (player && player.isLoaded()) {
						$f().unload();
					}
				}); break;
			case 'contact' : $("#fn_"+pag[0]).removeClass('fn_selected'); $("#contact").fadeOut('slow',function(){ manire.interval = setInterval(manire.unloadNewsContact,20); }); break;
			case 'news' : $("#news").fadeOut('slow',function(){ manire.interval = setInterval(manire.unloadNewsContact,20); }); break;
			case 'privacy' : $("#fn_"+pag[0]).removeClass('fn_selected'); $("#privacy").fadeOut('slow',function(){ manire.interval = setInterval(manire.unloadNewsContact,20); }); break;
			case 'careers' : $("#fn_"+pag[0]).removeClass('fn_selected'); $("#careers").fadeOut('slow',function(){ manire.interval = setInterval(manire.unloadNewsContact,20); }); break;
			case 'sitemap' : $("#fn_"+pag[0]).removeClass('fn_selected'); $("#sitemap").fadeOut('slow',function(){ manire.interval = setInterval(manire.unloadNewsContact,20); }); break;
		}
	},
	navigateFinish : function()
	{
		$("#navigation > li a").removeClass('current');
		$("#navigation li a[title='"+manire.currentPage+"']").addClass('current');

		var pag = manire.currentPage.split("-");

		switch(pag[0])
		{
			case 'home':
				
				$("#navigation").css('left','650px');
				$("#navigation").css('top','65px');
				$("#navigation").fadeIn('fast');
			break;
			case 'about':
				manire.interval = setInterval(manire.loadAbout,10);
			break;
			case 'work':
				manire.bypass_restriction = true;
				manire.interval = setInterval(manire.loadWork,20);
				if(manire.portfolio.length == 0)
				{
					$.post('ajax/portfolio', {}, function(data)
					{
						manire.portfolio = data.results;
					},"json");	
				}
			break;
			case 'contact' : 
			case 'news' :
			case 'careers' :
			case 'privacy' :
			case 'sitemap' :
				$("#navigation").css('left','2700px');
				$("#navigation").css('top','425px');
				$("#navigation").fadeIn('fast');
				$("#contact_container").show();
				$("#contact_success").hide();

				if(pag[0] != 'news')
					$("#fn_"+pag[0]).addClass('fn_selected');

				manire.interval = setInterval(manire.loadNewsContact,20);
				
			break;	
		}
	},
	loadHome : function()
	{
		$("#home .circles").eq(manire.i).css('display','none'); manire.i++;
		if(manire.i < $("#home .circles").length) { setTimeout(manire.loadHome,250);  }else{ manire.i=0; }
	},
	unloadAbout : function()
	{
		$("#about .circles").eq(manire.i).css('display','none'); $("#about .circles").eq((manire.i + 1)).css('display','none');	manire.i+=2;
		if(manire.i >= $("#about .circles").length) {	clearInterval(manire.interval); manire.i=0; manire.unloadAboutFinish(); }
	},
	loadAbout: function()
	{

		$("#about .circles").eq(manire.i).css('display','block'); $("#about .circles").eq((manire.i + 1)).css('display','block'); manire.i+=2;
		if(manire.i >= $("#about .circles").length) {
			clearInterval(manire.interval); 
			manire.i=0; 
			manire.loadAboutFinish();
			var pag = manire.currentPage.split("-");
			if(pag[1] != null)
			{
				manire.aboutNavigate(manire.currentPage);
			}
		}
	},
	unloadAboutFinish : function()
	{
		//Reset About to Main Page
		$("#about > .current-content").removeClass('current-content');
		$("#about_us").addClass('current-content');
		$("#navigation .about").css('display','none');
		//Initiate Navigation
		manire.navigateTo();
	},
	loadAboutFinish : function()
	{
		$("#navigation li ul li a[title='about_us']").addClass('current');
		
		var pag = manire.currentPage.split("-");
		if(pag[1] == null)
		{
			$("#about .current-content").fadeIn('slow');
		}
		
		$("#navigation").css('left','1585px');
		$("#navigation").css('top','425px');
		$("#navigation .about").css('display','block');
		$("#navigation").fadeIn('fast');
	},
	aboutNavigate : function(pg)
	{
		$.address.value(pg);
		var pag = pg.split("-");

		manire.navAbout = pag[1];
		if(pag.length > 2)
			manire.subNav1 = pag[2];

		if(pag.length > 3)
			manire.subNav2 = pag[3];

		$("#navigation li ul li a").removeClass('current');
		$("#navigation li ul li a[title='"+pag[1]+"']").addClass('current');
		$("#about .current-content").fadeOut('slow',function(){ $("#about > .current-content").removeClass('current-content'); $("#"+pag[1]).addClass('current-content'); $("#"+pag[1]).fadeIn('slow'); }); 
	},
	loadWork : function()
	{
		var obj = $("#work .circles").eq(manire.i);
		var left = obj.css('left');
		left = Number(left.substr(0,left.length - 2));
		var top = obj.css('top');
		top = Number(top.substr(0,top.length - 2));
		if(left > 0 && left < 520 && top > 200)
			obj.css('display','block'); 
		else
			obj.css('display','none');
		manire.i++;
		if(manire.i >= $("#work .circles").length) { 
			clearInterval(manire.interval); 
			manire.i=0; 
			manire.loadWorkFinish();

			var pag = manire.currentPage.split("-");
			if(pag[1] != null)
			{
				manire.workNavigate(manire.currentPage);
			}
		}
	
	},
	unloadWork : function()
	{
		$("#work .circles").eq(manire.i).css('display','none'); manire.i++;	
		if(manire.i >= $("#work .circles").length) { clearInterval(manire.interval); manire.i=0; manire.unloadWorkFinish();}
	},
	loadWorkFinish : function()
	{
		var pag = manire.currentPage.split("-");
		if(pag[1] == null)
		{
			$("#work .current-content").fadeIn('slow');	
		}

		if(pag.length > 2)
			manire.subNav1 = pag[2];

		if(pag.length > 3)
			manire.subNav2 = pag[3];
		
		$("#navigation").css('left','1685px');
		$("#navigation").css('top','425px');
		$("#navigation").fadeIn('fast');
		$("#navigation .work").css('display','block');
		$("#navigation li ul li a[title='our_work']").addClass('current');
	},
	unloadWorkFinish : function()
	{
		$("#work > .current-content").removeClass('current-content');
		$("#our_work").addClass('current-content');
		$("#navigation .work").css('display','none');
		manire.navigateTo();
	},
	workNavigate : function(pg)
	{
		if(manire.validateCall())
		{
			var player = $f("player");
			if (window.player && player.isLoaded()) {
				$f().unload();
			}

			$.address.value(pg);
			var pag = pg.split("-");

			manire.navWork = pag[1];
			if(pag.length == 2)
			{
				manire.subNav1 = '';
				manire.subNav2 = '';
			}

			if(pag.length > 2)
				manire.subNav1 = pag[2];

			if(pag.length > 3)
				manire.subNav2 = pag[3];

			$("#navigation li ul li a").removeClass('current');
			$("#navigation li ul li a[title='"+pag[1]+"']").addClass('current');
			manire.unloadCurrentWork();
		}
	},
	workNavigateGo : function()
	{
		manire.currentWork = manire.navWork;
		manire.navWork = '';
		$("#work > .current-content").removeClass('current-content'); $("#"+manire.currentWork).addClass('current-content');
		$("#portfolio_footer").css('display','none');

		switch(manire.currentWork)
		{
			case "our_work" : 
				
				if($('#bubble_6').css("display") != "block")
				{
					$('#bubble_6').fadeIn("slow"); 
					$('#bubble_7').fadeIn("slow");
				}
				
				manire.interval = setInterval(manire.loadWork,20); 
			break;	
			case "portfolio" : 
				
				if($('#bubble_6').css("display") == "block")
				{
					$('#bubble_6').fadeOut("slow"); 
					$('#bubble_7').fadeOut("slow");
				}
				
				$('#our_work').css('display', 'none'); 
				$("#portfolio").css('display','block'); 
				$("#portfolio").css('top','-700px'); 
				$("#portfolio").animate({top: "0px"},1000,function(){ 
					$("#portfolio_footer").fadeIn('fast');
				});
				var address = $.address.value();
				var pag = address.split("-");
				if(pag.length > 2)
				{
					manire.portfolioIndex = pag[2];
					manire.setPortfolio();
				}
				else
				{
					manire.portfolioIndex = 0;
					manire.setPortfolio();
				}
			break;
			case "cases" : 
				
				if($('#bubble_6').css("display") == "block")
				{
					$('#bubble_6').fadeOut("slow"); 
					$('#bubble_7').fadeOut("slow");
				}
				
				$('#our_work').css('display', 'none'); 
				if(manire.casesOpen){ 
					manire.interval = setInterval(manire.loadCases,20); 
				}else{
					manire.interval = setInterval(manire.loadCaseCircles,20); 
				}
			break;
			case "awards" :
				
				if($('#bubble_6').css("display") != "block")
				{
					$('#bubble_6').fadeIn("slow"); 
					$('#bubble_7').fadeIn("slow");
				}
				
				$('#our_work').css('display', 'none'); 
				manire.interval = setInterval(manire.loadWorkAwards,20); 
			break;
		}
	},
	unloadCurrentWork : function()
	{
		switch(manire.currentWork)
		{
			case "our_work" : 
			case "awards" :
			$("#work .current-content").fadeOut('slow',manire.workNavigateGo); break;	
			case "cases" : $("#cases").css('display','none'); manire.workNavigateGo(); break;
			case "portfolio" : $("#portfolio_footer").fadeOut('fast'); $("#portfolio").animate({top: "-700px"},1000,manire.workNavigateGo); break;
		}
	},
	loadWorkAwards : function()
	{
		var obj = $("#work .circles").eq(manire.i);
		var left = obj.css('left');
		left = Number(left.substr(0,left.length - 2));
		if(left > 425 && left < 725)
			obj.css('display','block'); 
		else
			obj.css('display','none'); 
		manire.i++;
		if(manire.i >= $("#work .circles").length) { clearInterval(manire.interval); manire.i=0; $("#work .current-content").fadeIn('slow'); }
	},
	embedVideo : function(type, file)
	{
		if(type == 'file')
		{
			$("#portfolio_img").html('<a href="/media/uploaded' + file +'" style="display:block;width:820px;height:495px" id="player"></a>');
			flowplayer("player", "/media/flash/flowplayer.commercial-3.1.5.swf", {key: '#$56aeca8c24fc9f4d582',clip: {autoPlay:false}});
		}
		else if(type == 'embed')
			$("#portfolio_img").html(file);
	},
	portfolioLeft : function()
	{
		if(manire.portfolioIndex == 0)
			manire.portfolioIndex = manire.portfolio.length - 1;
		else
			manire.portfolioIndex--;
		manire.setPortfolio();		
	},
	portfolioRight : function()
	{
		if(manire.portfolioIndex == manire.portfolio.length - 1)
			manire.portfolioIndex = 0;
		else
			manire.portfolioIndex++;
		manire.setPortfolio();		
	},
	setPortfolio : function()
	{
		if(manire.portfolioIndex > 0)
			$.address.value('work-portfolio-' + manire.portfolioIndex);
		else
			$.address.value('work-portfolio');

		var title = (manire.portfolio[manire.portfolioIndex]['url'] != ""?'<a href="'+manire.portfolio[manire.portfolioIndex]['url']+'" target="_blank">'+manire.portfolio[manire.portfolioIndex]['title']+'</a>':manire.portfolio[manire.portfolioIndex]['title']);
		var embed = manire.portfolio[manire.portfolioIndex]['embed'];
		var flv_url = manire.portfolio[manire.portfolioIndex]['flv_url'];
		$("#portfolio_footer .titles h2").html(title);
		$("#portfolio_footer .titles p").html(manire.portfolio[manire.portfolioIndex]['description']);
		$("#portfolio_img img").css('display','none');
		$("#portfolio_img img").load(function(){ $("#portfolio_img img").fadeIn('fast'); });
		if((embed != null && embed != '') || (flv_url != null && flv_url != ''))
		{
			if(embed != null && embed != '')
				manire.embedVideo('embed', embed);
			else if(flv_url != null && flv_url != '')
				manire.embedVideo('file', flv_url);

		}
		else
		{
			$("#portfolio_img").html('<img src="" alt="" />');
			$("#portfolio_img img").attr('src','/media/uploaded/'+manire.portfolio[manire.portfolioIndex]['image_url']);		
		}
	},
	loadCases : function()
	{
		var obj = $("#work .circles").eq(manire.i);
		obj.css('display','block'); 
		manire.i++;
		if(manire.i >= $("#work .circles").length) { clearInterval(manire.interval); manire.i=0; manire.loadCasesFinish();}		
	},
	loadCaseCircles : function()
	{
		var obj = $("#work .circles").eq(manire.i);
		obj.css('display','none'); 
		manire.i++;
		if(manire.i >= $("#work .circles").length) { 
			clearInterval(manire.interval); 
			manire.i=0; 
			$("#cases").fadeIn('fast'); 
			
			if(manire.subNav1 != '')
				manire.openCase(manire.subNav1);
		}	

	},
	openCase : function(id)
	{
		if(manire.caseID != id) //!manire.casesOpen &&  was removed
		{
			$('#case'+manire.caseID).fadeOut('slow');
			manire.caseID = id;

			$.address.value('work-' + manire.currentWork + '-' + id);

			if(!manire.casesOpen)
				manire.interval = setInterval(manire.loadCases,20);
			else
				$('#case'+manire.caseID).fadeIn('slow');
		}
	},
	openCaseImage : function(url)
	{
		$("#case_image_viewer img").attr('src',url);
		$("#case_image_viewer").css('display','block'); $("#case_image_viewer").css('top','-700px'); $("#case_image_viewer").animate({top: "0px"},1000,
																									function(){ $("#case_image_viewer_footer").fadeIn('fast');});
	},
	closeCaseImage : function()
	{
		$("#case_image_viewer_footer").fadeOut('fast'); $("#case_image_viewer").animate({top: "-700px"},1000);
	},
	loadCasesFinish  : function()
	{
		if(!manire.casesOpen)
		{
			manire.casesOpen = true;
			$(".case_bubble").animate({left:'795px',top:'165px'},1500,redrawCases);
		}
		else
			$("#cases").fadeIn('fast');
		function redrawCases()
		{
			for(var i = 0; i < $(".case_bubble").length; i++)
			{
				$(".case_bubble").eq(i).animate({width:'80px',height:'80px',top:(226 + (i*85))+'px',left:'897px'},1000);
			}
			$('#case'+manire.caseID).fadeIn('slow');
		}		
	},
	loadNewsContact : function()
	{
		var pag = manire.currentPage.split("-");

		var obj = $("#contact_news_parent .circles").eq(manire.i);
		obj.css('display','block'); 
		manire.i++;
		if(manire.i >= $("#contact_news_parent .circles").length) { 
			clearInterval(manire.interval); 
			manire.i=0; 
			$("#"+pag[0]).fadeIn('fast');
			if(pag.length > 1)
			{
				manire.loadNewsArticle(pag[1]);
				var targetOffset = $('#news_title_' + pag[1]).offset().top; 
				$('html, body').animate({scrollTop: targetOffset - 50}, 400); 
			}
		}		
	},
	unloadNewsContact : function()
	{
		var obj = $("#contact_news_parent .circles").eq(manire.i);
		obj.css('display','none'); 
		manire.i++;
		if(manire.i >= $("#contact_news_parent .circles").length) { clearInterval(manire.interval); manire.i=0; manire.navigateTo(); }				
	},
	loadNewsArticle : function(id)
	{
		//$('html, body').animate({scrollTop:0}, 'fast');
		//$('#news_article_' + id).slideUp("fast");

		$.address.value('news-' + id);

		$.each($('.news_desc_block'), function(){
			var e_id = this.id;
			var id = e_id.split("news_article_");
			manire.unloadNewsArticle(id[1]);
		});

		$('#news_article_short_' + id).slideUp("fast");
		setTimeout(function(){

			$.ajax({
				type: "POST",
				url: '/ajax/newsArticle',
				data: 'id=' + id,
				dataType: "json",
				success: function(res) {
					$('#news_title_' + id).html(res.title);
					$('#news_article_' + id).html(res.body);
					$('#news_article_' + id).slideDown("fast");
				}
			});	

		}, 100);
	},
	unloadNewsArticle : function(id)
	{
		$('#news_article_short_' + id).slideDown("fast");
		$('#news_article_' + id).slideUp("fast");
		setTimeout(function(){

			$.ajax({
				type: "POST",
				url: '/ajax/newsArticle',
				data: 'id=' + id,
				dataType: "json",
				success: function(res) {
					$('#news_title_' + id).html(res.short_title);
				}
			});	

		}, 100);

	},
	transition : function(page, action)
	{
		switch(page)
		{
			case 'home':
				manire.navigate('home');
				break;
			case 'about':
				manire.navigate('about');
				setTimeout(function()
				{
					manire.aboutNavigate(action);	
				}, 2000);
				break;
			case 'work':
				manire.navigate('work');
				setTimeout(function()
				{
					manire.workNavigate(action);	
				}, 2000);
				break;
			case 'contact':
				manire.navigate('contact');
				break;
			case 'news':
			case 'privacy':
			case 'careers':
				manire.navigate('news');
				
				if(page == 'news')
				{
					setTimeout(function()
					{
						manire.loadNewsArticle(action);	
					}, 2000);
				}
				break;
		}

	},
	expandTestimonial : function(id)
	{
		if($("#testimonial_"+id).hasClass('isFull'))
		{
			$('#testimonial_full_' + id).fadeOut("medium");
			//$("#testimonial"+id+" .testimonial_full").hide(1);
			//$("#testimonial"+id+" .testimonial_description").show(1);
			//$("#testimonial"+id).width($("#testimonial"+id).width() / 1.5);
			$("#testimonial_"+id).removeClass('isFull');
			//$("#testimonial"+id+" a").html('more');
		}
		else
		{
			$('#testimonial_full_' + id).fadeIn("medium");
			//$("#testimonial"+id+" .testimonial_description").hide(1);
			//$("#testimonial"+id+" .testimonial_full").show(1);
			//$("#testimonial"+id).width($("#testimonial"+id).width() * 1.5);
			$("#testimonial_"+id).addClass('isFull');
			//$("#testimonial"+id+" a").html('less');
		}
		/*
		if($("#testimonial"+id).hasClass('isFull'))
		{
			$("#testimonial"+id+" .testimonial_full").hide(1);
			$("#testimonial"+id+" .testimonial_description").show(1);
			//$("#testimonial"+id).width($("#testimonial"+id).width() / 1.5);
			$("#testimonial"+id).removeClass('isFull');
			$("#testimonial"+id+" a").html('more');
		}
		else
		{
			$("#testimonial"+id+" .testimonial_description").hide(1);
			$("#testimonial"+id+" .testimonial_full").show(1);
			//$("#testimonial"+id).width($("#testimonial"+id).width() * 1.5);
			$("#testimonial"+id).addClass('isFull');
			$("#testimonial"+id+" a").html('less');
		}	
		*/
	},
	scrollTestimonial: function(id, dir)
	{
		var cur = $('#tinner_' + id).css('marginTop');
		var max = $('#tinner_' + id).css('height');
		
		var mm = max.split('px');
		var md = mm[0];
		var f = cur.split('px');
		var c = f[0];

		var move = 0;

		if(dir == 'up')
		{
			if(c < 0)
				move = "+=80";
		}
		else
		{
			if((md / 2) * -1 < c)  
				move = "-=80";
		}
		
		if(move != 0)
			$('#tinner_' + id).animate({'marginTop': move + 'px'}, 200);
	},
	flashBubbles : function(page)
	{
		
		$(".flash_bubble").css('z-index','99');

		$('#bubble_1').flash({ 
			swf: '/media/images/pop1.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});	

		$('#bubble_2').flash({ 
			swf: '/media/images/pop2.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});
	

		$('#bubble_3').flash({ 
			swf: '/media/images/pop3.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});	

		$('#bubble_4').flash({ 
			swf: '/media/images/pop4.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});

		$('#bubble_5').flash({ 
			swf: '/media/images/pop5.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});	

		$('#bubble_6').flash({ 
			swf: '/media/images/pop6.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});

		$('#bubble_7').flash({ 
			swf: '/media/images/pop7.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});	

		$('#bubble_8').flash({ 
			swf: '/media/images/pop8.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});

		$('#bubble_9').flash({ 
			swf: '/media/images/pop9.swf',  
			width: 220,
			height: 220,
			wmode: 'transparent'
		});

		
	}
}

$(document).ready(function()
{		
	$(document).pngFix(); 

	//Load background image
	var bg = $("<img>");
	$(bg).load(function(){ $('#preloader').remove(); $('#home .overlay').fadeIn('fast'); $('#background-image').fadeIn('slow',function(){ manire.init(); })});
	$("#parent").append(bg);
	$(bg).attr('id','background-image');
	$(bg).attr('src','/media/images/background.jpg');
	$("#preloader").fadeOut(2000,manire.preloadInit);
	$("a[rel='image']").colorbox({transition:"fade"});
	
	var contact_form_options = {
		
		url:	'/ajax/contact',
		type:	'POST',
		dataType:	'json',
		beforeSubmit : function(arr, form, opt)
		{
			var form = form[0]; 
			if(!form.name.value || !form.company.value || !form.title.value || !form.phone.value || !form.email.value)
			{
				$("#contact_error").html("Please fill in all required fields.");
				$("#contact_error").show('fast');
				return false;
			}
		},
		success:	function(res)
		{
			if(res.results == 'error')
			{
				$("#contact_error").html(res.message);
				$("#contact_error").show('fast');
			}
			else
			{
				$("#contact_success").html(res.message);
				$("#contact_container").hide('fast',function(){ 
					$("#contact_success").show('fast'); 
					$('[name=name]').val('');
					$('[name=company]').val('');
					$('[name=title]').val('');
					$('[name=phone]').val('');
					$('[name=email]').val('');
					$('[name=address]').val('');
					$('[name=city]').val('');
					$('[name=state]').val('');
					$('[name=zip]').val('');
					$('[name=comments]').val('');
				});

			}
		}

	};

	$(".fg-button").hover(
		function(){ 
			$('#form_submit_button').attr('src', '/media/images/submit_over.gif'); 
		},
		function(){ 
			$('#form_submit_button').attr('src', '/media/images/submit.gif'); 
		}
	);

	$(".fg-button").mousedown(
		function(){ 
			$('#form_submit_button').attr('src', '/media/images/submit_click.gif'); 
		}
	);

	$(".fg-button").mouseup(
		function(){ 
			$('#form_submit_button').attr('src', '/media/images/submit.gif'); 
		}
	);

	
	$('#contact_form').ajaxForm(contact_form_options); 
	
	$.address.externalChange(function(event)
		{
			var segments = event.path.split("/");
			if(segments[1] != "" && manire.currentPage == "home")
				manire.navigate(segments[1]);

			return false;
		}
	);

	$("#follow_box").hover(
		function(){ $("#follow_list").fadeIn("medium"); 
			setTimeout(function(){
				$('#fl1').fadeIn("medium");
			}, 50);
			setTimeout(function(){
				$('#fl2').fadeIn("medium");
			}, 100);
			setTimeout(function(){
				$('#fl3').fadeIn("medium");
			}, 150);
			setTimeout(function(){
				$('#fl4').fadeIn("medium");
			}, 200);
		}, 
		function() { } 
	);

	$("#follow_list").hover(
		function() { },
		function(){ 
			$('#fl4').fadeOut("medium");

			setTimeout(function(){
				$('#fl3').fadeOut("medium");
			}, 50);
			setTimeout(function(){
				$('#fl2').fadeOut("medium");
			}, 100);
			setTimeout(function(){
				$('#fl1').fadeOut("medium");
			}, 150);
			setTimeout(function(){
				$("#follow_list").fadeOut("medium");
			}, 200);
		 }
	);

});

String.prototype.capitalize = function(){return this.replace(/\w+/g, function(a){return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();});}; function getFlashObject(n){if(window.document[n]){return window.document[n];}if (navigator.appName.indexOf("Microsoft Internet")==-1){if(document.embeds && document.embeds[n]){return document.embeds[n];}}else{return document.getElementById(n);}} function randOrd(){return (Math.round(Math.random())-0.5); } 

function popBubble(id)
{
	$('#bmsg_' + id).fadeIn("fast");
	setTimeout(function(){
		$('#bmsg_' + id).fadeOut("fast");
	}, 2800);
}
/*
$(function(){
	positionFooter(); 
	function positionFooter(){
		$("#footer_nav").animate({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#footer_nav").height())+"px"}, 100)	
	}
 
	$(window)
		.scroll(positionFooter)
		.resize(positionFooter)
});
*/
