
(function(){
	var $ = jQuery.noConflict(),
		gf = window["gf"] = {},
		undefined,
		console=window.console;
	
	//this is a hack, because the console is being overriden by some of the other JS included by magento
	$(function(){			
		try{
		window.console = console;		
		}catch(err){}
	});
	
	//Create console to avoid tracing errors in browsers without.
	if(console===undefined){
        console = window.console = {};
        window.console.log = window.console.dir = function(){};		
    }
		
	
	/*
     * UTILITIES
     */
    
    gf.utils = {};
    
    //Intended to be passed to $("selector").filter(gf.utils.findActiveLinks). Attaches a $.data("matchLevel", int);
    gf.utils.filterActiveLinks=function(i){
    	var matchLevel = gf.utils.compareUrls(window.location.href, this.href);
    	if(matchLevel >- 1){
    		$(this).data("matchLevel", matchLevel);    		
    		return true;
    	}
    	return false;
    };
    
    gf.utils.filterByMatchLevel=function(i){
    	var $this = $(this),
    		matchLevel = $this.data("matchLevel");
    	if(matchLevel != null && matchLevel > -1){
    		return true;
    	}
    	return false;
    };
    
	gf.utils.splitUrl=function(urlStr){
		var url = urlStr.toString().split(/\/+/),
			extra = [];
		
		url[url.length-1] = url[url.length-1].replace(/#.[^\?]+/, function(){
			extra.push(arguments[0]);
			return "";
		}).replace(/\?.+$/, function(){
			//[].push.apply(extra, arguments[0].split("&"));
			extra.push(arguments[0]);
			return "";
		});
		
		[].push.apply(url, extra);
		return url;
	};

	gf.utils.compareUrls=function(pivot, subject){
		pivot = (typeof pivot === "string") ? gf.utils.splitUrl(pivot) : pivot;
		subject = (typeof subject === "string") ? gf.utils.splitUrl(subject) : subject;
		var match = -1;
		
		for(var u=0; u<pivot.length; u++){
			if(pivot[u] !== subject[u]){
				break;
			}
			match = u;
		}
		//Add an extra match point if the urls are the same length. This will give direct matches priority over subject items that are longer than the pivot. 
		if(match > -1 && pivot.length === subject.length) match++;
		return match;
	};
	
	gf.utils.popup=function(url, id, options){
		var opts = $.extend({
				height:null,
				width:null,
				location:null,
				fullscreen:false,
				status:false,
				toolbar:false,
				location:false,
				menubar:false,
				directories:false,
				resizable:false,
				scrollbars:false
			}, options),
			optsStr = (function(){ 
				var arr=[];
				for(var optName in opts) 
					arr.push(optName+"="+ ((opts[optName] !== true && opts[optName] !== false) ? opts[optName] : opts[optName]*1)); 
				return arr.join(",");
			})(),
			win = window.open(url, id||"gallfurn", optsStr);
		return win;
	}

	gf.utils.popupHtml=function(htmlStr, opts){
		var win = gf.utils.popup(null, "gallfurn_html", $.extend({height:500,width:800}, opts));        
		win.document.writeln(htmlStr);     
		return win;
	}
	
	
    gf.utils.initPages = function (prefix, collection) {
		var hooks;
		if(!collection) collection = site.pages;
		if(!collection) return;
        // Initialize any pages with the same name as any of the class names on the <body/> tag.
        $(document).ready(function () {
			hooks = $.trim(prefix + " " + document.body.className).split(/\s+/);
			//remove duplicate hooks
			for(var h=0,idx; h<hooks.length; h++){
				var firstIdx = $.inArray(hooks[h], hooks);
				if(firstIdx > -1 && firstIdx < h){
					hooks.splice(h,1);
				}
			}
			
            $.each(hooks, function (i, className) {
                if (className.length && typeof collection[className] === "object" && $.isFunction(collection[className].init)) {
                    collection[className].init();
                }
            });
        });
        $(window).load(function () {
            $.each($.trim(prefix + " " + document.body.className).split(/\s+/), function (i, className) {
                if (className.length && typeof collection[className] === "object") {
                    if ($.isFunction(collection[className].load)) {
                        collection[className].load();
                    }
                    if ($.isFunction(collection[className].delay)) {
                        setTimeout(function () { collection[className].delay(); }, collection[className].delayTime);
                    }
                }

            });
        });

    };
	
	
	gf.utils.preloadImages = function(paths){
		var images = [];
		for(var p=0; p<paths.length; p++){
			var img = new Image();	
			img.src = paths[p];
			images.push(img);
		}
		return images;
	};
	
	
	window.popup = function (url, id, options) {
        var opts = $.extend({
            height: null,
            width: null,
            location: null,
            fullscreen: 0
        }, (typeof id === "object") ? id : options),
            win = window.open(url, id || window.location.host, (function () { var str = ""; for (var optName in opts) { str += optName + "=" + opts[optName] + ","; } return str; })());
        return win;
    };

    window.printHtml = function (htmlStr, opts) {
        var win = window.popup(null, "print", $.extend({ height: 500, width: 800 }, opts));
        win.document.writeln(htmlStr);
        win.print();
        return win;
    };
	
    
	
	
    /*
     * WIDGETS
     */
    gf.widget = {};
    
    
    
    
    
    

	 
	
	
})();
