$(document).ready(function() {

//NAVIGATION STUFF
    $('.blog a').click(function(){
    $('.gn_for_blog').slideDown(300);
    $('.blog a').css('color', '#000');
    return false;
    });
    
//INDEX PAGE ACCORDION STUFF
    $('.main_tout .slide6, .blog_tout .slide2').css({'width': 660, 'height': 345});
    $('#slide6').fadeIn(300);
    
    $('.main_tout .slip').click(function(){
		if ($(this).width() > 64)
			return;
		var what = $(this).attr('class').replace( /(^.+\D)(\d+)(\D.+$)/i,'$2');
		$('.main_tout .slip').stop(true);
		$(this).animate({'width': 660}, 1000);
		$('.main_tout .slip').not(this).animate({'width': 64}, 1000);
		$('.lead_post').slideUp(300);
		$('#slide'+what).slideDown(300);
    });
    
// DIRTY, FILTHY COLUMN EVENING    
    var lcolHeight = $('.lcol').height();
    $('.rcol').css('min-height', lcolHeight);   
    $('.split_rcol, .temp2').css('min-height', lcolHeight+15);   
    
//REVEAL STUFF
    $('.comm_add').click(function(){
       $('.comm_form').slideDown(300);
       });
       
//FORM STUFF
// CLEAR FIELDS ON FOCUS
	$('.search_form input[type="text"]').focus(function(){
	    if ($(this).val() == 'Search SurfaceMag.com'){ 
	        $(this).val('').removeClass('graytext upper');
	    }
	});
	$('.search_form input[type="text"]').addClass('graytext upper');
	
	$('#newsletter-subscribe input[type="text"][name="email"]').focus(function(){
        if ($(this).val() == 'Enter E-mail'){ 
            $(this).val('').removeClass('graytext upper');
        }
	})
	$('#newsletter-subscribe input[type="text"][name="email"]').addClass('graytext upper').attr('value','Enter E-mail');
	   
	if ($('.comm_form input[name="name"]').val() == '') $('.comm_form input[name="name"]').val('Name');
	if ($('.comm_form input[name="email"]').val() == '') $('.comm_form input[name="email"]').val('Email');
	if ($('.comm_form input[name="website"]').val() == '') $('.comm_form input[name="website"]').val('Website');

	$('.comm_form input[name="name"]').focus(function(){
		if ($(this).val() == 'Name') $(this).val('');
	});
	$('.comm_form input[name="name"]').focusout(function(){
		if ($(this).val() == '') $(this).val('Name');
	});
	$('.comm_form input[name="email"]').focus(function(){
		if ($(this).val() == 'Email') $(this).val('');
	});
	$('.comm_form input[name="email"]').focusout(function(){
		if ($(this).val() == '') $(this).val('Email');
	});
	$('.comm_form input[name="website"]').focus(function(){
		if ($(this).val() == 'Website') $(this).val('');
	});
	$('.comm_form input[name="website"]').focusout(function(){
		if ($(this).val() == '') $(this).val('Website');
	});
	$('.comm_form').submit(function() {
		if ($('.comm_form input[name="name"]').val() == 'Name')
			$('.comm_form input[name="name"]').val('');
		if ($('.comm_form input[name="email"]').val() == 'Email')
			$('.comm_form input[name="email"]').val('');
		if ($('.comm_form input[name="website"]').val() == 'Website')
			$('.comm_form input[name="website"]').val('');
	});

//COMMENTING FROM LINK
   var addy = location.href;
   var blogcoms = addy.indexOf("#blogcoms");
   if(blogcoms != -1)   {
       $('.comm_add').hide();
       $('.comm_form').slideDown(300);
   }
   var commsuccess = addy.indexOf("#commsuccess");
   if(commsuccess != -1)   {
       $('.comm_add').hide();
   }
   
// HANDLE SUBSCRIPTION SUBMIT
    $('#submit-subscription').click(function(event) {
        event.preventDefault();
		$.fn.cmsPatchCSRF();
        var email = $("#newsletter-subscribe > input[type='text']").val();
        if (email != '') {
            $.ajax({
                type: 'POST',
                url: '/subscribe_to_mailing',
                data: {'email' : email},
                dataType: "json",
                complete : function(data) {
                    $('#subscription-message').html("Thank you for signing up for Surface Mag mailings.");
                    $('#subscription-message').css('display', 'inline');
                    return true;
                }
            });
        } else {
            
        }
    });
    
    // social networking
    $('.sharelink').bind('mouseenter mouseleave click', function(e){
        
        e.preventDefault(); // don't follow the link
        
        var type = e.type;
        var link = $(this); link.css('position','relative');
        var box = $('#sharebox');
        var pos = link.position();
        var left = pos.left;
        var top = pos.top;
        if(link.offsetParent() != box.offsetParent()){
            pos = link.offsetParent().position() ;
            left += pos.left;
            top += pos.top;
        }
        
        if(!box.length) return;
        if(/mouseenter|click/.test(type)){
            
            box.css({
                'left' : left + 'px',
                'top' : (top + link.height() ) + 'px'
            }).show();
            
        }
        else {
            if( !$(e.relatedTarget).closest('#sharebox').length 
                    && !box.find(e.relatedTarget).length 
                    && !box.filter(e.relatedTarget).length ) {
                box.hide();
            }
        }
    });
    $('#sharebox').mouseleave(function(){
        $(this).hide();
    });
    
    $('.blog_comment_continue').click(function(e){
        var expander = $(this);
        var comment = expander.prevAll('.blog_comment').eq(0);
        if(comment.height() < comment.prop('scrollHeight')){
            comment.animate({'max-height':comment.prop('scrollHeight') }, {
                delay: 200,
                complete: function(){
                    expander.animate({
                        'margin-top': -20,
                        'margin-bottom' : 0,
                        'opacity': 0
                    },{ 
                        delay:200,
                        complete: function(){
                            $(this).hide();
                        }
                    });
                }
            });
            
        }
        
    });
    
    $('.blog_comment').each(function(index, comment){
        
        var expander = $(this).nextAll('.blog_comment_continue').eq(0);
        if($(comment).height() < comment.scrollHeight){
            $(expander).css('display','block');
        }
        else {
            $(expander).css('display','none');
        }
    });
  
});



// browser Object
// exposes various Web browser properties
// browser#name, browser#version and browser#ua (User-Agent) return strings
// The tests -- browser#ie6, browser#chrome, et al -- each return a boolean
// NOTE: Unlike jQuery, this will return browser rather than rendering engine versions
var browser = (function (){
    var ua = navigator.userAgent, ver = "0", name = "";
    var b = {
    /* by rendering engine */
    gecko:  ua.indexOf('Gecko') > -1 && !/KHTML|AppleWebKit|Opera/.test(ua),
    msie: !!(window.attachEvent && ua.indexOf('Opera') === -1),
    presto: ua.indexOf('Presto/') > -1,
    webkit: ua.indexOf('AppleWebKit/') > -1, 
    /* by brand */
    chrome: ua.indexOf('Chrome/') > -1,
    firefox: ua.indexOf('Firefox/') > -1 && !/KHTML|AppleWebKit|Opera/.test(ua),
    mobilesafari: /Apple.*Mobile.*Safari/.test(ua),
    mobileopera: ua.indexOf('Opera Mobi') > -1,
    opera:  ua.indexOf('Opera') > -1,
    safari: ua.indexOf('Safari/') > -1 && ua.indexOf('Chrome') === -1,
    thunderbird: ua.indexOf('Thunderbird/') > -1 /* email client */
    };
    
    if(b.msie)
        {ver = ua.replace(/.*MSIE\s([^;]*).*/,'$1'); name = "Internet Explorer";}
    else if(b.firefox)
        {ver = ua.replace(/.*Firefox\/([^\(]*)\b.*/,'$1'); name = "Firefox";}
    else if(b.safari && !b.mobilesafari)
        {ver = ua.replace(/.*Version\/([^\s]*).*/,'$1'); name = "Safari";}
    else if(b.opera && !b.mobileopera)
        {ver = ua.replace(/.*Version\/([^\s]*).*/,'$1'); name = "Opera";}
    else if(b.chrome)
        {ver = ua.replace(/.*Chrome\/([^\s]*).*/,'$1'); name = "Chrome";}
    else if(b.mobileopera)
        {ver = ua.replace(/.*Version\/([^\s]*).*/,'$1'); name = "Opera Mobi";}
    else if(b.mobilesafari)
        {ver = ua.replace(/.*Version\/([^\s]*).*/,'$1'); name = "Mobile Safari";}
    else if(b.thunderbird)
        {ver = ua.replace(/.*Thunderbird\/([^\s]*).*/,'$1'); name = "Thunderbird";}
    else
        ver = ua.replace(/.*Version\/([^\s]*).*/,'$1');
        
    if(isNaN(parseFloat(ver))) ver = "0";
    b.version = ver;
    b.name = name;
    b.ua = ua;
    
    // aliases
    b.ver = b.version;
    b.ff = b.firefox; 
    b.ie = b.msie;
    b.ie6 = b.ie && parseInt(b.ver) == 6;
    b.ie7 = b.ie && parseInt(b.ver) == 7; 
    b.ie8 = b.ie && parseInt(b.ver) == 8; 
    b.ie9 = b.ie && parseInt(b.ver) == 9;
    b.operamobi = b.mobileopera;
    b.tbird = b.thunderbird;  
    
    return b;
})();

