// NORMAL RULES

$(document).ready(function() {
	
	function sticky_modul_window (initiate, resize, open_close) {
		if (initiate == true){	
			modul_str = '<div id="modul_window">&nbsp;</div><div id="modul_window_content_container"><div id="modul_window_content"></div></div>';
			$("#container_2").prepend(modul_str);
			$("#container_2").addClass("sticky_modul_window");
		}		
		if (resize == true){
			modul_height = $("#container_4").height()+"px";
			modul_width = $(window).width()+"px";
			$("#modul_window").css({height: modul_height, width:modul_width	});
		}
		if (open_close == "open"){
			$("#modul_window").fadeTo(600, 1);
			$("body").addClass("sticky_modul_window_open");
			$("body").removeClass("sticky_modul_window_close");
		}
		if (open_close == "close"){
			$("#modul_window").fadeOut(600);
			$("#modul_window_content").fadeOut(400, function(){
				$(this).empty();
				$(this).fadeIn(1);
			});
			$("body").addClass("sticky_modul_window_close");
			$("body").removeClass("sticky_modul_window_open");
		}
		
	}

	sticky_modul_window(true, true, false);

	$("#modul_window, #back_to_site, #close_staff_list_btn").live('click', function (){
		$("#header h1").fadeIn(300);
		sticky_modul_window(false, false, "close");
		return false
	//	DD_roundies.addRule('.staff_photo', 4, true);
	//	DD_roundies.addRule('.staff_profile_container', 4, true);

	});

	$(window).resize(function() {
		sticky_modul_window(false, true, false);
	});
	
	$('#modul_window_content .ae_preview li a ').live('mouseover mouseout', function(event) {
	  if (event.type == 'mouseover') {
		  $(this).find('h3').stop().animate({height:38}, 150);
	  } else {
		  $(this).find('h3').stop().animate({height:0}, 150);
	  }
	});
	
	$('#modul_window_content .staff_photo').live('click', function() {
		$('body').removeClass("staff_profile_live");											 
		$(".staff_profile_container").hide(300);
		$(this).next(".staff_profile_container").show(300);
		$('body').addClass("staff_profile_live");											 
		return false;
	});
	
	$('#modul_window_content .staff_profile_container_close').live('click', function() {
		$('body').removeClass("staff_profile_live");											 
		$(".staff_profile_container").hide(300);
		return false;
	});
	
	
	$(document).keyup(function(e) {
	  if (e.keyCode == 27) { 
		if ($('body').hasClass("staff_profile_live") && $('body').hasClass("sticky_modul_window_open")){
			$(".staff_profile_container").hide(300); 
			$('body').removeClass("staff_profile_live");											 
		}
		if ($('body').hasClass("sticky_modul_window_open")){
		if ($('body').hasClass("staff_profile_live")){}else{
			$("#header h1").fadeIn(300);
			sticky_modul_window(false, false, "close");
		}
		}
	
	  }
	});
	
	// USER DEIFINED MODULE WINDOW RULES
	
	$(".who_we_are_btn, #who_we_are_link").live('click',function(){
		$('html, body').animate({scrollTop:0}, 'slow');		
		if($("body").hasClass("sticky_modul_window_open")){
			$("#header h1").fadeIn(300);
			sticky_modul_window(false, false, "close");
		}
		else {
			sticky_modul_window(false, true, "open");
			$("#modul_window_content").load('/includes/staff_list.php?id=6', function (){
				$("#header h1").fadeOut(300);
				// DD_roundies.addRule('.ae_preview li a', 4, false);
				$("#staff_container").css({height: modul_height});
				$("#staff_container").find(".who_we_are_btn").addClass("active");
			});
		}
		return false;
	});
	
	if($("#container_4").hasClass("home")){
		$("#container_3").prepend('<div id="eye_candy">&nbsp;</div>');
		$("#explore_btn").click(function(){
			$("#home_link").addClass("active");
			$(this).fadeOut(600, function (){
				$("#eye_candy").fadeIn(500, function(){
					$("#navigation_container").animate({height:427},1400);
				});
			});				
			return false;
		});
		
		$("#home_link").click(function (e){
			if($(this).hasClass("active")){
				$("#navigation_container").animate({height:0},600, function (){
					$("#eye_candy").fadeOut(500, function(){
						$("#explore_btn").fadeIn(600);
					});
				});
			e.preventDefault();
			}
		});
		nav_counter = -1;
		$("#navigation li a").each(function(){
			nav_counter = nav_counter + 1;
			top_val = (nav_counter * 39) + 37;
			$(this).data('top_val', top_val);
		});
		
		$("#navigation").hover(function(){},function(){
			$("#back_to_top").stop().animate({height: 0}, 100);
		});
		
		$("#navigation li a").hoverIntent(
			function(){
				this_top_value = $(this).data('top_val');
				$("#back_to_top").stop().animate({height: 44}, 10).animate({top:this_top_value},700);
				$(this).find("span").css({"backgroundPosition":"right -36px"});
			},
			function(){
				$(this).find("span").css({"background-position":"right 0px"});
			}
		);	
		
		$("#explore_btn strong").animate({'top':'45px'}, 2750, "easeOutElastic");
		
		$("#explore_btn").hover(								
			function(){
				$("#explore_btn strong").stop().animate({'top':'30px'}, 200).animate({'top':'45px'}, 1000, "easeOutElastic");
			}, 
			function(){
				$("#explore_btn strong").stop().animate({'top':'45px'}, 500);
			}
		);
	}
	else{
		// IMAGE TAG REPLACEMENT FOR ROUDNING CORNERS
		
		function img_p_replace (selector, width_override, height_override){
			selector_name = '.'+selector;
			$(selector_name).each(function() {
				if(width_override > 1){
					item_width =  width_override;
				}
				else {
					item_width = $(this).width(); 
				}
				if(height_override > 1){
					item_height = height_override;
				}
				else {
					item_height = $(this).height(); 
				}
				item_src= $(this).attr('src');
				background_image_str = 'url("'+item_src+'") top center';
				item_class= selector+'_container'; 
				$(this).wrap('<p/>');
				
				
				if ($(this).hasClass('staff_photo_container')) {
					img_h = $(this).find('img.staff_photo').height();
				}
				else if ($(this).hasClass('staff_photo_thumbs_container')) {
					img_h = $(this).find('img.staff_photo_thumbs').height();
				}
				else {
					img_h = item_height;
				}
				
				$(this).parent().css({width:item_width, height:img_h, background: background_image_str, overflow:"hidden"}).addClass(item_class);
				$(this).hide();
			});
		}
		
		
  		img_p_replace("staff_photo_thumbs", 95, 115);
  		img_p_replace("staff_photo", 164, 220);
  		img_p_replace("staff_group_photo", 604, 243);
		
		// ODD EVEN 
		
		$(".testimonial:odd").addClass("odd");
		$(".testimonial:even").addClass("even");
		
		// TESTIMONIAL CYCLE
		
		$('#testimonial_showcase_container').cycle({ 
			timeout: 6000,
			speed: 1000
		});
		
		
		
		
	}
	

});
