/*
Global variable scope, use this object instead of defining "worldwide global" variables
*/

var dw = {};

/*
Common, useful methods, thingies
*/


/*-*-*-*	GENERAL		*/

var df = {};

//-* A shorter :P getElementById
df.gid = function (obj)
{
	//It's a string, so get the object
	if (typeof obj == "string") obj = document.getElementById(obj);

	return obj;
} //function

//Remove Element
df.rid = function (obj)
{
	var o = df.gid(obj);
	o.parentNode.removeChild(o);
} //if

df.setValue = function (objid, value)
{
	obj = df.gid(objid);
	
	obj.value = value;
} //function

df.system = (function()
{
	var av = navigator.appVersion;
	var ua = navigator.userAgent;
	
	return {
		win: ( av.indexOf( 'Win' ) != -1 ),
		mac: ( av.indexOf( 'Mac' ) != -1 ),
		lin: ( ua.indexOf( 'Linux' ) != -1 )
	} //return
} //function
)();

//It's so neat and tidy, so taken from Prototype!
df.browser = (function()
{
	var ua = navigator.userAgent;
	//Only Opera 5+ has its own object for debug
	var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';
	
	return {
		IE:             !!window.attachEvent && !isOpera,
		Opera:          isOpera,
		WebKit:         ua.indexOf('AppleWebKit/') > -1,
		Gecko:          ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1,
		MobileSafari:   /Apple.*Mobile.*Safari/.test(ua)
	} //return
} //function
)();

/*-*-*-*	DOM RELATED		*/

df.dom = {};

//Appends an element as a child to the object.
df.dom.add = function (oNew, oDest)
{
	var oNew = df.gid(oNew);
	df.gid(oDest).appendChild(oNew);
//	oDest
} //if

//Insert element
df.dom.ins = function (oNew, oReference, place)
{
	var oReference = df.gid(oReference);
	var oNew = df.gid(oNew);

	var parent = oReference.parentNode;

	switch(place)
	{
		case 'after':
			parent.insertBefore(oNew, oReference.nextSibling);
		break;
		
		case 'before':
		default:
			parent.insertBefore(oNew,oReference);
		break;
	} //switch
} //if


/*-*-*-*	POSITIONS	*/

df.pos = {};

df.pos.global = (function()
{
	var compatMode = (document.compatMode == "CSS1Compat" ? true : false);
	var cyberbody = (compatMode ? document.documentElement : document.body);

	return {
		body: 			cyberbody,
		//Window inner height and width...IE doesn't support window.innerWidth/innerHeight
		innerWidth:		typeof( window.innerWidth ) == 'number' ? window.innerWidth : cyberbody.clientWidth,
		innerHeight: 	typeof( window.innerHeight ) == 'number' ? window.innerHeight : cyberbody.clientHeight,
		scrollLeft:		df.browser.IE ? cyberbody.scrollLeft : window.pageXOffset,
		scrollTop:		df.browser.IE ? cyberbody.scrollTop : window.pageYOffset
	} //return
} //function
)();


//Position object into (obj: e to position, oContainer: position inside this e)
df.pos.obj2center = function(o,oContainer)
{
	o = df.gid(o);

	//Use pos.global
	if (!oContainer)
	{
		
	}
	else
	{
		var whContainer = this.getWH(oContainer);
		var whO = this.getWH(o);

		this.setXY(o,
		[
		(whContainer[0]-whO[0]) / 2,
		(whContainer[1]-whO[1]) / 2 
		]
		);
	}
} //function

//Get width/height of obj
df.pos.getWH = function (o)
{
	o = df.gid(o);

	return [parseInt(o.style.width), parseInt(o.style.height)];
}

//Set width/height... arrWH must be an array of [width,height]
df.pos.setWH = function(o,arrWH)
{
	if (typeof(arrWH) == 'object')
	{
		o = df.gid(o);

		o.style.width = parseInt(arrWH[0])+'px';
		o.style.height = parseInt(arrWH[1])+'px';
	} //if
} //function

//Set X/Y aka left/top ... arrXY must be an array of [left,top]
df.pos.setXY = function(o,arrXY)
{
	if (typeof(arrXY) == 'object')
	{
		o = df.gid(o);

		o.style.left = parseInt(arrXY[0])+'px';
		o.style.top = parseInt(arrXY[1])+'px';
	} //if
} //function

//Get width/height of obj
df.pos.getXY = function (o)
{
	o = df.gid(o);

	return [parseInt(o.style.left), parseInt(o.style.top)];
}




/*-*-*-*	WINDOWS		*/
/*	req: modal.css */

df.disableArea = function(name, obj)
{
	obj = df.gid(obj);
	
	oModalWrapper = document.createElement('div');
	oModalWrapper.id = 'disabler_'+(name);
	oModalWrapper.style.position = 'relative';
	
	df.dom.ins(oModalWrapper,obj);
	
	//insert a fill size, semi transparent div right after o
	oModal = document.createElement('div');
	oModal.id = 'Xdisabler_'+(name);
	
	oModal.className = 'dfdisabler';
	
	oModal.style.width = (obj.offsetWidth+10)+'px';
	oModal.style.height = (obj.offsetHeight)+'px';
	
	df.dom.add(oModal,oModalWrapper);
} //function

df.enableArea = function (name)
{
	df.rid('disabler_'+name);
} //function

df.winModal = 
{
//	modalStack: 0,

	modalID: '',
	xxx: 'asd',

/*

DATA = 
{
modalName: "piggy",
modalClass: "winmodal",

modalWH: [100,100],
//Escapable modal window or not
modalClose: true,

modalimageClose: "/img/manci.png"
modalimageCloseClass: "btnclose"

modalcontentClass: "xxx"
};

*/
	
	open: function(DATA)
	{
		//++this.modalStack;
		
		/* WRAPPER - To avoid CSS opacity inheritance*/
		oModalWrapper = document.createElement('div');
		oModalWrapper.id = 'modalwrapper_'+(DATA.modalName);
		
		//Insert right after document.body
		df.dom.ins(oModalWrapper,document.body.firstChild);

		/* MODAL */
		oModal = document.createElement('div');
		oModal.id = 'modal_'+(DATA.modalName);

		oModal.className = DATA.modalClass;

		oModal.style.width = df.pos.global.innerWidth+'px';
		oModal.style.height = df.pos.global.body.scrollHeight+'px';

		if (DATA.modalClose)
			df.addEvent(oModal,'click',this.evclick);

		//Insert into the wrapper, which has 100% opacity
		df.dom.add(oModal,oModalWrapper);


		/* CONTENT */
		oContent = document.createElement('div');
		oContent.id = 'modalcontent_'+(DATA.modalName);

		if (DATA.modalcontentLoadFrom)
		{
			oSRC = df.gid( DATA.modalcontentLoadFrom );
			oSRC.style.display = '';
			df.dom.add(oSRC,oContent);
		} //if

		df._css.add(oContent,DATA.modalcontentClass);
		df.pos.setWH(oContent,DATA.modalWH);

		//Insert content container into oModalWrapper - if we make this oModal's child, opacity will be fckd'
		df.dom.add(oContent,oModalWrapper);

		//OLD:Center it relative to oModal
		//NEW:Center it relative to document.body.clientHeight since we have to cast a veil on the whole scrollable area
		var whArea = [df.pos.global.innerWidth,df.pos.global.body.clientHeight];
		var whO = df.pos.getWH(oContent);

		df.pos.setXY(oContent,[(whArea[0]-whO[0])/2,(whArea[1]-whO[1])/2]);
		//df.pos.obj2center(oContent,document.body);
		
/*		
		CONTINUE HERE
	
df.pos.setXY(oContent,[df.pos.global.innerWidth,df.pos.global.body.clientHeight]);
		
		oModal.style.width = df.pos.global.innerWidth+'px';
		oModal.style.height = df.pos.global.body.scrollHeight+'px';

df.pos.setXY = function(o,arrXY)
{
	if (typeof(arrXY) == 'object')
	{
		o = df.gid(o);

		o.style.left = parseInt(arrXY[0])+'px';
		o.style.top = parseInt(arrXY[1])+'px';
	} //if
} //function
		
*/		/* CLOSE IMG */
		
		//Init close image
		if (DATA.modalimageClose)
		{
			oImgClose = document.createElement('IMG');
			oImgClose.src = DATA.modalimageClose;
			oImgClose.callback = DATA.modalimageCallback;
			
			//Pass ModalID
			oImgClose.setAttribute('modalName',DATA.modalName);
			//Pass callback... weird...
			oImgClose.setAttribute('callback',DATA.modalimageCallback);

			df.addEvent(oImgClose,'click',this.evCloseModal);
			df._css.add(oImgClose,DATA.modalimageCloseClass);
			
			//Inject into oContent
			df.dom.add(oImgClose,oContent);
		} //if

		return oModal.id;
	}, //function

	evclick: function(e)
	{
//		--this.modalStack;
		df.rid(this);
	}, //function

	evCloseModal: function(e)
	{
		if (!e) var e = window.event;
		t = (e.target ? e.target : (e.srcElement ? e.srcElement : null));

		if (typeof(t.callback) === 'function')
			t.callback();

		df.rid('modalwrapper_'+t.getAttribute('modalName'));
	} //function
};


/*-*-*-*	EVENTS		*/

//Very basic shit

df.addEvent = function(oTarget, eType, fn, useCapture) 
{
	var oTarget = df.gid(oTarget);

	if (oTarget.addEventListener) 
	{
		oTarget.addEventListener(eType, fn, useCapture);
		return true;
	}
	else if (oTarget.attachEvent) 
	{
		var r = oTarget.attachEvent('on' + eType, fn);
		return r;
	}
	else 
	{
		oTarget['on' + eType] = fn;
	} //if
} //function

/*-*-*-*	VISIBILITY		*/

df.vis = {};

df.vis.display = function(e, status)
{
	if (status)
		df.gid(e).style.display = '';
	else
		df.gid(e).style.display = 'none';
} //function

df.vis.displayToggle = function (e1,e2, eActive)
{
	//eActive == 0 => e1 will be shown, e2 wont
	if (eActive == 0)
	{
		df.vis.display(e1,true);
		df.vis.display(e2,false);
	}
	else
	{
		df.vis.display(e1,false);
		df.vis.display(e2,true);
	} //if
} //function

df.vis.ible = function(e, status)
{
	if (status)
		df.gid(e).style.visibility = 'visible';
	else
		df.gid(e).style.visibility = 'hidden';
} //function

df.vis.ibleToggle = function (e1,e2, eActive)
{
	//eActive == 0 => e1 will be shown, e2 wont
	if (eActive == 0)
	{
		df.vis.ible(e1,true);
		df.vis.ible(e2,false);
	}
	else
	{
		df.vis.ible(e1,false);
		df.vis.ible(e2,true);
	} //if
} //function


/*-*-*-*	INPUT		*/

//-* (Un)Select all the checkboxes inside a given form
df.chkboxSelectAll = function (formname, status)
{
	obj = df.gid(formname);

	for (var i=0; i < obj.elements.length; i++) 
	{
		obj.elements[i].checked = status;
	} //for
} //function

df.formhasSelectedElement = function (formname,elementType)
{
	obj = df.gid(formname);

	for (var i=0; i < obj.elements.length; i++) 
	{
		if (obj.elements[i].type == elementType && obj.elements[i].checked)
			return true;
		
	} //for
	return false;
} //function

df.getSelectedElements = function(formname,elementType)
{
	obj = df.gid(formname);

	selected = [];
	for (var i=0; i < obj.elements.length; i++) 
	{
		if (obj.elements[i].type == elementType && obj.elements[i].checked)
		{
			selected.push(obj.elements[i].value);
		} //if
	} //for

	return selected;
} //function


/*-*-*-*	CSS		*/

/**
 * df._css.js: utilities for manipulating the CSS class of an HTML element.
 *
 * This module defines a single global symbol named df._css. This object
 * contains utility functions for working with the class attribute (className
 * property) of HTML elements. All functions take two arguments: the element
 * e being tested or manipulated and the CSS class c that is to be tested,
 * added, or removed. If element e is a string, it is taken as an element
 * id and passed to document.getElementById().
 */
df._css = {};

// Return true if element e is a member of the class c; false otherwise
df._css.is = function(e, c) 
{
    if (typeof e == "string") e = document.getElementById(e); // element id

    // Before doing a regexp search, optimize for a couple of common cases.
    var classes = e.className;
    if (!classes) return false;    // Not a member of any classes
    if (classes == c) return true; // Member of just this one class

    // Otherwise, use a regular expression to search for c as a word by itself
    // \b in a regular expression requires a match at a word boundary.
    return e.className.search("\\b" + c + "\\b") != -1;
};

// Add class c to the className of element e if it is not already there.
df._css.add = function(e, c) 
{
    if (typeof e == "string") e = document.getElementById(e); // element id
    if (df._css.is(e, c)) return; // If already a member, do nothing
    if (e.className) c = " " + c;  // Whitespace separator, if needed
    e.className += c;              // Append the new class to the end
};

// Remove all occurrences (if any) of class c from the className of element e
df._css.remove = function(e, c) 
{
    if (typeof e == "string") e = document.getElementById(e); // element id
    // Search the className for all occurrences of c and replace with "".
    // \s* matches any number of whitespace characters.
    // "g" makes the regular expression match any number of occurrences
    e.className = e.className.replace(new RegExp("\\b"+ c+"\\b\\s*", "g"), "");
};


/*-*-*-*	WINDOWS		*/

df.winCenter = function (pageURL, pageName, popWidth, popHeight, extraOptions) 
{
	var posLeft	= (screen.width - popWidth) / 2;
	var posUp	= (screen.height - popHeight) / 2;

	//location,status,scrollbars,
	winProp	= 'width='+popWidth+',height='+popHeight+',left='+posLeft+',top='+posUp+extraOptions;

	Win = window.open(pageURL, pageName, winProp);
	if (parseInt(navigator.appVersion) >= 4) { Win.window.focus(); }
} 


/* old code... Make these non AB normal*/

//yukk
var strUserAgent = navigator.userAgent.toLowerCase(); 
var isIE = strUserAgent.indexOf("msie") > -1; 
var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 

var reNumeric = /[0-9]/;
var reValidChars = /[0-9.]/;
var reValidString = /^[0-9.]*$/;
var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
var reClipboardChars = /[cvxz]/i;

function noNumeric(objEvent) 
{
	var iKeyCode, strKey, objInput;  
	
	if (isIE) 
	{
		iKeyCode = objEvent.keyCode;
		objInput = objEvent.srcElement;
	} 
	else 
	{
		iKeyCode = objEvent.which;
		objInput = objEvent.target;
	} //if
	
	strKey = String.fromCharCode(iKeyCode);
	
	objInput.validValue = objInput.value;
	if (reNumeric.test(strKey) && !reKeyboardChars.test(strKey) && !checkClipboardCode(objEvent, strKey)) 
	{
		//alert("Invalid Character Detected!\nKeyCode = " + iKeyCode + "\nCharacter =" + strKey);
		return false;
	}
	return true;

}

function noAlpha(objEvent) 
{
	var iKeyCode, strKey, objInput;  
	
	if (isIE)
	{
		iKeyCode = objEvent.keyCode;
		objInput = objEvent.srcElement;
	} 
	else 
	{
		iKeyCode = objEvent.which;
		objInput = objEvent.target;
	} //if
	
	strKey = String.fromCharCode(iKeyCode);
	
	objInput.validValue = objInput.value;
	if (! reNumeric.test(strKey) && !reKeyboardChars.test(strKey) && !checkClipboardCode(objEvent, strKey)) 
	{
		//alert("Invalid Character Detected!\nKeyCode = " + iKeyCode + "\nCharacter =" + strKey);
		return false;
	}
	return true;
}

function checkClipboardCode(objEvent, strKey) 
{
	if (isNS6)
		return objEvent.ctrlKey && reClipboardChars.test(strKey);
	else
		return false;
}

function iCharLimiter(e,o)
{
	var rxCharMovement = /[\x00\x03\x08\x0D\x16\x18\x1A\x21-\x28\x2E]/;

	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which;

	o = df.gid(o);
	oDest = df.gid(o.getAttribute('charcounter')) || null;

	//df.gid('btnAlbumNew').value = rxCharMovement.test( String.fromCharCode(code) );

	if (oDest)
	{
		//yukk
		if (! o.getAttribute('isonkeyup'))
		{
			o.setAttribute('isonkeyup',1)
			o.observe('keyup', function(){ oDest.innerHTML = parseInt(o.getAttribute('charlimit')) - o.value.length; }.bind(this) );
		}
	} //if
	
	if 
	( 
	o.value.length >= parseInt(o.getAttribute('charlimit'))	&&
	!String.fromCharCode(code).match(rxCharMovement)
	)
	{
		return false;
	} //if
} //function

