/*
	JQuery code run on every page
*/

$(document).ready(function() {
   
    //On page load, fix the position and height of the header and footer
	$("#header").css('position','fixed');
	$("#content").css('top', $("#header").height());
	$("#footer").css('top', $("#header").height());

   	//image rollovers
	$(".ro").hover(
		function(){
			var str = $(this).attr('src');
			str = str.substr(0,str.length-4) + "_ro.png";
			$(this).attr('src',str);
		},
		function(){
			var str = $(this).attr('src');
			str = str.substr(0,str.length-7) + ".png";
			$(this).attr('src',str);
		}
	);
	
	//smooth page scrolling
	/*
	$(".smooth").click(function(){
		
		$("html").animate({scrollTop : 0},'slow');
	
	});
	*/
	
	$(".smooth").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;
		
		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];
				

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();	
		var target_top = target_offset.top - $("#header").height();

		

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 1000);
	});
	
        $('.close-box').live('click', function(e) {
            e.preventDefault();
            $(this).parent().hide();
        });
        $('.open-box').live('click', function(e) {
            e.preventDefault();
            $(this).parent().find('.share_this_box').show();
        });
        
        
        $('.email-field').focus(function(){ if($(this).val() == 'email me updates') $(this).val(''); });
        $('.email-field').blur(function() { if($(this).val() == '') $(this).val('email me updates'); });
        
        $('.email-update-form').submit(function(e) {
            e.preventDefault();
            
            var email = $.trim($('.email-update-form .email-field').val());
            $('.email-update-form .email-field').removeClass('error');
            $('.email-update-form .errorMessage, .email-update-form .successMessage').css('display', 'none');
            
            if(email == '' || email == 'email me updates') {
                $('.email-update-form .email-field').addClass('error');
                $('.email-update-form .errorMessage').css('display', 'block');
            }
            else {
                $.ajax({
                    type: "POST",
                    url:'/home/subscribe',
                    data: { subEmail:email },
                    cache: false,
                    dataTypeString: 'json',
                    success: function(result) {
                        if(result == '"0"') {
                            $('.email-update-form .successMessage').css('display', 'block');
                            pageTracker._trackPageview('subscribe');
                        }
                        else {
                            $('.email-update-form .errorMessage').css('display', 'block');
                        }
                    }
                });
            }
        });
        
        $('.twitter_link').click(function() {
            pageTracker._trackPageview('twitter-link');
        });
        $('.rss_link').click(function() {
            pageTracker._trackPageview('rss-link');
        });
        $('.linkedin_link').click(function() {
            pageTracker._trackPageview('linkedin-link');
        });
});
