/* LibSick php 0.2beta
Author: HtS
Site: http://www.netsick.org
------------------------------------
Some functions use libsick php & js
This script is licensed following GPL v3 license. */

//get object props
function getobjecttype(target){
	if (document.getElementById) {var object = document.getElementById(target);}
	else if (document.all) {var object = document.all[target];}
	else if (document.layers) {var object = document.layers[target];}
	return object;
}

var gmap_counter = 1;
//Gmaps loader (uses Gmaps API)
function gmapsload(loc,target,icon) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(getobjecttype(target));
		var geocoder = new GClientGeocoder();
		map.setUIToDefault();
		//map.addControl(new GSmallMapControl());
		geocoder.getLatLng(loc, function(point) {
			if (!point) {
				alert(loc + " not found");
			}
			else {
				map.setCenter(point, 16);
				// Create our "tiny" marker icon
				var customIcon = []
				customIcon[0] = new GIcon();
				customIcon[0].image = "dot.png";
				customIcon[0].iconSize = new GSize(20, 20);
				customIcon[0].iconAnchor = new GPoint(10, 10);
				customIcon[0].infoWindowAnchor = new GPoint(10, 10);

				customIcon[1] = new GIcon();
				customIcon[1].image = "dot2.png";
				customIcon[1].iconSize = new GSize(20, 20);
				customIcon[1].iconAnchor = new GPoint(10, 10);
				customIcon[1].infoWindowAnchor = new GPoint(10, 10);
				
				if (!icon) {var selIcon=customIcon[0]}
				else {var selIcon=customIcon[icon];}
				var marker = new GMarker(point,selIcon);
				map.addOverlay(marker);
				gmap_counter++;
				var myHtml = "<p style=\"font: 10px Verdana; overflow: auto; height: 75px;\" id=\"gmap_cont"+gmap_counter+"\"><a href=\"#load_me\" onclick=\"ajax('load.html','gmap_cont"+gmap_counter+"');\">yepp</a><br /><b>Localit&agrave; cercata:</b> <br />"+loc+"</p>";
				marker.openInfoWindowHtml(myHtml);
				GEvent.addListener(marker,"click", function() {
					marker.openInfoWindowHtml(myHtml);
				});
      return marker;

			}
		});
		//var customUI = map.getDefaultUI();
		//customUI.controls.maptypecontrol = false;
		//map.setUI(customUI);
	}  
}

//element toggler + state checker (used for interface buttons)
function toggle(target,options){
	/* force:none|block,state,animate:stile*/
	if (!options) {}
	else {
		optarr = options.split(",")
		for (var opti in optarr) {
			opt=optarr[opti].split(":")
		}
	}
	object=getobjecttype(target).style
	if (object.display == "none") {	object.display = object.display? "":"block" }
	else {object.display = "none"}
}

//UTILITIES
//chr function (http://kevin.vanzonneveld.net)
function chr(ascii) {
  return String.fromCharCode(ascii)
}
//array unique
function arrayUnique(a) {
  tmp = new Array(0)
  for(i=0;i<a.length;i++){
    if(!contains(tmp, a[i])){
      tmp.length+=1
      tmp[tmp.length-1]=a[i]
    }
  }
  return tmp
}
//is numeric function
function IsNumeric(sText){
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	  { 
	  Char = sText.charAt(i); 
	  if (ValidChars.indexOf(Char) == -1) 
		 {
		 IsNumber = false;
		 }
	  }
	return IsNumber;
}
//find a string object in array
function inArray(reqArray,reqStr) {
	for (i=0; i<reqArray.length; i++) {
		if (reqArray[i]==reqStr) {
			var found = true;
		}
		else {} 
	}
	if (found) {return true;}
}

//remove items from an array
function removeItems(array, item) {
	for (var i in array){
		if (i==item) {
			array[i].splice(0,2)
		}
	}
}

//remove object from array
function arrayRemove(reqArray,reqStr) {
	for (i=0; i<reqArray.length; i++) {
		if (reqArray[i]==reqStr) {
			reqArray.splice(i,1);
		}
	}
	return reqArray;
}









/*Tabber*/
function changeTab(id){
	document.getElementById(id).className = 'active'
	for (i=1;i<=25;i++){
		if(id == "asd_"+i+""){}
		else{
			if (document.getElementById("asd_"+i)){
				document.getElementById("asd_"+i).className = 'unactive'
			}
			else{}
		}
	}
}

/* Slider v0.3*/
function slideIt(mainCont,elemCont,buttonCont){
	if (!elemCont || !mainCont) {
		alert('slideIt v0.31\n\nYou have to specify a valid container element');
		false;
	}
	//declaring elements
	this.btnCont = buttonCont;
	this.mainCont = mainCont;
	this.elemCont = elemCont;
	this.mainObj = getobjecttype(this.mainCont);
	this.elemObj = getobjecttype(this.elemCont);
	this.mainWidth = getobjecttype(this.mainCont).offsetWidth;	
	this.mainHeight = getobjecttype(this.mainCont).offsetHeight;
	//css styles & control elements
	this.cntNext = '';
	this.cntPrev = '';
	this.prevActive = '';
	this.prevUnactive = '';
	this.nextActive = '';
	this.nextUnactive = '';
	//options
	this.activeElem = 0;
	this.scrollSpeed = 30;
	//arrays
	this.activeLoops = new Array();
	this.slideObjs = new Array();
	this.btnObjs = new Array();
	var __this = this;
	this.slideInit = function(){
		var buttonNodes = getobjecttype(this.btnCont).childNodes;
		for (var c=0; c <=buttonNodes.length-1; c++ ) {
			if (buttonNodes[c].tagName == "IMG") {
				this.btnObjs.push(buttonNodes[c].id);
				getobjecttype(buttonNodes[c].id).style.cursor = 'pointer';
			}
		}
		var contNodes = this.elemObj.childNodes;
		for (var n=0; n <=contNodes.length-1; n++ ) {
			if (contNodes[n].tagName == "DIV") {
				this.slideObjs.push(contNodes[n].id);
				contNodes[n].style.width = this.mainWidth+'px';
				contNodes[n].style.height = this.mainHeight+'px';
			}
		}
		this.elemObj.style.width = this.mainObj.offsetWidth*(this.slideObjs.length-1)+'px';
		this.jump(0);
	}
	this.next = function(loopStop){
		if (!loopStop){
			this.clearLoops();
		}
		if (this.slideObjs[this.activeElem+1]) {
			this.active(this.activeElem+1);
			clearTimeout(this.activeScroll);
			this.scroll(this.mainWidth*this.activeElem,30);
			this.controls();
		}
	}
	this.previous = function(loopStop){
		if (!loopStop){
			this.clearLoops();
		}
		if (this.slideObjs[this.activeElem-1]) {
			this.active(this.activeElem-1);
			clearTimeout(this.activeScroll);
			this.scroll(this.mainWidth*this.activeElem,30);
			this.controls();
		}
	}
	this.active = function(req){
		this.activeElem = req;
		for (n = 0; n<=this.btnObjs.length-1; n++) {
			var object = getobjecttype(this.btnObjs[n]).style;
			if (n==req) {
				object.opacity = (1); 
				object.MozOpacity = (1); 
				object.KhtmlOpacity = (1); 
				object.MsFilter = '"progid:DXImageTransform.Microsoft.Alpha(Opacity='+(100)+')";';
				object.filter = "alpha(opacity="+(100)+");";
				object.border = '2px solid #d4000a';
			}
			else {
				
				object.opacity = (.50); 
				object.MozOpacity = (.50); 
				object.KhtmlOpacity = (.50); 
				object.MsFilter = '"progid:DXImageTransform.Microsoft.Alpha(Opacity='+(50)+')";';
				object.filter = "alpha(opacity="+(50)+");";
				object.border = '0px';
			}
		}
	}
	this.jump = function(target){
		clearTimeout(this.activeScroll);
		this.clearLoops();
		this.active(target);
		this.scroll(this.mainWidth*this.activeElem,5);
		this.controls();
	}
	this.scroll = function(point,speed){
		if (!speed) {}
		else {this.scrollSpeed = speed;}
		if (this.mainObj.scrollLeft < point){
			this.mainObj.scrollLeft += this.mainWidth/10;
		}
		else if (this.mainObj.scrollLeft > point) {
			this.mainObj.scrollLeft -= this.mainWidth/10;
		}
		if (this.mainObj.scrollLeft!=point) {
			this.activeScroll = setTimeout(function (){__this.scroll(point);},this.scrollSpeed );
		}
		else {}
	}
	this.controls = function(){
		if (!this.slideObjs[this.activeElem-1]) {getobjecttype(this.cntPrev).src = this.prevUnactive;}
		else {getobjecttype(this.cntPrev).src = this.prevActive;}
		if (!this.slideObjs[this.activeElem+1]) {getobjecttype(this.cntNext).src = this.nextUnactive;}
		else {getobjecttype(this.cntNext).src = this.nextActive;}
	}
	this.loop = function(){
		var timer=0;
		for (i=1; i<=this.slideObjs.length-1; i++) {
			timer+=4000
			this.activeLoops.push(setTimeout(function(){__this.next('avoids_loop_stopping');},timer));
			if (i == this.slideObjs.length-1){
				this.activeLoops.push(setTimeout(function(){__this.jump(0);},timer+2000));
			}
		}		
	}
	this.clearLoops = function(){
		if (this.activeLoops.length!=0) {
			for (var i = 0; i<=this.activeLoops.length-1; i++) {
				clearTimeout(this.activeLoops[i]);
			}
		}
	}
}

/* Slider v0.2 */
divSlider = function(activeset,action,ids,looper){
	this.set = activeset[2]
	this.set_divs = activeset[3]
	this.set_obj = activeset[0]
	this.set_btm = activeset[1]
	nextControl = document.getElementById("next_div");  		
	prevControl = document.getElementById("prev_div");
	if (looper){loop_interrupt=true}
	if (action==="next") {divSlider.next()}
	if (action==="previous") {divSlider.previous()}
	if (action==="open") {divSlider.open(ids)}
}

divSlider.next = function(){
	activeid=activeDiv.replace(/\D/g, '')
	activeid=set_obj+(parseInt(activeid)+1)
	if (activeDiv===set_divs[set_divs.length-1]){}
	else {
		this.hide(activeDiv)
		this.show(activeid)
		this.controls()
	}
}

divSlider.previous = function(){
	activeid=activeDiv.replace(/\D/g, '')
	activeid=set_obj+(parseInt(activeid)-1)
	if (activeDiv===set_divs[0]){}
	else{
		this.hide(activeDiv)
		this.show(activeid)
		this.controls()
	}
}

divSlider.active = function(act) {
	activeDiv=act
}

divSlider.show = function(elem){
	this.active(elem);
	var perc2 = parseInt(document.getElementById(elem).style.width)
	perc2 += 5;
	document.getElementById(elem).style.display = "block";
	document.getElementById(elem).style.width = perc2 + "%";
	if( perc2 < 100 ){
		setTimeout( "divSlider.show('"+elem+"');",5 );
	}
}

divSlider.hide = function(elem){
	var perc = parseInt(document.getElementById(elem).style.width)
	perc -= 5;
	document.getElementById(elem).style.width = perc + "%";
	if( perc > 0 ){
		setTimeout( "divSlider.hide('"+elem+"');",5 );
	}
	else {
		document.getElementById(elem).style.display = "none";
	}
}

divSlider.open = function(id){
	if (activeDiv===id){}
	else {
		this.hide(activeDiv)
		this.show(id)
		this.controls()
	}
}

divSlider.controls = function(){
	//Side buttons
	if (activeDiv===set_divs[0]) {
		prevControl.src = "system/gui/left_unactive.png"
		nextControl.src = "system/gui/right_active.png"
	}
	else if (activeDiv===set_divs[set_divs.length-1]) {
		nextControl.src = "system/gui/right_unactive.png"
		prevControl.src =  "system/gui/left_active.png"
	}
	else {
		nextControl.src = "system/gui/right_active.png"
		prevControl.src =  "system/gui/left_active.png"
	}
	//Bottom controls
	bottomid=(activeDiv.replace("p", ""))
	var i=1
	for (i=1;i<=set_divs.length;i++) {
		document.getElementById(set_btm+i).style.border = "0px"
	}
	document.getElementById(set_btm+bottomid).style.border = "2px solid #d4000a"
}
var loop_interrupt=false
function slideLoop(loopset,looplength) {
	loopinterrupt = function(loopset){
		if(loop_interrupt){}
		else {
			divSlider(loopset,'next','');
		}
	}
	var i=1;
	var timer=0;
	for (i=1; i<=looplength-1; i++) {
		timer+=4000
		setTimeout("loopinterrupt("+loopset+");",timer);
	}
}

function center(offx,offy){
	return mleft = new Array(""+(document.body.clientWidth/2)-(offx/2)+"",""+(window.innerHeight/2)-(offy/2)+"");
}

