if (top.location != self.location) {
    top.location = self.location.href
}

/// DEFERRED VARS

var staff_click_start;


// FOR PRINTING AJAX ERRORS

$(document).ajaxError(function(e, xhr, settings, exception) {
//    console.log('error in: ' + settings.url + ' \n'+'error:\n' + exception );
});

// GET HASH VARS FUNCTIONS

function getHashVars(hashBang) {
    var vars = [], hash;
    var hashes = hashBang.slice(hashBang.indexOf('!') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

// GET CENTER POSITON FOR BACKGROND POSITON ANIMATION 

function resizeWindow(e) {
    var body_width = $('body').width()
    center_pos = body_width - 3600;
    center_pos = center_pos / 2;
	
	if ($('#container_3').hasClass('fixed_bg')){
		window_height = $(window).height();
	    orig_x_swoop_val = -2800;	
		swoop_x_val = orig_x_swoop_val + window_height - 450;
		bg_pos = Math.round(center_pos)+'px ' + swoop_x_val+'px';
		$("#container_2, #container_3").css({backgroundPosition: bg_pos});

		/// INTERACTIVE AD LEFT SET
		
		ask_junior_offset = $("#ask_junior").offset();
		ask_junior_offset_left = ask_junior_offset.left;
//		console.log(ask_junior_offset);
//		console.log(ask_junior_offset_left);
		$.data(document.body, 'ask_junior_offset', ask_junior_offset);
		$.data(document.body, 'ask_junior_offset_left', ask_junior_offset_left);
		$("#ask_junior").css({left:ask_junior_offset_left});
		
	}
}

function show_staff_profile (staff_id){
	
    hash_bang = getHashVars($.param.fragment());

	$("#staff_next, #staff_prev").fadeOut(300);
	load_page = 'refresh/templates/entry.php?id='+ staff_id + '&page=staff_member';
	$('#staff_page').load(load_page, function (){
		staff_height =  $(this).height();
		staff_height = staff_height - 166;
		staff_height = staff_height + 800;
		$("a.fancy_regular").fancybox({ 
			'padding':			1,
			'type':  		'iframe',								
			'width': 		900,
			'height': 		550,
			'speedIn': 		100,
			'speedOut': 	100,
			'transitionIn': 'elastic',
			'transitionOut': 'elastic',
			'easingIn': 	'easeInSine',
			'easingOut': 	'easeOutSine',
			'titleShow' : false
		}); 
		$('html,body').animate({scrollTop: 590}, 1000);
		$("#container").animate({height : staff_height}, 1000);
		$("#close_staff_member_page").click( function(){
			if (hash_bang['staff_id']!= undefined && hash_bang['staff_id'] != ""){
				$('html,body').animate({scrollTop: 0}, 1000);
				window.location = '#!interface=blog';
			}
			else {
				$("#staff_page").empty();
				$("#container").animate({height : 800}, 1000, function (){
					$("#staff_container").animate({'height':'150px'}, 900);
					$("#staff_next, #staff_prev").fadeIn(300);
					rotating = setInterval(function() {
						slider("test");
					},5000);
					$('html,body').animate({scrollTop: 600}, 1000);
	
				});
			}
			return false;
		});
	});
	
}
//  STATEMENT RULES

function statement_slide(speed) {
    
    $(".statement_recruitment").animate({width: "187px"}, speed, function() {
        $(".statement_impacting_buisness").animate({width: "156px"}, speed, function() {
            $(".statement_enchancing_career").animate({width: "150px"}, speed, function() {
                $(".statement_changing_lives").animate({width: "161px"}, speed);
            });
        });
    });
}


// HASH EVENTS

function hashEvents() {

    hash_bang = getHashVars($.param.fragment());

    // LOC VARIABLE DEFINITIONS
    
    interface = hash_bang['interface'];
    id = hash_bang['id'];
    staff_id = hash_bang['staff_id'];
	blog_id = hash_bang['blog_id'];
    type = hash_bang['type'];
    blog_category = hash_bang['blog_category'];
	autoslider = false;
	
	window_height = $(window).height();

	// NORMAL RULES
    
    if ($("#container_3").hasClass("home")) {
        
        $("#navigation li a").removeClass("current_page");
        $(this).addClass("current_page");     
        $("#body_text").hide('fast', function() {
            $(this).css("display", "block");
            $("#body_text").empty();
			var load_args = "";
			if (interface != undefined){ load_args = load_args + 'page=' + interface }
			if (id != undefined)       { load_args = load_args + '&id=' + id }
			if (blog_id != undefined){ load_args = load_args + '&blog_id=' + blog_id }
			if (type != undefined){ load_args = load_args + '&type=' + type }
			if (blog_category != undefined){ load_args = load_args + '&blog_category=' + blog_category }
            load_page = 'refresh/templates/entry.php?' + load_args;
            $('#body_text').load(load_page, function() {
			   
                if (interface == "staff") {

/* DEFERED EXAMPLE 
	var hash_events_start = $.Deferred();
	staff_click_start.resolve();
	$.when(staff_click_start).done(function(){});
*/
	               	staff_click_start = $.Deferred();
					if (staff_id != undefined && staff_id != ""){
					}

					$("#body_text").css("visibility", "hidden");
                    $("#home_page_statement").animate({width: '0'}, 600, function() {
                        $("#container").animate({height: 800}, 1500);
                        $("#header h1").animate({top: staff_header_h1_top}, 1000, "easeOutSine");
						countainer_callback_counter = 0;
                        $("#container_2, #container_3").animate({backgroundPosition: staff_center_pos_str}, 1000, "easeOutSine", function() {
                            $("#body_text").show('slow', function () {
									$("#body_text").delay(1000).css({visibility: "visible"});									   
							});
                            $('html,body').animate({scrollTop: 270}, 1000);

                            
							if (countainer_callback_counter == 0){
	  						// CALL STAFF SLIDER STUFF
                              $.getScript('/scripts/staff_slider.js');
							}
							
							$.when(staff_click_start).done(function(){
								//alert('test');
								if (staff_id != undefined && staff_id != ""){
									target_href = '/refresh/templates/staff_profile.php?staff_id='+staff_id; 
									target_selector = 'a[href="'+target_href+'"]';
									//$(target_selector).trigger('click');
							
									$("#staff_container").animate({'height':'0px'}, 900);
									show_staff_profile(staff_id);		
									
								}
							});
							countainer_callback_counter = countainer_callback_counter + 1;

                        });
                    });
                
                } 
                if (interface == "ask_junior") {
                    
					$("#body_text").css("visibility", "hidden");
                    $("#home_page_statement").animate({width: '0'}, 600, function() {
                        $("#container").animate({height: 850}, 1500);
                        $("#header h1").animate({top: staff_header_h1_top}, 1000, "easeOutSine");
						countainer_callback_counter = 0;
                        $("#container_2, #container_3").animate({backgroundPosition: staff_center_pos_str}, 1000, "easeOutSine", function() {
                            $("#body_text").show('slow', function () {
									$("#body_text").delay(1000).css({visibility: "visible"});									   
							});
                            $('html,body').animate({scrollTop: 270}, 1000);

							$('#submit_question').unbind('click').click(function(){
								form_data = $(this).parent('form').serialize();
								$.ajax({
								  type: 'POST',
								  url: '/forms/question_form_code.php',
								  data: form_data,
								  success: function(msg){
									  if ($.trim(msg) == "") {
										  $("#ask_junior_form p, #ask_junior_form a").fadeOut(300);
										  $('#form_submited_info').fadeIn(300);

									  }
									  else {
										  $('#form_error').html(msg).show();
									  }
								  }
								});
								return false;
							});
							
							$('#ask_junior_form input').keydown(function(e){
								if (e.keyCode == 13) {
									$('a#submit_question').trigger('click');
									return false;
								}
							});	
							
                        });
                    });
                
                } 
                else if (interface == "clients") {
                    
                    // CLICK SPACIFIC VARIABLES
                    body_text_height = ($("#body_text").outerHeight());
                    body_text_height = body_text_height - 43;
                    swoop_x_val = orig_x_swoop_val + body_text_height
                    animated_center_pos_str = center_pos + 'px ' + swoop_x_val + 'px';
                    container_height = orig_container_height + body_text_height;
                    header_h1_top_val = header_h1_top + body_text_height;
					$("#body_text").css("visibility", "hidden");
                    $("#home_page_statement").animate({width: '0'}, 600, function() {
                        $("#header h1").animate({top: header_h1_top_val}, 1000, "easeOutSine");
						countainer_callback_counter = 0;
                        $("#container_2, #container_3").animate({backgroundPosition: animated_center_pos_str}, 1000, "easeOutSine", function() {
							if (countainer_callback_counter == 0){
								$("#body_text").show('slow', function() {
									$("#body_text").delay(1000).css({visibility: "visible"});
								});
							}
							countainer_callback_counter = countainer_callback_counter + 1;
							
                        });
                    });
                    $("#container").animate({height: orig_container_height}, 1000);
                    // CALL CLIENT SLIDER 
                    $.getScript('/scripts/client_slider.js');
                
                
                } 
                else if (interface == "about" || interface == "jobs" || interface == "contact" ||  interface == "blog" ||  interface == "blog_archive" || interface == "blog_entry" ){
//                    console.log('about');
                    // CLICK SPACIFIC VARIABLES
                    body_text_height = ($("#body_text").outerHeight());
                    body_text_height = body_text_height - 43;
                    swoop_x_val = orig_x_swoop_val + body_text_height
					if ( interface == "blog" || interface == "blog_entry" ||  interface == "blog_archive" ){
	                    swoop_x_val = orig_x_swoop_val + window_height - 450;
					}
                    animated_center_pos_str = center_pos + 'px ' + swoop_x_val + 'px';
                    container_height = orig_container_height + body_text_height;
                    header_h1_top_val = header_h1_top + body_text_height;
					$("#body_text").css("visibility", "hidden");
                    $("#home_page_statement").animate({width: '0'}, 600, function() {
                        $("#header h1").animate({top: header_h1_top_val}, 1000, "easeOutSine");
						countainer_callback_counter = 0;
                        $("#container_2, #container_3").animate({backgroundPosition: animated_center_pos_str}, 1000, "easeOutSine", function() {
							if (countainer_callback_counter == 0){
								$("#body_text").show('slow', function() {
									$("#body_text").delay(1000).css({visibility: "visible"});
									$('div.youtube_embed_container').delay(3000).css({visibility: "visible"});																				  
									$("a.fancy_regular").fancybox({
										'padding': 1,
										'type': 'iframe',
										'width': 900,
										'height': 550,
										'speedIn': 100,
										'speedOut': 100,
										'transitionIn': 'elastic',
										'transitionOut': 'elastic',
										'easingIn': 'easeInSine',
										'easingOut': 'easeOutSine',
										'titleShow': false
									});
									if (IN.parse) {
									IN.parse(document.body);
									}
								});
							}
	
						});
                    });
                    if (interface == "contact") {
                        $("#container").animate({height: orig_container_height}, 1000);
                    } 
                    else {
                        $("#container").animate({height: container_height}, 1000);
                    }
                }
				
				if ( interface == "blog" || interface == "blog_entry" ||  interface == "blog_archive"){
					$('#container_3').addClass('fixed_bg');
					$('#footer_container').addClass('footer_fixed');
					ask_junior_offset = $("#ask_junior").offset();
					ask_junior_offset_left = ask_junior_offset.left;
					$.data(document.body, 'ask_junior_offset', ask_junior_offset);
					$.data(document.body, 'ask_junior_offset_left', ask_junior_offset_left);
					$("#ask_junior").css({left:ask_junior_offset_left});
				}
				else{
					$('#container_3').removeClass('fixed_bg');
					$('#footer_container').removeClass('footer_fixed');
				}
				
            
            });
        });
    }
}

// INITIAL PAGE LOAD FUNCTIONS
$(document).ready(resizeWindow);
$(window).bind("resize", resizeWindow);
$(document).ready(function() {
    
    if (typeof console === "undefined") {
        console = {log: function() {
            }};
    }
    
    $("#navigation li a span").fadeOut(0);
    
    $("#navigation li a").hover(
    function() {
        $(this).find("span").fadeIn(400);
    }, 
    function() {
        $(this).find("span").fadeOut(400);
    }
    );
    
    intial_load_over = false;
    top_center_pos_str = center_pos + "px  0px";
    orig_x_swoop_val = -2800;
    staff_header_h1_top = 111;
    header_h1_top = 161;
    staff_center_pos_str = Math.round(center_pos) + 'px -2850px';
    orig_animated_center_pos_str = Math.round(center_pos) + 'px -2800px';
    animated_center_pos_str = Math.round(center_pos) + 'px ' + orig_x_swoop_val + 'px';
    staff_center_pos_str = Math.round(center_pos) + 'px ' + '-2850px';
    orig_container_height = $("#container_3").outerHeight();
    modified_container_height = 1200;
    sliding = false;
    autoslider = null;
    ($(document.body).data('sliding', false));
	
	
    $("#body_text").css("display", "block");
    $("#body_text").css("visibility", "hidden");
    $("#container_2, #container_3").css({'background-position': top_center_pos_str})
    $("#header, #navigation").show('slow');
    $("#container_2, #container_3").animate({backgroundPosition: orig_animated_center_pos_str}, 1000, "easeOutSine", function() {
        intial_load_over = true;
        statement_slide(900);
    });
    
    $("#header a").click(function() {
        $("#body_text").hide('fast', function() {
            $(this).css("display", "block");
            $(this).empty();
			//$('a#blog_link').fadeIn(500);		
            $("#navigation li a").removeClass("current_page");
            $("#container").animate({height: orig_container_height}, 1500);
            $("#header h1").animate({top: header_h1_top}, 1500, "easeOutSine");
            $("#container_2, #container_3").animate({backgroundPosition: orig_animated_center_pos_str}, 1500, "easeOutSine", function() {
                $("#home_page_statement").animate({width: 654}, 600);
            });
        });
        return false;
    });
    
	
    hash = location.hash;
    hashEvents(hash);

    // HASH CHANGE CALL 
    
});
$(window).hashchange(function(e) {
	hash = location.hash;
	hashEvents(hash);		
});

$(window).load(function(){
	//$("#hidden_welcome_trigger").trigger('click');					
});



$(window).scroll(function(){
						  
	if ( $.data(document.body, 'ask_junior_offset')){		
		min_scroll_offset  = $.data(document.body, 'ask_junior_offset');
		min_scroll_offset  = min_scroll_offset.top;
		ask_junior_offset_left  = $.data(document.body, 'ask_junior_offset_left') - 29; 
//		console.log(min_scroll_offset);
//		console.log(ask_junior_offset_left);
			
		footer_offset = $("#footer").offset();
		max_scroll_offset = footer_offset.top - $("#ask_junior").height() - 20;
		current_scroll = $(window).scrollTop();	
		ask_junior_abs_pos = max_scroll_offset - 387;
		if(current_scroll > min_scroll_offset) {
			if (current_scroll > max_scroll_offset ){
				scrollstate = "abs"
				$("#ask_junior").removeAttr('style');
			}
			else {
				scrollstate = "fixed"
				$("#ask_junior").css({position:'fixed', top:'0', left:ask_junior_offset_left});
			}
		}
		else {
			$("#ask_junior").css({position:'static', 'top':'auto'});
			$("#ask_junior").removeAttr('style');
		}
	}
	
});
