$(function(){
	
	//buildSlideshowNav();
	//setTimeout('featured()', 3000);
	
	$('.backlink a').click(function(e){
		if (history.length) {
			e.preventDefault();
	    	history.go(-1);
		}
	})
	
	// generic element block click
	
	$('.hoverme').hover(
		function(){
			$(this).css({'cursor':'pointer'});
		},
		function(){
		
		}
	);
	
	$('#showcase_promos div').click(function(){
		var get_url = $(this).find('a').attr('href');
		window.location = get_url;
	});
	
	 $('input.srch').ToggleInputValue();
	
	if(!$('#site_search').hasClass('default_open')) {

		$("#search_close").css({'cursor':'pointer'});
		$("#nav_search, #search_close").bind("click", function(e){
		
			if($('#nav_search a').hasClass('active')){
				$('#nav_search a').removeClass('active');
			} else {
				$('#nav_search a').addClass('active');
			}
		
			$("#site_search").slideToggle('normal', function() {
			  
			});
			return false;
		});
		$("#site_search div.hiddenFields input").unwrap("div.hiddenFields");
	
	} else {
	
		$('#nav_search a').addClass('active');
		$("#nav_search").click(function(e){ e.preventDefault() });
		$("#search_close").hide();	
	
	}
		
	
	// check for page slideshow
	
	if($('.slideshow img').length > 1) {
		setup_pageSlideshow();
	}
	$('ul li:empty').remove();
	/* people tooltip */
	
	$("body").append("<div id='tooltip'></div>");
	
	$(".team_profile").mouseenter(function() {
		var tooltip_txt = $(this).find('img').attr('alt');
		var tooltip_ypos = 14;
		$(this).append('<div class="tooltip"><span>' + tooltip_txt + '</span></div>');
		$('.tooltip').css({ 'opacity': 0 });
		
		tooltip_ypos = tooltip_ypos - $(this).children('.tooltip').outerHeight();
		$(this).addClass('hover');
		$(this).find('img').stop(true,false).animate({ 'opacity': 0.5 }, 300);
		$(this).find('.tooltip').stop(true,false).animate({ 'opacity': 1.0, 'top': tooltip_ypos + 'px' }, 300);
	}).mouseleave(function() {
		$(this).find('img').animate({ 'opacity' : 1.0 }, 300);
		$(this).find('.tooltip').animate({ 'opacity': 0, 'top': '10px' }, 300);
	});
	
	/*********************************
	
	Section Landing
	
	*********************************/
	if($('body').hasClass('section_landing')){
	
		//$('.pg_content').append('<div id="section_visual"></div>');
		//$('#section_visual').smartBackgroundImage('http://logicpd.com/development/assets/img/sectionbkg.jpg');
		
		$('#section_navblocks li').mouseenter(function() {		
			$(this).css({ 'cursor': 'pointer' });
			$(this).append('<div class="overlay"></div>');
			$(this).children('.overlay').stop(true,false).fadeIn('normal');
			$(this).addClass('hover');
		}).mouseleave(function() {
			$(this).children('.overlay').fadeOut('fast', function(){
				$(this).parent('li').removeClass('hover');
			});
		});	
		
		$('#section_navblocks li').click(function(){
			var navlink = $(this).find('a').attr('href');			
			window.location = navlink;
		});

	}
	
	$('#gridview div.block').click(function(){
		var navlink = $(this).find('a').attr('href');			
		window.location = navlink;
	})
	
	/**********************************
		Stories Grid Rollover
	**********************************/
	
	$('#gridview .gridblock').mouseenter(function() {		
		$(this).css({ 'cursor': 'pointer' });
		$(this).append('<div class="overlay"></div>');
		$(this).find('img').animate({ 'margin-top' : '-10px' }, 300);
		$(this).children('.overlay').stop(true,false).fadeIn('normal');
		$(this).addClass('hover');
	}).mouseleave(function() {
		$(this).find('img').animate({ 'margin-top' : '0' }, 300);
		$(this).children('.overlay').fadeOut('fast', function(){
			$(this).parent('div').removeClass('hover');
		});
	});	
	
	$('#section_navblocks li').click(function(){
		var navlink = $(this).find('a').attr('href');			
		window.location = navlink;
	})
	
	$('#section_main .imageblock').css({ 'opacity' : 0 })
	
	/**********************************
		Partners Grid Rollover
	**********************************/

	$('#partnergrid .gridblock').mouseenter(function() {		
		$(this).css({ 'cursor': 'pointer' });
		$(this).append('<div class="overlay"></div>');
		$(this).children('.overlay').stop(true,false).fadeIn('normal');
	}).mouseleave(function() {
		$(this).children('.overlay').fadeOut('normal');
	});		
	
	/* ---------------------------------------
		Simple Toggle Dropmenu
	--------------------------------------- */
	
	$('#togglelist').hide();
	$('#togglenav, #pg_slideshow img').mouseenter(function(){
		$(this).css({ 'cursor': 'pointer' });
	}).mouseleave(function() {
	
	
	});
   $('#togglenav').click(function(e){
   	$(this).toggleClass('expand');
   	$("#togglelist").slideToggle();
   });

   if($("#contactform").length != 0){
	   $("#contactform").validate({
			rules: {
			
				first_name: "required",
				last_name: "required",
				company: "required",
				street: "required",
				city: "required",
				state: "required",
				country: "required",
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				required: "This field is required.",
				first_name: "^ Please enter your firstname",
				last_name: "^ Please enter your lastname",
				email: "^ Please enter a valid email address",
				company: "^ Please enter company name",
				street: "^ Please enter street address",
				city: "^ Please enter city",
				state: "^ Please enter state",
				country: "^ Please enter country",
				industry: "^ Please select an industry"
			}
		});
	}
});

var generic_slide_curr = 0;
var generic_total_slides;


function setup_pageSlideshow() {
	
	var separate_shows = [];
	var slide_chunks = $(".slideshow");
	
	$.each(slide_chunks, function(index, value) { 
		separate_shows.push($(this).find('img'));
	});
	
	var slideshow_h = slide_chunks.eq(0).find('img:eq(0)').outerHeight();
	var slideshow_w = slide_chunks.eq(0).outerWidth();
	
	$.each(separate_shows, function(index, parent_slideshow){
		$("#process_slides div.slideshow_nav").remove();
		slide_chunks.eq(index).css({
			'height'		: slideshow_h + 'px',
			'width'		: slideshow_w + 'px'
			})
		
		if (slide_chunks.eq(index).find('img').length > 1){ // more than 1 image to constitute a show
			slide_chunks.eq(index).append('<div id="pg_slideshow_nav"><div class="navwrap"></div></div>');
			var image_width = slide_chunks.eq(index).find('img:eq(0)').width();
			var slidshow_nav = slide_chunks.eq(index).find('#pg_slideshow_nav');
			$.each(parent_slideshow, function(child_index, child_slideshow){
				slidshow_nav.append('<span href="'+index+'" rel="'+child_index+'">'+(child_index+1) + '</span>');
			}).css({
				'position' 	: 'absolute',
				'top'			: 0,
				'left'		: 0
				}).hide().filter(':first').show();
				
			var span_nav = slidshow_nav.find("span");
			var span_nav_xpos = (image_width/2) - ((span_nav.length*12)/2);
		
			slidshow_nav.css({
				'top'	: (slideshow_h-35) + 'px',
				'width'	: image_width,
				'overflow' : 'hidden'
			});

			//console.log(slidshow_nav);
			//var span_nav = slidshow_nav.find("span");
			span_nav.css({ 'cursor': 'pointer' });
			span_nav.eq(0).addClass('curr').css({ 'margin-left' : span_nav_xpos + 'px' });
			slide_chunks.eq(index).find('img').bind('click', function(e){
				//console.log($(this).index());
				var slide_num = $(this).index()+1;
				if (slide_num >= $(this).parent('div.slideshow').data('total_slides')){
					slide_num = 0;
				}
				$(this).parent('div.slideshow').data({'current_slide':slide_num});
				change_slide($(this).parent('div.slideshow'));
				return false;
			});
			span_nav.bind("click", function(e){
				$(this).parent().parent('div.slideshow').data({'current_slide':$(this).index()-1});
				change_slide($(this).parent().parent('div.slideshow'));
				return false;
			});
			
		}
		slide_chunks.eq(index).data({ 'id': index, 'current_slide': 0, 'total_slides':slide_chunks.eq(index).find('img').length });
	});
}

function change_slide(elem){
	var current_child_id = elem.data('current_slide');
	var nav = elem.find("#pg_slideshow_nav span");
	nav.removeClass('curr');
	nav.eq(current_child_id).addClass('curr');
	var slides = elem.find("img");
	slides.fadeOut();
	slides.eq(current_child_id).stop(true,false).fadeIn();
}
// 
// function slideshow_change(num){
// 
// 	if(num >= generic_total_slides) {
// 		num = 0;
// 	}
// 
// 	var slideshow = $('.slideshow');
// 	var slides = slideshow.children('img');
// 	
// 	slides.eq(generic_slide_curr).fadeOut();
// 	slides.eq(num).fadeIn();
// 	
// 	slideshow_nav_change(num);
// 	
// 	generic_slide_curr = num;
// 
// }
// 
// function slideshow_nav_change(num){
// 
// 	var slides_nav = $('#pg_slideshow_nav span');
// 
// 	slides_nav.eq(generic_slide_curr).removeClass('curr');
// 	slides_nav.eq(num).addClass('curr');
// 
// }



var isPhotoLoaded = false;

function loadcheck(){
	isPhotoLoaded = true;
	
	$('#section_main .imageblock').delay(400).animate({
		'opacity' : 1.0
	}, 800)
	
}



$.fn.smartBackgroundImage = function(url){
  var t = this;
  //create an img so the browser will download the image:
  $('<img />')
    .attr('src', url)
    .load(function(){ //attach onload to set background-image
       t.each(function(){ 
          $(this).css('backgroundImage', 'url('+url+')' );
       });
    });
   return this;
 }
 
 $.fn.ToggleInputValue = function(){
    return $(this).each(function(){
        var Input = $(this);
        var default_value = Input.val();

        Input.focus(function() {
           if(Input.val() == default_value) Input.val("");
        }).blur(function(){
            if(Input.val().length == 0) Input.val(default_value);
        });
    });
}
