
function isiPhone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPad") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)
    );
}

// remap jQuery to $
(function($){

	function preload(arrayOfImages) {
		$(arrayOfImages).each(function(){
			$('<img/>')[0].src = this;
		});
	}
	preload([
		'img/logos/logo-rtc-black.png',
		'img/logos/logo-rtc-blue.png',
		'img/logos/logo-rtc-red.png',
		'img/logos/logo-rtc-green.png',
		'img/logos/logo-rtc-orange.png',
		'img/logos/logo-rtc-purple.png',
		'img/logos/logo-rtc-gray.png'
	]);		

	function goToPage(href) {
		arID = href+'_';
		$('body').removeClass().addClass($('header a'+href).attr('class'));
		if (isiPhone()) {
			$('body').scrollTo(arID,500);
		}
		else {
			$('div#viewport').scrollTo(arID,500,{offset:-$('section').css('padding-left').replace('px','')});
			$('aside').fadeIn(500);
			$(arID).find('aside').fadeOut(500);
			$('section').height($(arID).outerHeight());
			if ($('section').height() < $('div#viewport').height()) $('section').height($('div#viewport').height());
		}
		$('header a').removeClass('On');
		$('header a'+href).addClass('On')
	}

	//same as $(document).ready();
	$(function() {
		
		$('header a').click(function(e){
			e.preventDefault();
			window.location.hash = $(this).attr('id');
		});
		
/*		$('aside').hover(function(){
			$(this).animate({ opacity: 0.85 },300);
		},function(){
			$(this).animate({ opacity: 1 },300);
		});*/
		
		$('.fancybox').fancybox({
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.8
		});
		
	});


	$(window).bind("load", function() {
		
		var $winW = $(window).width();
		var $winH = $(window).height();
		var $secW = $('section').width();
		var $arOW = $('article').outerWidth();

		if (!isiPhone()) {

			$('section').width( $('article').length * $arOW );
			if ( $winW > $arOW ) $('section').width( $secW + ( $winW - $arOW ) );
	
			$('section').height($('#'+$('header a.On').attr('id')+'_').outerHeight());
			if ($('section').height() < $('div#viewport').height()) $('section').height($('div#viewport').height());
			
		}
	
		var storedHash = window.location.hash;
		window.setInterval(function() {
			if (window.location.hash != storedHash) {
				storedHash = window.location.hash;
				goToPage(storedHash);
			}
		}, 100);
		
		$('#Careers_ tr.description td').hide();
		
		$('body').fadeIn(400);

		if (!isiPhone()) {

			$('div#viewport').height( $winH - $('header').height() );
			
			$('#RTC-is').VideoJS();
			
			$f("flashPlayer", "http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf", {
				playlist: [
					{
						url: 'http://rtc.com/img/vid/RTC-is-flash.png', 
						scaling: 'orig'
					},
					{
						url: 'http://rtc.com/vid/RTC-is.mp4', 
						autoPlay: false, 
						autoBuffering: true
					}
				],
				play: { opacity: 0 },
				plugins: { controls: null }
			}).controls("vimBar", {duration: 25});

			$('aside').each(function(){
				$(this).width($(this).closest('article').outerWidth()).height($(this).closest('article').outerHeight());
			})/*.click(function(){
				window.location.hash = $(this).closest('article').attr('id').replace('_','');
			})*/;
			
			$('header li.small:eq(0)').css('padding-left','100px');
			
// 			$('div#viewport').scroll(function(){
// 				if ($(this).scrollTop() > 0) $('header').css('border-bottom','1px solid #e3e3e3');
// 				else $('header').css('border-bottom','none');
// 			});
			
			$('#Careers_ a.title').click(function(e){
				e.preventDefault();
				if ($(this).hasClass('Open')) {
					$(this).removeClass('Open').next('div.description').slideUp(300,function(){
						$('section').height($('#'+$('header a.On').attr('id')+'_').outerHeight());
						if ($('section').height() < $('div#viewport').height()) $('section').height($('div#viewport').height());
					});
				}
				else {
					$(this).addClass('Open').next('div.description').slideDown(600,function(){
						$('section').height($('#'+$('header a.On').attr('id')+'_').outerHeight());
						if ($('section').height() < $('div#viewport').height()) $('section').height($('div#viewport').height());
					});
				}
			});
			$('#Careers_ td a.close').click(function(e){
				e.preventDefault();
				$(this).closest('div.description').slideUp(150,function(){
					$('section').height($('#'+$('header a.On').attr('id')+'_').outerHeight());
					if ($('section').height() < $('div#viewport').height()) $('section').height($('div#viewport').height());
				}).prev('a.title').removeClass('Open');
			});
	
		}
		else {
		
			$('.description').show();
			$('.show').hide();
			$('#Careers_ td a.close').remove();
		
		}

		if (window.location.hash) { goToPage(window.location.hash); }
		else {
			$('nav a#Video').addClass('On');
	  		$('aside:eq(0)').hide();
		}
		
		var paper = Raphael("graph", 255, 700);
		var clientsPos = {};
		$('#clients div').each(function(){
			clientsPos[$(this).attr('id')] = {};
			clientsPos[$(this).attr('id')]['left'] = $(this).position().left + 10;
			clientsPos[$(this).attr('id')]['top'] = $(this).position().top + 6;
		});
		var servicesPos = {};
		$('#services div').each(function(){
			servicesPos[$(this).attr('id')] = {};
			servicesPos[$(this).attr('id')]['left'] = $(this).position().left - 95;
			servicesPos[$(this).attr('id')]['top'] = $(this).position().top + 20;
		});
		var clients = {};
		var services = {};
		$.each(servicesPos,function(sk,sv){
			services[sk] = {};
			var cclassList = $('#'+sk).attr('class').split(/\s+/);
			$.each(cclassList,function(cli,clv){
				services[sk][clv] = true;
			});
		});
		$.each(clientsPos,function(ck,cv){
			clients[ck] = {};
			var sclassList = $('#'+ck).attr('class').split(/\s+/);
			$.each(sclassList,function(sli,slv){
				clients[ck][slv] = true;
			});
		});
		var lines = {};
		var thid = '';
		var lineColor = '000000';
		$('#clients div')/*.click(function(){
			if ($(this).hasClass('On')) {
				$(this).removeClass('On');
				thid = $(this).attr('id');
				$.each(lines[thid],function(lk,lv){
					lines[thid][lk].remove();
				});
			}
			else {
				$(this).addClass('On');
			}
		})*/.hover(function(){
			if (!$(this).hasClass('On')) {
				thid = $(this).attr('id');
				lines[thid] = {};
				$.each(clients[thid],function(ck,cv){
					if ($('#services div').index($('#'+ck)) == 0 || $('#services div').index($('#'+ck)) == 4 || $('#services div').index($('#'+ck)) == 8) { lineColor = "e04738" }
					if ($('#services div').index($('#'+ck)) == 1 || $('#services div').index($('#'+ck)) == 5 || $('#services div').index($('#'+ck)) == 9) { lineColor = "007cc4" }
					if ($('#services div').index($('#'+ck)) == 2 || $('#services div').index($('#'+ck)) == 6) { lineColor = "70a842" }
					if ($('#services div').index($('#'+ck)) == 3 || $('#services div').index($('#'+ck)) == 7) { lineColor = "ffaa00" }
					lines[thid][ck] = paper.path('M'+(clientsPos[thid]['left'])+','+(clientsPos[thid]['top'])+' C'+(clientsPos[thid]['left']+120)+','+(clientsPos[thid]['top'])+' '+(servicesPos[ck]['left']-120)+','+(servicesPos[ck]['top'])+' '+(servicesPos[ck]['left'])+','+(servicesPos[ck]['top'])).attr({stroke: "#"+lineColor});
				});
			}
		},function(){
			if (!$(this).hasClass('On')) {
				$.each(lines[thid],function(lk,lv){
					lines[thid][lk].remove();
				});
			}
		});
		$('#services div')/*.click(function(){
			if ($(this).hasClass('On')) {
				$(this).removeClass('On');
				thid = $(this).attr('id');
				$.each(lines[thid],function(lk,lv){
					lines[thid][lk].remove();
				});
			}
			else {
				$(this).addClass('On');
			}
		})*/.hover(function(){
			if (!$(this).hasClass('On')) {
				thid = $(this).attr('id');
				if ($('#services div').index($(this)) == 0 || $('#services div').index($(this)) == 4 || $('#services div').index($(this)) == 8) { lineColor = "e04738" }
				if ($('#services div').index($(this)) == 1 || $('#services div').index($(this)) == 5 || $('#services div').index($(this)) == 9) { lineColor = "007cc4" }
				if ($('#services div').index($(this)) == 2 || $('#services div').index($(this)) == 6) { lineColor = "70a842" }
				if ($('#services div').index($(this)) == 3 || $('#services div').index($(this)) == 7) { lineColor = "ffaa00" }
				lines[thid] = {};
				$.each(services[thid],function(sk,sv){
					lines[thid][sk] = paper.path('M'+(servicesPos[thid]['left'])+','+(servicesPos[thid]['top'])+' C'+(servicesPos[thid]['left']-120)+','+(servicesPos[thid]['top'])+' '+(clientsPos[sk]['left']+120)+','+(clientsPos[sk]['top'])+' '+(clientsPos[sk]['left'])+','+(clientsPos[sk]['top'])).attr({stroke: "#"+lineColor});
				});
			}
		},function(){
			if (!$(this).hasClass('On')) {
				$.each(lines[thid],function(lk,lv){
					lines[thid][lk].remove();
				});
			}
		});
		
	});
	
	$(window).resize(function() {

		var $winW = $(window).width();
		var $winH = $(window).height();
		var $secW = $('section').width();
	
		if (!isiPhone()) {

			$('div#viewport').height( $winH - $('header').height() );
			if ( $winW > $('article').outerWidth() ) $('section').width( $secW + ( $winW - $('article').outerWidth() ) );		
	
			$('section').height($('#'+$('header a.On').attr('id')+'_').outerHeight());
			if ($('section').height() < $('div#viewport').height()) $('section').height($('div#viewport').height());

		}

	});
	

})(window.jQuery);
