// enable the png fix for I6 & under.
 if (document.all && /MSIE (5\.5|6)/.test(navigator.userAgent) &&
  document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule)
 {
  // document.styleSheets[0].addRule('*', 'behavior: url(iepngfix.htc)');
  // Feel free to add rules for specific elements only, as above.
  // You have to call this once for each selector, like so:
  document.styleSheets[0].addRule('img', 'behavior: url(iepngfix.htc)');
  document.styleSheets[0].addRule('div', 'behavior: url(iepngfix.htc)');
 }


/////////// ALIASES //////////////////
function de(elementId) {
	return document.getElementById(elementId);
}
function sf(form, name, value) {
	form.elements[name].value = value;
}

// keyboard enter?
function kH(e) {
	var code;

	if (!e) var e = window.event;
	if (e.keyCode) { code = e.keyCode; }
	else if (e.which) { code = e.which; }

	return ((code == 13) || (code == 9));
}

function hideObj(objId) {
	var o = document.getElementById(objId);
	if (o != null) {
		o.style.display = "none";
	}
}
function showObj(objId) {
	var o = document.getElementById(objId);
	if (o != null) {
		o.style.display = "block";
	}
}
function setClass(objId, className) {
	var o = document.getElementById(objId);
	if (o != null) {
		o.className = className;
	}
}

var _debug_ajax = false;
var _ajax_bad_chars = "<>&'\"";
var _ajax_bad_chars_html = "&lt; &gt; &amp; &#39; &quot;";
var _ajax_bad_chars_text = "< > & ' \"";

function AjaxHasBadChars(value) {
	for (var i = 0; i < _ajax_bad_chars.length; i++) {
		var c = _ajax_bad_chars.charAt(i);
		if (value.indexOf("" + c) >= 0) {
			return true;
		}
	}
	return false;
}

function AjaxGetBadChars() {
	return _ajax_bad_chars_html;
}

function AjaxGetBadChars2() {
	return _ajax_bad_chars_text;
}

// get the Ajax object (XmlHttp)
function GetXmlHttp() {
	var obj = null;
	try {
		obj = new ActiveXObject("Msxml2.XMLHTTP");
		return obj;
	} catch (e) {
		try {
			obj = new ActiveXObject("Microsoft.XMLHTTP");
			return obj;
		} catch (E) {
			// didn't work
		}
	}

	if (typeof XMLHttpRequest != 'undefined') {
		try {
			obj = new XMLHttpRequest();
			return obj;
		} catch (e) {
			// didn't work
		}
	}
	if (window.createRequest) {
		try {
			obj = window.createRequest();
			return obj;
		} catch (e) {
			// didn't work
		}
	}
	return obj;
}

function LoadXml(xmlString) {
	var xdoc;

	if (window.ActiveXObject && /Win/.test(navigator.userAgent)) {
		// IE code
		xdoc = new ActiveXObject("Microsoft.XMLDOM");
		xdoc.async = false;
		xdoc.loadXML(xmlString);
		return xdoc;
	} else if (document.implementation && document.implementation.createDocument) {
		// FF and Opera
		var parser = new DOMParser();
		xdoc = parser.parseFromString(xmlString, 'text/xml');
		return xdoc;
	} else {
		return null;
	}
}


function toDOM(HTMLstring) {
	var d = document.createElement('div');
	d.innerHTML = HTMLstring;
	var docFrag = document.createDocumentFragment();

	while (d.firstChild) {
		docFrag.appendChild(d.firstChild)
	};

	d.innerHTML = "";   // remove it

	return docFrag;
}


function AjaxRefreshAsync(formObj, divId) {
	// sniff form script and params
	var script = formObj.action;
	if (script == "") {
		script = document.location.href;
	}
	var httpreq = "";
	for (var i = 0; i < formObj.elements.length; i++) {
		var name = escape(formObj.elements[i].name);
		var value = escape(formObj.elements[i].value);
		if (name == "") {
			continue;
		}

		if (httpreq.length > 0) {
			httpreq += "&";
		}
		httpreq += name + "=" + value;
	}
	if (_debug_ajax) {
		alert("running script=" + script + "\nhttpreq=" + httpreq);
	}
	// get the ajax object
	var xmlhttp = GetXmlHttp();
	if (xmlhttp != null) {
		// post form
		xmlhttp.open("POST", script, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
					if (_debug_ajax) {
						alert("ok: text= " + xmlhttp.responseText);
					}
					var htmlString = xmlhttp.responseText;

					var dom = toDOM(htmlString);
					var htmlDiv1 = dom.getElementById(divId);
					var htmlDiv2 = document.getElementById(divId);
					if (htmlDiv1 != null && htmlDiv2) {
						htmlDiv2.innerHTML = htmlDiv1.innerHTML;
						if (_debug_ajax) alert('target DIV updated successfully');
					} else {
						if (_debug_ajax) alert('target DIV not found');
					}

				} else {
					if (_debug_ajax) {
						alert("error: " + xmlhttp.status);
					}
				}
			}
		}
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
		xmlhttp.send(httpreq);
	}
}

////////////////////////////// lvsys string object /////////////////////////
function lvsysStr() {
	// encode the quotes and newlines so that we can use the string in javascript
	this.encodeEntities = function(s) {
		if ( s == null ) return "";
		s = s.replace(/\'/g,"&#39;");
		s = s.replace(/\r\n/g,"<br/>");
		s = s.replace(/\n/g,"<br/>");
		return s;
	}
	// decodes the quotes and newlines so that we can use the string in javascript
	this.restoreEntities = function(s) {
		if ( s == null ) return "";
		s = s.replace(/&#39;/,"'");
		s = s.replace(/<br\/>/,"\n");
		return s;
	}
}
var lvsysStr = new lvsysStr();

////////////////////////////// lvsys object /////////////////////////
// this object is a singleton (only one static instance)
function lvsys() {
	
	this.imgIdArray = new Array();
	this.imgCycleIdArray = new Array();
	this.imgInterval = null;

	this.prepImg = function(sImageId) {
		this.imgIdArray.push(sImageId);
	}
	this.prepCycle = function(sCycleId, sCycleEffect, iTransitionInterval, iSpeed) {
		var o = new Object();
		o.sCycleId = sCycleId;
		o.sCycleEffect = sCycleEffect;
		o.iTransitionInterval = iTransitionInterval;
		o.iSpeed = iSpeed;
		this.imgCycleIdArray.push(o);
	}
	
	this.imgReady = function() {
		var bImgLoaded = true;

		for(var i = 0; i < this.imgIdArray.length; i++){
			if (document.getElementById(this.imgIdArray[i]).complete == false) {
				bImgLoaded = false;
			}
		}
		return bImgLoaded;
	}

	this.imgCheckStatus = function() {
		if (this.imgReady()) {
			// all images are ready.
			clearInterval(this.imgInterval);

			// start all galleries.
			for (var i = 0; i < this.imgCycleIdArray.length; i++) {
				$("#"+this.imgCycleIdArray[i].sCycleId).cycle({
					fx: this.imgCycleIdArray[i].sCycleEffect,
					speed: this.imgCycleIdArray[i].iTransitionInterval, 
					timeout: this.imgCycleIdArray[i].iSpeed  // still duration
					//delay: -2000,
					//startingSlide: 0
				});
			}
			
			// reset array
			this.imgCycleIdArray = new Array();
		}
	}
	
	
	this.ajaxPager = function(options)
	{
		$('#'+options['htmlID']+' > *').remove();
		$('#'+options['htmlID']).html('<div style="text-align:center;"><img src="admin/js/ajax-loader.gif" /></div>');
		$.post(     
				'index.php',      
		 				{     
		 					ajax: 			'1',    
		 					func: 			'ajaxPager',     
		 					plugin: 		options['plugin'],    
		 					pluginType: 	options['pluginType'],   
		 					pluginParams:	options['pluginParams'],
		 					pageno: 		options['pageno'],
		 					htmlID:			options['htmlID']
		 				},     
		 				function (data, textStatus)      
		 				{    
		 					if ( textStatus == 'success' ) {    
		 						//alert(data);
		 						//dom = toDOM(data);    
		 						var dom = toDOM(data);
		 						
		 						$('#'+options['htmlID']).html(dom);
		 						//dom.firstChild.innerHTML);    
		 						//$('#'+options['id']).attr('rel',$(data).attr('rel'));    
		 					}    
		 				},    
		 				'html'    
		 			);    
		
	}
	

	// method called when window is ready
	this.ready = function() {
		this.imgInterval = setInterval("lvsys.imgCheckStatus()", 1000);
	}

}

// load lvsys when the document is ready
var lvsys = new lvsys();
$(document).ready(function() {
	lvsys.ready();
});
