



var pluginSrfreecap = {

	newFreeCap : function () {

	    // loads new freeCap image
	    if(document.getElementById) {

		// extract image name from image source (i.e. cut off ?randomness)
		var theImage = $('tx_srfreecap_pi2_captcha_image');
		var parts = theImage.src.split("&");
		// add ?(random) to prevent browser/isp caching
		// parts[0] should be id=page_id
		// parts[1] should be L=sys_language_uid
		theImage.src = parts[0] + "&" + parts[1] + "&set=" + Math.round(Math.random()*100000);

	    } else {
		alert("Sorry, cannot autoreload new image\nSubmit the form and a new image will be loaded");
	    }
	}
}


var helper = {

	Version: '0.1',
	extKey: 'sfpHelper',

	hideElements : function (boxId, tagName) {

	    hideMe = new Array();
	    hideMe = $A($(boxId).getElementsByTagName(tagName)).each(function(e) {

		    if (e.style.visibility == '' || e.style.visibility == 'visible') {
			    e.style.visibility = 'hidden';
		    } else {
			    e.style.visibility = 'visible';
		    }

	    });
	},

	hideElementByClassName : function (container, className) {

		var overlaysOpen = document.getElementsByClassName(className,container);
		overlaysOpen.each(function(element) {
			element.setStyle ({
				display: 'none'
			})
		});

	},

	preloadImages : function(imglist) {
	    var imgs = new Array();
	    var count;
	    if (document.images) {
		for (count=0; count<imglist.length; count++) {
		    imgs[count]=new Image(); imgs[count].src=imglist[count];
		}
	    }
	},

	sfHover : function(idList, tagList) {

		var count;

		for (count=0; count<idList.length; count++) {

			if ($(idList[count])) {

				var sfEls = $(idList[count]).getElementsByTagName(tagList[count]);
				var ulSiblings = $(idList[count]).immediateDescendants();

				for (var i=0; i<sfEls.length; i++) {

					sfEls[i].onmouseover=function() {

						if(this.className.match('index') == 'index') {

							var liIndex = this.className.match(new RegExp('index_[0-9]')).toString().split('_')[1];
							var liCounter = 0;

							if(this.parentNode.id == 'mainNavigation') {

								ulSiblings.each(function(e){
									liCounter ++;
									if(liIndex == liCounter) {

										var rId = helper.uniqueId();
										e.id = 'liM'+rId;
										$('liM'+rId).addClassName('sfhover'+liIndex);
									}
								});
							}
						}

						var rId = helper.uniqueId();
						this.id = 'li'+rId;

						if (!$('li'+rId).hasClassName('sfhover')) {
							$('li'+rId).addClassName('sfhover');
							$('li'+rId).addClassName('sfhover0');
						}

					}

					sfEls[i].onmouseout=function() {

						if(this.className.match('index') == 'index') {
							var liIndex = this.className.match(new RegExp('index_[0-9]')).toString().split('_')[1];
						}

						$(this.id).removeClassName('sfhover');
						$(this.id).removeClassName('sfhover'+liIndex);
						$(this.id).removeClassName('sfhover0');
					}

				}


			}
		}


	},

	getUrlParams : function (url) {
		return url.split('?');
	},

	random : function(imgs) {
	    var imgsNum = 0;

	    imgs.each(function(imgs){
		imgsNum ++
	    });

	    var imgSelect = Math.round(Math.random()*(imgsNum-1));
	    var bgImage = imgs[imgSelect];

	    $('header').setStyle({
		backgroundImage: 'url('+bgImage+')'
	    });
	},

	mark : function(element,errorMsg,marked) {
		try{
		   var elm = $(element);
		    var eadvice = 'captcha-advice-required-' + element;
	    if(marked) {
		   advice = '<div class="validation-advice" id="'+ eadvice +'" style="display:none" >' + errorMsg + '</div>'
					    switch (elm.type.toLowerCase()) {
						    case 'checkbox':
						    case 'radio':
							    var p = elm.parentNode;
							    if(p) {
								    new Insertion.Bottom(p, advice);
							    } else {
								    new Insertion.After(elm, advice);
							    }
							    break;
						    default:
							    new Insertion.After(elm, advice);
					}
				    if(typeof Effect == 'undefined') {
					    $(eadvice).style.display = 'block';
				    } else {
					    new Effect.Appear($(eadvice), {duration : 1 });
				    }
			    elm.removeClassName('validation-passed');
			    elm.addClassName('validation-failed');
			    return false;
		    } else {
			    if($(eadvice) != null) $(eadvice).remove();
			    elm.removeClassName('validation-failed');
			    //elm.addClassName('validation-passed');
			    return true;
		    }
		    } catch(e) {
			    throw(e)
		    }
	    },


	//showloading
	loading : function (boxId, action) {

	    var ajaxLoader = $(boxId);

	    if (ajaxLoader) {

		 if (action == 'start') {
		    Effect.Appear (ajaxLoader);
		 } else {
		   ajaxLoader.hide();
		}

	    }
	},

	overlay : function (element,boxOverlay,boxParent,overlayClass) {

		//create a uniqe id
		var rId = this.uniqueId();

		if (boxOverlay.style.display == 'none') {

			boxOverlay.id = 'b'+rId;

			// creat a dynamic margin
			element.id = 'a'+rId;
			var aTagWidth = $('a'+rId).getWidth();

			boxOverlay.style.marginLeft = 20+aTagWidth+'px';

			helper.hideElementByClassName(boxParent,overlayClass);

			//close open overlayBoxes
			Effect.Appear(boxOverlay);

		}
	},

	//create a uniqe id
	uniqueId : function () {
		var d = new Date();
		var rId = Math.round(Math.random()*d);
		return rId;
	},

	cursor : function (element) {
		element.style.cursor='pointer';
	}

}

preloadImages = function(imglist) {
	var imgs = new Array();
	var count;
	if (document.images) {
		for (count=0; count<imglist.length; count++) {
			imgs[count]=new Image(); imgs[count].src=imglist[count];
		}
	}
}

sfHover = function(idList, tagList) {
	var count;
	for (count=0; count<idList.length; count++) {
		var sfEls = document.getElementById(idList[count]).getElementsByTagName(tagList[count]);
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=' sfhover';
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(' sfhover\\b'), '');
			}
		}
	}	
}

// Mouseover on homepage

mHover = function(idContainer, idOut, idOver) {
	document.getElementById(idContainer).onmouseover=function() {
		document.getElementById(idOut).style.display='none';
		document.getElementById(idOver).style.display='block';
		}
	document.getElementById(idContainer).onmouseout=function() {
		document.getElementById(idOut).style.display='block';
		document.getElementById(idOver).style.display='none';
		}
}

