

jQuery.scgrph_absolute_path = function(path) {
	var e = document.createElement('span');
	e.innerHTML = '<a href="' + path + '" />';
	return e.firstChild.href;
}

jQuery.fn.scgrph_swapper = function(settings) {
	var regExp = {
		name_NUM_n_o:{
			attach:{regexp:new RegExp("^(.+)_n(\.gif|\.jpg|\.png)$"),callback:"$1_o$2"},
			dettach:{regexp:new RegExp("^(.+)_o(\.gif|\.jpg|\.png)$"),callback:"$1_n$2"}
		},
		name_NUM_o:{
			attach:{regexp:new RegExp("^(.+)(\.gif|\.jpg|\.png)$"),callback:"$1_o$2"},
			dettach:{regexp:new RegExp("^(.+)_o(\.gif|\.jpg|\.png)$"),callback:"$1$2"}
		},
		name_o_NUM:{
			attach:{regexp:new RegExp("^(.+[^o])(_[0-9]+)(\.gif|\.jpg|\.png)$"),callback:"$1_o$2$3"},
			dettach:{regexp:new RegExp("^(.+)_o(_[0-9]+)(\.gif|\.jpg|\.png)$"),callback:"$1$2$3"}
		}
	};
	

	
	var basename = function(path){
		if(path.indexOf("http://") != 0 && path.indexOf("/") != 0) path = jQuery.scgrph_absolute_path(path);
		if(path.indexOf("?")!=-1) path = path.substring(0,path.indexOf("?"));
		if(path.indexOf("#")!=-1) path = path.substring(0,path.indexOf("#"));
		
		if(path.indexOf(window.location.protocol + "//")>-1) return path;
		if(path.charAt(path.length-1) == "/"){
			//if(path.indexOf("/") == 0) path = path.substr(1);
//			path += "index.html";
		}
		return window.location.protocol + "//" + window.location.hostname +  path;
	};
	
	settings = jQuery.extend({
		regexp: "name_o_NUM"
	}, settings || {});


	
	var re = regExp[settings.regexp];
	
	this.
	each(function(){
		var e = jQuery(this);
		var src = e.attr("src").replace(re.dettach.regexp,re.dettach.callback);
		if(basename(window.location.href).indexOf(basename(e.parent().attr("href"))) == 0){
			src = src.replace(re.attach.regexp,re.attach.callback);
			e.addClass("scgrph-cur");
		}
		e.attr({src:src});
	}).
	mouseover(function(){
		if(!jQuery.className.has(this,"scgrph-cur")){
			var e = jQuery(this);
			var src = e.attr("src").replace(re.attach.regexp,re.attach.callback);
			e.attr({src:src});
		}
	}).
	mouseout(function(){
		if(!jQuery.className.has(this,"scgrph-cur")){
			var e = jQuery(this);
			var src = e.attr("src").replace(re.dettach.regexp,re.dettach.callback);
			e.attr({src:src});
		}
	});


	if(settings.force == "active"){
		this.each(function(){
			var e = jQuery(this);
			e.addClass("scgrph-cur").attr({src:e.attr("src").replace(re.attach.regexp,re.attach.callback)}).
			mouseover(function(){}).mouseout(function(){});
		});
	}
	else if(settings.force == "inactive"){
		this.each(function(){
			var e = jQuery(this);
			e.removeClass("scgrph-cur").attr({src:e.attr("src").replace(re.dettach.regexp,re.dettach.callback)});
		});
	}

	
};



jQuery.fn.openWin = function(settings) {
	/*
	settings = jQuery.extend({
	name: "defaultName",
	size: 5,
	global: true
	}, settings || {});
	*/
	/*
	this.click(function(){
		var href = this.href;
		var name = href.split("-").join("_").split("/").join("_").split(".").join("_").split(":").join("_").split("?")[0].split("#")[0];
		var ext = eval("[{"+this.rel+"}]")[0];
		var w = (ext && ext["w"])?ext["w"]:640;
		var h = (ext && ext["h"])?ext["h"]:480;
		var s = (ext && ext["s"])?ext["s"]:0;
		var r = (ext && ext["r"])?ext["r"]:0;
		var win = window.open(href,name,'width=' + w + ',height=' + h + ',scrollbars=' + s+",resizable="+r);
		win.focus();
		return false;
	});
*/
	var sv = navigator.userAgent.split("Safari/")[1];
	if(parseInt(sv) <= 312){
//		$(this).attr("target","_self").click(jQuery.openWinClick);
//		var url = "id"+Math.floor(Math.random()*10000);
//		$(this).before('<span id="'+url+'" />');
//		$(this).attr("title",$(this).attr("href")).attr("href","#"+url).attr("target","_self").click(jQuery.openWinClick)
;	
		$(this).each(
			function(){$(this).get(0).onclick=function(e){jQuery.openWinClick($(e.target).parents("a").get(0));return false;}});
}
	else this.click(jQuery.openWinClick);
};



jQuery.openWinClick = function(o){
	if(o.clientX) o = this;
	if(o.length) o=o[0];
	var href = o.href;//console.log(o);console.log(href);return false;
	if(!href || window.location.href == href) href = o.title;
	
	var name = href.split("-").join("_").split("/").join("_").split(".").join("_").split(":").join("_").split("?")[0].split("#")[0];
	
	var ext;
	if(o.rel) ext = eval("[{"+o.rel+"}]")[0];
	else ext = {};
	var w = (ext && ext["w"])?ext["w"]:640;
	var h = (ext && ext["h"])?ext["h"]:480;
	var s = (ext && ext["s"])?ext["s"]:0;
	var r = (ext && ext["r"])?ext["r"]:0;
	var win = window.open(href,name,'width=' + w + ',height=' + h + ',scrollbars=' + s+",resizable="+r);
	
	win.focus();
	
	return false;
};


(function($){
	$.scgrph_import = function(){
		var callback = Array.prototype.pop.apply( arguments );
		var dict = {};
		var files = arguments;
		for(var i=0,l=files.length;i<l;i++){
			dict[files[i]] = false;
		}
		for(var i=0,l=files.length;i<l;i++){
			var file = $.scgrph_absolute_path(files[i]);
/*			if(jQuery.browser.safari)
				file = files[i];*/
			
			var target = files[i];
			var func = function(target,dict){
				return function(){
					dict[target] = true;
					for(var n=0,m=files.length;n<m;n++){
						if(!dict[files[n]]){return;}
					}
					
					/*if(!jQuery.browser.safari)*/ callback();
//					else setTimeout(callback,200);
				}
			};
			$.ajax({url:file,dataType:"script",complete:func(target,dict),cache:true});
		}
	};
})(jQuery);

(function($) {
	$.fn.equalizeCols = function(children){
		var child = Array(0);
		if (children) child = children.split(",");
		var maxH = 0;
		this.each(
		  function(i) 
		  {
		    if (this.offsetHeight>maxH) maxH = this.offsetHeight;
		  }
		).css("height", "auto").each(
		  function(i)
		  {
		    var gap = maxH-this.offsetHeight;
		    if (gap > 0)
		    {
		      t = document.createElement('div');
		      $(t).attr("class","fill").css("height",gap+"px");
		      if (child.length > i)
		      {
		        $(this).find(child[i]).children(':last-child').after(t);
		      } 
		      else 
		      {
		        $(this).children(':last-child').after(t);
		      }
		    }
		  }  
		);
	}
})(jQuery);

/*
* jQuery Watermark Plugin
*   http://updatepanel.net/2009/04/17/jquery-watermark-plugin/
*
* Copyright (c) 2009 Ting Zwei Kuei
*
* Dual licensed under the MIT and GPL licenses.
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*/
(function($) {
    $.fn.updnWatermark = function(options) {
        options = $.extend({}, $.fn.updnWatermark.defaults, options);
        return this.each(function() {
            // only create watermark if title attribute exists
            if (this.title) {
                var $input = $(this);
                var pos = $input.position();
                var $label = $(document.createElement("label"))
                    .text(this.title)
                    .attr("for", this.id)
                    .css({
                        position: "absolute",
                        left: pos.left,
                        top: pos.top
                    })
                    .addClass(options.cssClass)
                    .hide()
                    .insertBefore(this);
                $input
                    .focus(function(ev) {
                        $label.hide();
                    })
                    .blur(function(ev) {
                        if (!$(this).val()) {
                            $label.fadeIn("fast");
                        }
                    });
                // set initial state
                if (!$input.val()) {
                    $label.fadeIn("fast");
                }
            }
        });
    };
    $.fn.updnWatermark.defaults = {
        cssClass: "updnWatermark"
    };
    $.updnWatermark = {
        attachAll: function(options) {
            $("input:text[title!=''],input:password[title!=''],textarea[title!='']").updnWatermark(options);
        }
    };
})(jQuery);


/* 
	jQuery TextAreaResizer plugin
	Created on 17th January 2008 by Ryan O'Dell 
	Version 1.0.4
	
	Converted from Drupal -> textarea.js
	Found source: http://plugins.jquery.com/misc/textarea.js
	$Id: textarea.js,v 1.11.2.1 2007/04/18 02:41:19 drumm Exp $

	1.0.1 Updates to missing global 'var', added extra global variables, fixed multiple instances, improved iFrame support
	1.0.2 Updates according to textarea.focus
	1.0.3 Further updates including removing the textarea.focus and moving private variables to top
	1.0.4 Re-instated the blur/focus events, according to information supplied by dec

	
*/
(function($) {
	/* private variable "oHover" used to determine if you're still hovering over the same element */
	var textarea, staticOffset;  // added the var declaration for 'staticOffset' thanks to issue logged by dec.
	var iLastMousePos = 0;
	var iMin = 32;
	var grip;
	/* TextAreaResizer plugin */
	$.fn.TextAreaResizer = function() {
		return this.each(function() {
		    textarea = $(this).addClass('processed'), staticOffset = null;

			// 18-01-08 jQuery bind to pass data element rather than direct mousedown - Ryan O'Dell
		    // When wrapping the text area, work around an IE margin bug.  See:
		    // http://jaspan.com/ie-inherited-margin-bug-form-elements-and-haslayout
		    $(this).wrap('<div class="resizable-textarea"><span></span></div>')
		      .parent().append($('<div class="grippie"></div>').bind("mousedown",{el: this} , startDrag));

		    var grippie = $('div.grippie', $(this).parent())[0];
		    grippie.style.marginRight = (grippie.offsetWidth - $(this)[0].offsetWidth) +'px';

		});
	};
	/* private functions */
	function startDrag(e) {
		textarea = $(e.data.el);
		textarea.blur();
		iLastMousePos = mousePosition(e).y;
		staticOffset = textarea.height() - iLastMousePos;
		textarea.css('opacity', 0.25);
		$(document).mousemove(performDrag).mouseup(endDrag);
		return false;
	}

	function performDrag(e) {
		var iThisMousePos = mousePosition(e).y;
		var iMousePos = staticOffset + iThisMousePos;
		if (iLastMousePos >= (iThisMousePos)) {
			iMousePos -= 5;
		}
		iLastMousePos = iThisMousePos;
		iMousePos = Math.max(iMin, iMousePos);
		textarea.height(iMousePos + 'px');
		if (iMousePos < iMin) {
			endDrag(e);
		}
		return false;
	}

	function endDrag(e) {
		$(document).unbind('mousemove', performDrag).unbind('mouseup', endDrag);
		textarea.css('opacity', 1);
		textarea.focus();
		textarea = null;
		staticOffset = null;
		iLastMousePos = 0;
	}

	function mousePosition(e) {
		return { x: e.clientX + document.documentElement.scrollLeft, y: e.clientY + document.documentElement.scrollTop };
	};
})(jQuery);


