/*
 * rdai.js
 *
 * All code/functions/classes
 * Author(s): Matt Robenolt, Kevin Krpicak
 *
 * Copyright © 2010 RDA International & YDEK Productions LLC
 * http://rdai.com & http://ydekproductions.com
 * hey@ydekproductions.com
 *
 * $Id: rdai.js 111 2010-03-25 21:39:51Z rdai_paulc $
 *
 * No, don't use this if you somehow get ahold of it.  We will hunt you down
 * and probably inflict massive amounts of pain on your family.
 */

// SHHH! 
var Konami=function(){var a={addEvent:function(e,c,b,d){if(e.addEventListener){e.addEventListener(c,b,false)}else{if(e.attachEvent){e["e"+c+b]=b;e[c+b]=function(){e["e"+c+b](window.event,d)};e.attachEvent("on"+c,e[c+b])}}},input:"",pattern:"3838404037393739666513",load:function(b){this.addEvent(document,"keydown",function(d,c){if(c){a=c}a.input+=d?d.keyCode:event.keyCode;if(a.input.indexOf(a.pattern)!=-1){a.code(b);a.input="";return}},this);this.iphone.load(b)},code:function(b){window.location=b},iphone:{start_x:0,start_y:0,stop_x:0,stop_y:0,tap:false,capture:false,keys:["UP","UP","DOWN","DOWN","LEFT","RIGHT","LEFT","RIGHT","TAP","TAP","TAP"],code:function(b){window.location=b},load:function(b){a.addEvent(document,"touchmove",function(c){if(c.touches.length==1&&a.iphone.capture==true){var d=c.touches[0];a.iphone.stop_x=d.pageX;a.iphone.stop_y=d.pageY;a.iphone.tap=false;a.iphone.capture=false;a.iphone.check_direction()}});a.addEvent(document,"touchend",function(c){if(a.iphone.tap==true){a.iphone.check_direction()}},false);a.addEvent(document,"touchstart",function(c){a.iphone.start_x=c.changedTouches[0].pageX;a.iphone.start_y=c.changedTouches[0].pageY;a.iphone.tap=true;a.iphone.capture=true})},check_direction:function(){x_magnitude=Math.abs(this.start_x-this.stop_x);y_magnitude=Math.abs(this.start_y-this.stop_y);x=((this.start_x-this.stop_x)<0)?"RIGHT":"LEFT";y=((this.start_y-this.stop_y)<0)?"DOWN":"UP";result=(x_magnitude>y_magnitude)?x:y;result=(this.tap==true)?"TAP":result;if(result==this.keys[0]){this.keys=this.keys.slice(1,this.keys.length)}if(this.keys.length==0){this.code(this.link)}}}};return a};
 

rdai = {

	settings: {
		scrollDuration: 1000,
		baseTitle: 'RDA International, Inc.',
		titleSeparator: ' | '
	},
	
	cache: {
		$window: $(window),
		win_height: $(window).height(),
		win_width: $(window).width(),
		maxScrollHeight: 0,
		sections: [],  //<section> tags
		navLinks: []
	},
	
	clickedLink: true,
	isAnimating: false,
	isScrolling: false,
	ie6: $.browser.msie && parseInt($.browser.version) <= 6,
	ie: $.browser.msie,
	k: new Konami(),

	rockAndRoll: function(settings)
	{
		rdai.settings = $.extend({}, rdai.settings, settings); //merge in custom settings
		rdai.cacheCommonQueries();
		rdai.setUpNavLinks();
		rdai.redrawStage();
		rdai.setUpResizeListener();
		rdai.setUpScrollListener();
		rdai.setUpJqueryAddress();
		rdai.fixMenu();
		
		rdai.slideshow.init();
		
		if($.address.value() == '/') $.address.value('/about');

		rdai.k.code = rdai.ryan;
		rdai.k.load();		
	},
	
	cacheCommonQueries: function()
	{
		var c = rdai.cache;
		
		c.sections = $('body > section');   // c-sections  :)
		c.navLinks = $('body > aside > header > nav  a');
		
		$('section menu > li > a').each(function(i, el)
		{
			var $el = $(el);
			var $mask = $el.siblings('.tooltip').children('.mask');
			
			$mask.data('height', $mask.height());
			
			$el.siblings('.tooltip').hide();
		});
		
		
		// For Google Maps
		$('#map_link').each(function(i, el){
			var $el = $(el);
			var $mask = $el.children('.tooltip').children('.mask');
			
			$mask.data('height', $mask.height());
			
			$el.children('.tooltip').hide(); 
		});
	},
	
	setUpNavLinks: function()
	{
		var c = rdai.cache;
		
		$('body > aside > header > nav a').click(function(e)
		{
			e.preventDefault();
			rdai.clickedLink = true;
			$.address.value($(this).attr('href'));
		});
		
		$('.tooltip ul > li').each(function (i,el) 
		{
			$(el).children('a').click(function(e)
			{
				e.preventDefault();
				rdai.clickedLink = true;
				$.address.value($(this).attr('href'));
			});
		});
		
		$('section menu > li').each(function(i, el)
		{
			var $tooltip = $(this).children('div.tooltip');
			var $mask = $tooltip.children('.mask');
			var $maskHeight = $mask.data('height');
			
			$(el).children('a').click(function(e)
			{
				e.preventDefault();
				rdai.clickedLink = true;
				$.address.value($(this).attr('href'));
			});
			
			$(el).hover(function()
			{
				if($.browser.msie)
				{
					$tooltip.show();
					return;
				}
				$mask.css({'height':'0px'}); 
				$tooltip.stop().fadeTo(100, 1);
				$mask.stop().animate({height: $maskHeight}, 'fast'); 
			}
			,function()
			{
				if($.browser.msie)
				{
					$tooltip.hide();
					return;
				}
				$tooltip.stop().fadeOut('fast');
				$mask.stop().animate({height:'0px'}, 'fast');
			});
		});
		
		
		
		//for googlemaps
		$('#map_link').each(function(i, el){
			var $tooltip= $(this).children('div.tooltip');
			var $mask = $tooltip.children('.mask');
			var $maskHeight = $mask.data('height');
			
			$(el).hover(function()
			{
				if($.browser.msie)
				{
					$tooltip.show();
					rdai.map.draw();
					return;
				}
				$mask.css({'height':'0px'}); 
				$tooltip.stop().fadeTo(100, 1);
				$mask.stop().animate({height: $maskHeight}, 'fast', rdai.map.draw);
				
			}
			,function()
			{
				if($.browser.msie)
				{
					$tooltip.hide();
					return;
				}
				$tooltip.stop().fadeOut('fast');
				$mask.stop().animate({height:'0px'}, 'fast');
			});
			
		});
		
		$(c.navLinks[0]).addClass('active');
		
		$('a[rel~=external]').attr('target', '_blank');
	},
	
	setUpResizeListener: function()
	{
		var c = rdai.cache;
		
		c.$window.resize(rdai.redrawStage).resize().resize(); //dumb fucking rendering bug; must force resize twice initially
	},
	
	setUpScrollListener: function()
	{
		var c = rdai.cache;
		
		c.$window.unbind('scroll', rdai.fixMenu).scroll(rdai.fixMenu);
	},
	
	killScrollListener: function()
	{
		var c = rdai.cache;
		
		c.$window.unbind('scroll', rdai.fixMenu);
	},
	
	redrawStage: function()
	{
		var c = rdai.cache;
		
		c.win_height = c.$window.height();
		c.win_width  = c.$window.width();
		
		c.maxScrollHeight = c.win_height * (c.sections.length-1);
		
		if(rdai.ie6){
			//$('body,html').css({width:c.win_width+'px'});
		}
	},
	
	setUpJqueryAddress: function()
	{
		$.address.change(rdai.jumpPage);
	},
	
	movePage: function()
	{
		var c = rdai.cache;
		
		for(var i=0; i<c.sections.length; i++)
		{
			var s = $(c.sections[i]);
			
			s.css('top', (i*c.win_height) - (c.maxScrollHeight*rdai.currentScrollPosition));
		}
	},
	
	jumpPage: function()
	{
		if($.address.pathNames().length > 1) return;
		
		var c = rdai.cache;
		var page = $.address.pathNames()[0] || '';
		
		rdai.setUpScrollListener();
		
		if(!rdai.clickedLink) return;
		
		$(c.navLinks).removeClass('active').parent().find(' a[href=/'+page+']').addClass('active');
		rdai.killScrollListener();
		rdai.isAnimating = true;
		$('html,body').stop().animate({scrollTop: $('#'+page).offset().top+1 +"px"}, rdai.settings.scrollDuration, function()
			{
				rdai.isAnimating = false;
				rdai.setUpScrollListener();
				rdai.clickedLink = false;
			}
		);
		
		rdai.updatePageTitle();
	},
	
	fixMenu: function()
	{
		if(rdai.isAnimating) return;
		
		var c = rdai.cache;
		var el;
		
		if($(window).scrollTop() < c.win_height) el = c.navLinks[0];
		else if($(window).scrollTop() < c.win_height*2) el = c.navLinks[1];
		else el = c.navLinks[2];
		
		if(el != $(c.navLinks).find('.active'))
		{
			$(c.navLinks).removeClass('active')
			$(el).addClass('active');
			
			if(!rdai.clickedLink) $.address.value($(el).attr('href'));
		}
		
		rdai.updatePageTitle();
	},
	
	updatePageTitle: function()
	{
		//var fragment = $.address.pathNames()[0] || '';
		//fragment = fragment.substring(0,1).toUpperCase()+fragment.substring(1);
		document.title = rdai.settings.baseTitle;
		//document.title = rdai.settings.baseTitle + rdai.settings.titleSeparator + fragment;
	},
	
	ryan: function()
	{
		$(".bg:eq(0)").attr("src","images/bg/v1/about_bg.jpg");
		$(".bg:eq(1)").attr("src","images/bg/v1/work_bg.jpg");
		$(".bg:eq(2)").attr("src","images/bg/v1/contact_bg.jpg");
		$("body").css("background-color","#f99408");
		$(".bg").css("background-color","#f99408");
	}
};


// Let the party begin!
$(document).ready(rdai.rockAndRoll);
