/* 
Title:		dtott global javascripts
Author: 	dan@dtott.com
Copyright:	2010, Daniel T Ott, LLC. All rights resevered (no copying or use without permission).
*/
(function($){

    $.dtWatchSize = function (options) {
        return $.dtWatchSize.impl.init(options);
    };
    $.dtWatchSize.defaults = {
    
    };
        
    $.dtWatchSize.impl = {
    
        init: function(options){

            var obj = this;
            $.dtWatchSize.impl.switchSize($(window).width());
            $(window).resize(function(){$.dtWatchSize.impl.switchSize($(window).width());});
            
        },
        switchSize: function(size){
            $("body").removeClass();
            var sizeClass = "";
            if(size > 1675){
                sizeClass = "sizeStopWide";
            }
            else if(size > 1440){
                sizeClass = "sizeStop1440";
            }
            else if(size > 1280){
                sizeClass = "sizeStop1280";
            }
            else if(size > 1024){
                sizeClass = "sizeStop1024";
            }
            else if(size > 800){
                sizeClass = "sizeStop800";
            }
            else if(size > 650){
                sizeClass = "sizeStop500";
            }
            else{
                sizeClass = "sizeStopNarrow";
            }
            
            $("body").addClass(sizeClass);
        }
    };
    
    $.dtWatchSize({});
    
    var preloadLoader = document.createElement("img");
    preloadLoader.src = "/themes/dtott/i/loading.gif";
    
    
    $("#home #thoughts .callOut a").attr("href","/more-thoughts/P4").live("click",function(){
        var l = $(this);
        $("#thoughts").find(".w").fadeOut("fast",function(){
            var w = $(this);
            $("#thoughts").addClass("loading");
            $.get(l.attr("href"),function(data){
                $("#thoughts").removeClass("loading");
                w.html(data).fadeIn("fast");
            },"html");
        });
        return false;
    });
    
    
    $("#home #thoughts .w > article.alt, #thoughts-section #thoughts > article.alt").each(function(){
        var a = $(this);
        var b = $(this).prev();
        if(a.height() > b.height()){a.addClass("bg");}
        else{b.addClass("bg");}
        
    });
    
    
    $("*[href]:not(a)").click(function(){ window.location = $(this).attr("href"); });
    
    

    
    
    var contactFormSubmit = function(){
        
        var tform = $(this);
        tform.find("div.errors").remove();
        var errors = "";
        tform.find(":input[required='required']").each(function(){
            if($(this).val() == ""){
                errors = ", "+ $(this).attr("title") + errors;
            }
        });
        if(errors != ""){
            errors = "Please fill out the following fields: " + errors.substr(2);
        }
        
        tform.find(":input[name='email']").each(function(){
            var filter = /^([a-zA-Z0-9_.\-])+@([a-zA-Z0-9_.\-])+\.([a-zA-Z])+([a-zA-Z])+/;
            if (!filter.test($(this).val())){
                errors = errors + (errors ==  "" ? '' : '<br />') + "Please enter a valid email address";
            }
        });
        if(errors != ""){
            errors = $('<div class="errors"><h3>There were one or more errors:</h3><div>' + errors + '</div></div>');
            tform.find("h2").after(errors);
        }
        else{
            
            tform.height(tform.height()).children().wrapAll('<div class="w" />');
            var w = tform.find(".w");
            w.fadeOut("normal",function(){
                tform.addClass("loading");
                $.post(tform.attr("action"),tform.serialize(),function(data){
                    tform.removeClass("loading");
                    w.html(data).fadeIn("normal");
                },"html");
            });
            
        }
        return false;
          
    };
    

    var contactC = $("body:not(#contact-section) #contactForm").dialog({
        modal:true,
        resizable: false,
        draggable: false,
        width: 500,
        open: function(){
            var con = $("#contactForm");
            var sp = con.find(".sp");
            if(sp.length){
            sp.remove();
            con.append('<input type="hidden" name="checker" value="0" /><input type="hidden" name="checker2" value="" />');
            }
            con.submit(contactFormSubmit);
            if(!con.find(".footer .modalClose").length){
                var c = $('<a class="modalClose" href="#">cancel</a>');
                con.find(".footer").append("or ").append(c);
            }
            
        },
        autoOpen: false
    });


    $("a.contactLink").live("click",function(){
        contactC.dialog("open");
        return false;
    });
    
    
    $(".ui-widget-overlay,.modalClose").live("click",function(){
        contactC.dialog("close");
        return false;
    });

    
    if(!Modernizr.input.placeholder){
        $(":input[placeholder]").each(function(){
            if($(this).val() == "" || $(this).val()==$(this).attr("placeholder")){
                $(this).addClass("placeholder");
                $(this).val($(this).attr("placeholder"));
            }
            else{$(this).closest("form").addClass("inFocus");}
            $(this)
                .focus(function(){
                    $(this).removeClass("placeholder").closest("form").addClass("inFocus");
                    if($(this).val() == $(this).attr("placeholder")){$(this).val("");}
                })
                .blur(function(){
                    if($(this).val()==""){
                        $(this).addClass("placeholder").val($(this).attr("placeholder"));
                        $(this).closest("form").removeClass("inFocus");
                    }
                });
        
        });
    }


    
    
    

    
    
    

    
    
    

    
    
    

    
        
})(jQuery);