// DEFINE SIFR FONTS
var sifr_font = {
  src: base_url+'assets/public/swf/helvroman.swf'
};
sIFR.useStyleCheck = true;
sIFR.activate(sifr_font);

var num_rotations = 0;
var timeout    = 300;
var closetimer = 0;
var current_menuitem = 0;

$(function(){
	activate_sifr();
	
	if (window.location.href == base_url) {
		var so = new SWFObject(base_url+'loading_flash','ontimeswf',"976","436",'9');
	    so.addParam('allowscriptaccess','always');
	    so.addParam('wmode','transparent');
	    so.write('homeswf');

	}
	$(".rollover").mouseover(function(){
		var on = $(this).attr('on');
		var off = $(this).attr('off');
		
		if (on && off && on != '' && off != '' && on != off) {
			$(this).attr('src',on);
		}
	});
	$(".rollover").mouseout(function(){
		var on = $(this).attr('on');
		var off = $(this).attr('off');
		
		if (on && off && on != '' && off != '' && on != off) {
			$(this).attr('src',off);
		}
	});
	
	$(".rotate").each(function(){
		var num_images = 0;
		$(this).find("img").each(function(){
			num_images +=1;
		
		});
		
		$(this).attr('num_images',num_images);
		$(this).attr('current_image','1');
		num_rotations ++;
	});
	
	$(".navigation").hover(function(){
		var page_id = $(this).attr('page_id');
		menu_canceltimer();

		if (page_id != current_menuitem) {
			menu_close();
    		current_menuitem = page_id;
			
			var offset = $(this).offset();
			$('#subnav').hide();
			
			if (typeof sn[page_id] != 'undefined') {
			
				$('#subnav').css('left',offset.left+'px');
				$('#subnav').css('top',(offset.top+27)+'px');
				
				$('#subnav ul').html('');
				for (i=0;i<sn[page_id].length;i+=2) {
					var target = '';
					if (!stristr(sn[page_id][i],base_url)) {
						target = ' target="_blank" ';
					}
					
					$('#subnav ul').append("<li><a href=\""+sn[page_id][i]+"\""+target+">"+sn[page_id][i+1]+"</a></li>");
				}
				$('#subnav').attr('page_id',page_id);
				$('#subnav').show();
			}
		}
	
	},
    function() {
	    menu_settimer();    	
    });
	
	
	if (num_rotations > 0) {
		setInterval(function(){
			$(".rotate").each(function(){
				var num_images = parseInt($(this).attr('num_images'));
				
				if (num_images > 1) {
					var current_image = parseInt($(this).attr('current_image'));
					$(this).css('background-image','url('+$(this).find("img[n='"+current_image+"']").attr('src')+')');
					
					current_image += 1;
					
					if (current_image > num_images) current_image = 1;
					
					$(this).find('img').hide();

					$(this).find("img[n='"+current_image+"']").fadeIn('medium');
					
					$(this).attr('current_image',current_image);
				}
			});
		},2500);
		
	}
	
	setup_slideshow();
	$("#rotate-left,#rotate-right").click(function(){
		window.setTimeout(function(){ setup_slideshow(); },900);
	});
	
	
	
	$('.staff_member img').click(function(){
		$.get(base_url+'firm/team/staff/'+$(this).attr('staff_id'),function(xml){
			
			var record_name = $('#record-name').text();
			var record_content = $('#record-content').text();
			
			$('#record-name').html($(xml).find('name').text());
			$('#record-content').html($(xml).find('content').text());
			
			$('*').removeClass('sIFR-replaced');
			activate_sifr();
			    
			$('#record-image1').find('img').attr('src',base_url+'assets/public/images/staff/'+$(xml).find('image1').text());
			$('#record-image2').find('img').attr('src',base_url+'assets/public/images/staff/'+$(xml).find('image2').text());
			$('#record-image3').find('img').attr('src',base_url+'assets/public/images/staff/'+$(xml).find('image3').text());
			
			$("#staff-portrait-bg").show();
			$("#staff-portrait-bg").css('filter','alpha(opacity=70)');
			$("#staff-portrait-bg").css('opacity','0.7');
			$("#staff-portrait").show();
			
			$("#portrait-close").click(function(){
				$("#staff-portrait").hide();
				$("#staff-portrait-bg").hide();
				$('#record-name').html(record_name);
				$('#record-content').html(record_content);
				$('*').removeClass('sIFR-replaced');
				activate_sifr();
			});

			
		});
		
	
	});
	$('.key_staff_member img').click(function(){
		$.get(base_url+'firm/team/key-staff/'+$(this).attr('staff_id'),function(xml){
			
			var record_name = $('#record-name').text();
			var record_content = $('#record-content').text();
			
			$('#record-name').html($(xml).find('name').text());
			$('#record-content').html($(xml).find('content').text());
			$('*').removeClass('sIFR-replaced');
			activate_sifr();
			    
			$('#record-image').attr('src',base_url+'assets/public/images/key_staff/'+$(xml).find('image').text());
		});
		
	
	});
	
	$("input[name='requestinfo']").click(function(){

		var postData = $("form[name='contactform']").serialize();
		$.ajax({ url: base_url+'contact/submit', data:postData,type:'POST', success: function(){
			$("form[name='contactform']").hide();
        	$("#thankyou").show();
      	}});
		
	
	});

});

function activate_sifr() {
	sIFR.replace(sifr_font, {
      selector: '.contentwrapper h1,.contentwrapper h2,.contentwrapper h3,.casestudy-title,h1.grey,h2.grey',
      wmode:'transparent',
      css: {
        '.sIFR-root': { 'color': '#454545' }
      },
      ratios:[9, 1.16, 17, 1.09, 18, 1.08, 24, 1.05, 25, 1.06, 30, 1.04, 32, 1.05, 37, 1.03, 39, 1.04, 40, 1.02, 41, 1.04, 58, 1.03, 97, 1.02, 1.01],
      offsetTop:0,
      tuneHeight:-5
    });
	sIFR.replace(sifr_font, {
      selector: '.blogger-content h1',
      wmode:'transparent',
      css: {
        '.sIFR-root': { 'color': '#4A4A4A' }
        
      },
      ratios:[9, 1.16, 17, 1.09, 18, 1.08, 24, 1.05, 25, 1.06, 30, 1.04, 32, 1.05, 37, 1.03, 39, 1.04, 40, 1.02, 41, 1.04, 58, 1.03, 97, 1.02, 1.01],
      offsetTop:0,
      tuneHeight:-5
    });
	sIFR.replace(sifr_font, {
      selector: '.col-left h2,h1.red',
      wmode:'transparent',
      css: {
        '.sIFR-root': { 'color': '#AA0000' }
      },
      ratios:[9, 1.16, 17, 1.09, 18, 1.08, 24, 1.05, 25, 1.06, 30, 1.04, 32, 1.05, 37, 1.03, 39, 1.04, 40, 1.02, 41, 1.04, 58, 1.03, 97, 1.02, 1.01],
      offsetTop:0,
      tuneHeight:-5
    });
    sIFR.replace(sifr_font, {
      selector: '.hero-content h1,.hero-content h2,.hero-content h3,.talktown h1',
      wmode:'transparent',
      css: {
        '.sIFR-root': { 'color': '#FFFFFF' }
      },
      ratios:[9, 1.16, 17, 1.09, 18, 1.08, 24, 1.05, 25, 1.06, 30, 1.04, 32, 1.05, 37, 1.03, 39, 1.04, 40, 1.02, 41, 1.04, 58, 1.03, 97, 1.02, 1.01],
      offsetTop:0,
      tuneHeight:-5
    });

	
	
}
function our_town_video_open() {
	$("#home-video-bg").show();
	$("#ourtownvideo").css('left',(screen.width/2-300)+'px');
	$("#ourtownvideo").css('top',((screen.height+$(window).scrollTop())/2 - 225)+'px');
	$("#ourtownvideo").show();
	var so2 = new SWFObject('http://vimeo.com/moogaloop.swf?clip_id=12289939&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1','ourtownswf',"600","450",'9');
    so2.addParam('allowscriptaccess','always');
    so2.addParam('wmode','transparent');
    so2.write('ourtownswf_div');
}
function our_town_video_close() {
	$("#ourtownvideo").hide();
	$("#home-video-bg").hide();
	window.location = window.location;
}
function menu_close() {
	if (current_menuitem != 0) {
		$('#subnav').hide();
	}
	current_menuitem=0;
}

function menu_settimer() {
	closetimer = window.setTimeout(menu_close, timeout);
}

function menu_canceltimer()
{  
   if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;
   }
}
function setup_slideshow() {
	$("#viewer-frame").find('img').unbind('click');
	$("#viewer-frame").find('img').click(function(){
		$(".project-hero").css('background-image',"url('"+$("#project-image").attr('src')+"')");
		$("#project-image").hide();
		$("#project-image").attr('src',$(this).attr('full'));
		$("#project-image").fadeIn('slow');
	});
	
}
function stristr (haystack, needle, bool) {
    // Finds first occurrence of a string within another, case insensitive  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/stristr    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfxied by: Onno Marsman

    var pos = 0;
 
    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );    
    if (pos == -1){
        return false;
    } else{
        if (bool) {
            return haystack.substr( 0, pos );        } else{
            return haystack.slice( pos );
        }
    }
}