$(document).ready(function(){
    
    browser.check();
    
    //top search box behavior
    topSearch.checkSubmit();
    topSearch.initPlaceHolder();
    
    // Make jQuery dialog closable on blur. Must add .dialog class
    modalMessages.closeOnBlur();
    
    // facebook logout init
    topUserSession.initLogout();

});

var browser = {
    "check":function(){
        if($.browser.msie && $.browser.version == '8.0'){
            $('body').addClass('ie8');
        }
    }
};

var topSearch = {
    "checkSubmit": function(){
        $("#topBarSearch").submit(function(){

            //Millenial Media Tracking pixel code
            var timestamp = new Date().getTime();
            if(typeof(mm_urid) != 'undefined') $.get('http://cvt.mydas.mobi/handleConversion?goalid=17264&urid='+mm_urid+'&timestamp='+timestamp);

            var box = $(this).find('input');
            var text = box.val();
            text = jQuery.trim(text);
            box.val(text);
            if(text == "" || text == topSearch.defaultText){
                return false;
            }

        });
    },
    "initPlaceHolder":function(){
        placeHolder.init( $('input.topSearchBox'), topSearch.defaultText);
    },
    "defaultText": 'Enter Author, Title or ISBN'
};

var topUserSession = {
    "initLogout":function(){
        $("a.logoutLink").click(function(){

            if(FB.getAuthResponse() != null){
                FB.logout(function(response){
                    window.location = '/account/logout';    
                });
            }
            else{
                window.location = '/account/logout';
            }

            return false;
        });
    }
};

var modalMessages = {

    "closeOnBlur": function(){
        $('.ui-widget-overlay').live("click", function() {
            $(".dialog").dialog("close");
        });
    }

}

var redir = {
    RedirNonHttps:function() {
	    //I know this shouldn't be done on the client side, but because of how our backend is set up, it unfortunately has to be done this way...
        if (location.href.indexOf("https://") == -1) {
            location.href = location.href.replace("http://", "https://");
        }
    }
};

var placeHolder = {
    "init":function(elem, defaultText){

        var obj = this;

        if(obj.hasPlaceHolder()){
            return;
        }

        if( elem.val() == "" ){
            elem.addClass('gray').val(defaultText);
        }
        
        elem.focus(function(){

            if( $(this).hasClass('gray') ){
                $(this).removeClass('gray').removeClass('black').addClass('black').val("");
            }
        })
        .blur(function(){
            if( $(this).val() == "" ){
                $(this).removeClass('black').removeClass('gray').addClass('gray').val(defaultText);
            }
        });
    
    },
    "hasPlaceHolder":function(){
        var i = document.createElement('input');
        return 'placeholder' in i;
    }    
};

/************** Global JS Constants ***************/

// jQuery UI Modal default values
var modalDefault = {
	modal: true,
    draggable: false,
    resizable: false,
	position: "center",
    show: "fade"
//    hide: "fade"
};

var isiPad = navigator.userAgent.match(/iPad/i) != null;

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};

