/*

	13twelve vs. jQuery

	This javascript is the main javascript for the sites actions.

*/

// --------------------------------------------------------------------------------------------------------------

var thirteentwelve = function(){
    var isIE6;
    function browserTest() {
        //
        // Some browser and OS testing. 
        // 
        // Classes written to the body for good browsers, as there doesn't seem to be a performance hit doing that
        // IE seems to have an incredible performance hit doing that, so use conditional comments for it.
        //
    	if (navigator.appVersion.indexOf("Mac")!=-1) {
    		isMac = true;
    		$('body').addClass("isMac");
    	}
    	function searchVersion(browser) {
    		var dataString = navigator.userAgent;
    		var index = dataString.indexOf(browser);
    		if (index == -1) return;
    		var bVersion = parseFloat(dataString.substring(index+browser.length+1));
    		if (browser = "Firefox") {
    		    return bVersion.toString();
		    } else {
		        return bVersion.toString().split(".")[0];
		    }
    	}
    	// Webkit
    	if ($.browser.webkit) {
    		$('body').addClass("isWebkit");
    	}
    	// Mozilla versioning
    	if ($.browser.mozilla) {
    		$('body').addClass("isMozilla");
    		version = searchVersion("Firefox") || "";
    		version = version.replace(".","-");
    		$('body').addClass("isMozilla"+version);
    	}
    	// IE versioning
    	isIE = jQuery.browser.msie;
    	if (isIE) {	
    		version = searchVersion("MSIE") || "";
    		isIE6 = (version == 6) ? true : false;
    		if (isIE6) {
    		    ie6Helpers();
    		}
    	}
    }
    function defaultValues() {
        //
        // Form inputs with default values
        //
        // take off the default values in the sitewide search and newsletter forms
        $('input[type="text"]').each(function(){
            var initVal = $(this).val();
        	//
        	$(this).focus(function(){
        	    $(this).val("");
        	});
            $(this).blur(function(){
        	    if ($(this).val() == "") {
        			$(this).val(initVal);
        		}
        	});
        });

    }
    function listingClicks() {
	    //
	    // Some blocks want to be clickable and have hover states, yet aren't themselves links - they just contain a link
	    // eg. listings, touts
	    // html 5 links as blocks can't be used, as some of these blocks will contain more than one link
	    // and firefox struggles with what it sees as invalid code
	    //
	    var applyTo = $("article, .tout");
	    //
        applyTo.each(function(){
            // find the first link, its assumed that that is the prime link
            var href = $(this).find("a:first").attr("href");
            var $this = $(this);
            var targetBlank = ($(this).find("a:first").attr("target") == "_blank") ? true : false;
            //
            // 1. on hover, add a class, change the pointer, and decorate the link
            // 2. on click, go to to the prime link location
            //
            $this.hover(function(){
                $this.addClass("hover");
            }, function () {
                $this.removeClass("hover");
            }).click(function(event){
                event.preventDefault();                
                if (href != "#" && href != undefined && href.length > 1) {
                    if (!targetBlank) {
                        window.location = href;
                    } else {
                        window.open(href);
                    }
                }
            });
        }); 
	}
	function ie6Helpers() {
	    //
	    // Gah!
	    //
	}
    $(document).ready(function(){
        browserTest();
	    	
    	if(!isIE6) {
    	    listingClicks();
        }
        
        defaultValues();
    });
    return {
        browserTest:browserTest, 
        listingClicks:listingClicks, 
        defaultValues: defaultValues
    }
}();
