/* common.js, polarrose.com, johanb@polarrose.com */

/*  work it harder make it better
    do it faster makes us stronger
    more than ever hour after
    our work is never over  \o/ */

/* behaviors */

Event.addBehavior({
    '#input_search:focus': function() {
        this.addClassName("active");
        if(this.value == "Enter a persons name here..."){
            this.value = "";
        }
    },
    '#input_search:blur': function(){
        this.removeClassName("active");
        if(this.value == ""){
            this.value = "Enter a persons name here...";
        }
    },
    '#formcontainer .input:focus': function(){
        this.addClassName("active");
    },
    '#formcontainer .input:blur': function(){
        this.removeClassName("active");
    },
    '#search_submit:click': function(){
    	if($("input_search").value == 'Enter a persons name here...' || $("input_search").value == ' ' || $("input_search").value == null){
    		$("input_search").select();
            return false;
        } else {
        $("search_form").submit();
        $("input_search").blur();
        return false;
    	}
    },
    '#all_found_label:click': function() {
        this.addClassName("checked");
        $("my_found_label").removeClassName("checked");
    },
    '#my_found_label:click': function() {
        this.addClassName("checked");
        $("all_found_label").removeClassName("checked");
    },
    '#button_back:click': function(event){
        if(history.length > 1){
            history.back();
            return false;
        } else {
            top.location = this.href;
        }
    }
})

var results_width = {
    config : {
        target: "results"
    },
    change : function(){
        var el = $(this.config.target);
        var viewportwidth = document.viewport.getWidth();

        if(viewportwidth <= 950){
            el.setStyle({width : "950px"});
        } else if(viewportwidth <= 1140){
            el.setStyle({width : "950px"});
        } else if(viewportwidth <= 1330){
            el.setStyle({width : "1140px"});
        } else if(viewportwidth <= 1520){
            el.setStyle({width : "1140px"});
        } else if(viewportwidth <= 1710){
            el.setStyle({width : "1330px"});
        } else if(viewportwidth <= 1900){
            el.setStyle({width : "1520px"});
        } else if(viewportwidth <= 2090){
            el.setStyle({width : "1710px"});
        } else if(viewportwidth <= 2280){
            el.setStyle({width : "1900px"});
        } else if(viewportwidth <= 2470){
            el.setStyle({width : "2090px"});
        } else if(viewportwidth <= 2660){
            el.setStyle({width : "2280px"});
        } else if(viewportwidth <= 2850){
            el.setStyle({width : "2470px"});
        } else if(viewportwidth <= 3040){
            el.setStyle({width : "2660px"});
        } else if(viewportwidth <= 3230){
            el.setStyle({width : "2850px"});
        } else if(viewportwidth <= 3420){
            el.setStyle({width : "3040px"});
        } else if(viewportwidth <= 3610){
            el.setStyle({width : "3230px"});
        } else if(viewportwidth <= 3800) {
            el.setStyle({width : "3420px"});
        } else {
            el.setStyle({width : "950px"});
        }
    }    
}

Element.addMethods({
    pngfix: function(el){
        var el = $(el);
        var transparentGifPath = '/images/general/transparent.gif';
        if (Prototype.Browser.IE){ /* if it's an img and a png */
            if ((el.match('img')) || (el.match('input')) && (el.src.include('png'))){
                var alphaImgSrc = el.src;
                var sizingMethod = 'scale';
                el.src = transparentGifPath;  
            } else if (el.getStyle('backgroundImage').include('png')){ /* if it's an element with a background png */
                var bgColor = el.getStyle('backgroundColor') || '';
                var elBg = el.getStyle('backgroundImage');
                var alphaImgSrc = elBg.slice(5, elBg.length - 2);
                var sizingMethod = 'crop';
                el.setStyle({ 'background': bgColor + transparentGifPath });
            }
            if (alphaImgSrc) el.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{al}",sizingMethod="#{sz}")'.interpolate({ al: alphaImgSrc, sz: sizingMethod });
        }
    return el;
    }
});

/* dom ready */
document.observe("dom:loaded", function(){

    // google analytics
    if($("analytics")){
        urchinTracker();
    }
    
});