/*
common javascript functions
*/
// -----------------------------------------------
// validate email
// Code by: Sukiman - http://www.webexcellence.net
// -----------------------------------------------
function validEmail(str) {
	if (tmp = /^(.+)@(.+)$/.exec(str)) {
		var usr = tmp[1];
		var domain = tmp[2];
		var validDomain = false;
		var validUser = false;
		
		if (tmp = /^[^\] \(\)<>@,;:\.\\\"\[]+(\.[^\] \(\)<>@,;:\.\\\"\[]+)*$/.test(usr)) {
			validUser = true;
		}
		if (validUser) {
			if (tmp = /^[^\] \(\)<>@,;:\.\\\"\[]+(\.[^\] \(\)<>@,;:\.\\\"\[]+)*$/.exec(domain)) {
				if (tmp[0] != undefined && tmp[1] != undefined) {
					validDomain = true;
				}
			}
		}
		
		return (validUser && validDomain);
	} else {
		return false;
	}
}
//-- validate email

function debug(msg) {
	$("#debug").html(msg+"<br />");
}

// added 04-30-08
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

// added 04-30-08
function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

function NewWindow(mypage, myname, w, h, scroll, center) {
	if (typeof h == "string") {
		if (mm = h.match(/(.*?)\%/)) {
			h = (mm[1]/100) * screen.height;
		}
	}
	if (typeof w == "string") {
		if (mm = w.match(/(.*?)\%/)) {
			w = (mm[1]/100) * screen.width;
		}
	}

	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	if (center == null || center == undefined) {
		center = true;
	}
	
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function setCookie(c_name,value,expiredays,path) {
	var exdate=new Date();

	if (path == undefined || path == null) {
		path = "/";
	}

	params = new Array();
	i=0
	params[i++] = c_name + "=" + escape(value);

	if (!(expiredays == null || expiredays == undefined)) {
		exdate.setDate(exdate.getDate()+expiredays);
		params[i++] = "expires=" + exdate.toGMTString()
	}

	params[i++] = "path=" + path;

	document.cookie = params.join(";");

//document.cookie=c_name+ "=" +escape(value)+
//((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+"; path=/");
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}
function currentCookiePath() {
	url = document.URL;
	if (m = url.match(/\/work\/(.+?)\/.*/)) {
		return m[1];
	}
	return undefined;
}

//////////////////

var loadingImg = new Image();
loadingImg.src = "/images/ajax-loader.gif";
var scrollPaneCookie = "scroll_startuppos";
var scrollPaneCookieTag = scrollPaneCookie+"tag";
var cur_cookie_path = undefined;

cur_cookie_path = currentCookiePath();
if (cur_cookie_path == undefined ) {
	setCookie(scrollPaneCookie,0,-1);
}
if (cur_cookie_path != getCookie(scrollPaneCookieTag)) {
	setCookie(scrollPaneCookie,0,-1);
}
setCookie(scrollPaneCookieTag,cur_cookie_path);
/////


(function ($) {
	$.loewy = $.loewy || {};
	
	$.loewy.num = function(elem,prop) {
		return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0;
	}
	
	$.loewy.getPageSize = function() {
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}


		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		
		//
		var largestWidth;
		var largestHeight;
		var smallestWidth;
		var smallestHeight;
		//
		if ( pageWidth >= windowWidth )
		{	largestWidth = pageWidth; smallestWidth = windowWidth;	}
		else
		{	largestWidth = windowWidth; smallestWidth = pageWidth;	}
		//
		if ( pageHeight >= windowHeight )
		{	largestHeight = pageHeight; smallestHeight = windowHeight;	}
		else
		{	largestHeight = windowHeight; smallestHeight = pageHeight;	}
		
		// Return
		var arrayPageSize = {'pageWidth':pageWidth,'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight,'largestWidth':largestWidth,'largestHeight':largestHeight};
		return arrayPageSize;
	}
	
	$.loewy.getPageScroll = function ( ) {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
		return arrayPageScroll;
	}
	
	$.loewy.pngId=0;
	$.loewy.fixPng = function(p) {
		//fix slide png on IE 6
		if (jQuery.browser.msie && jQuery.browser.version < 7) {
			if (p == undefined) { 
				p = $("body");
			}
			$("img", p).each(function(){
				if (/\.png$/ig.test(this.src)) {
					if (this.id == undefined || this.id == "") {
						this.id = "png_"+$.loewy.pngId;
						$.loewy.pngId++;
					}
					
					if ($("#fixpng_"+this.id).length <= 0) {
					
						s = 'display: block; float: left';
						s += ';margin-left:'+($(this).css("margin-left") ? parseInt($(this).css("margin-left")) : 0)+'px';
						s += ';margin-top:'+($(this).css("margin-top") ? parseInt($(this).css("margin-top")) : 0)+'px';
						s += ';width:'+ ((this.width) ? this.width : $(this).width)+'px';
						s += ';height:'+ ((this.height) ? this.height : $(this).height)+'px';
						
						//alert(s);
						o = $('<div id="fixpng_'+this.id+'" style="'+s+'; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, src=\''+this.src+'\',sizingMethod=\'scale\')"></div>');
						
						$(this).css("display","none").parent().append(o);
					}
					return;
				}
			});
		}
		//-
	}
})(jQuery);
	
(function ($) {
	$.fn.loewyFixMainWrapper = function(m, n) {
		return this.each(function() {
			var self = this;
			var minHeight = m;
			var maxHeight = n;
			
			var fixHeight = function() {
				var pageSize = $.loewy.getPageSize();
				
				if (pageSize.largestHeight < minHeight) {
					$(self).css("height", minHeight+"px");
				} else if (pageSize.largestHeight >= minHeight && pageSize.largestHeight <= maxHeight) {
					$(self).css("height", pageSize.largestHeight+"px");
				} else {
					$(self).css("height", maxHeight+"px");
				}
			};
			
			$(window).resize(function () { fixHeight(); });
			
			fixHeight();
		});
	}
	
	$().ready(function() {
		$("#wrapper", "#home").loewyFixMainWrapper(609,785);
		$.loewy.fixPng();
	});
})($);
