/**
 * 
 */
function Article() {

	/* Article media functions */
    this.oTableNewItems = document.getElementById("newItems");
    this.noOfMedia = 0;
    this.noOfMediaUsed = 0;
    
    this.setNoOfMedia = function(noOfMedia, noOfMediaUsed) {
    	this.noOfMedia = parseInt(noOfMedia);
    	this.noOfMediaUsed = parseInt(noOfMediaUsed);

        var rowCount = this.oTableNewItems.rows.length;
    	if (this.noOfMedia <= rowCount + this.noOfMediaUsed) {
    		if (this.noOfMedia < rowCount + this.noOfMediaUsed) 
    			this.remoweItemRow(document.getElementById("remowe"));
        	setAddButton(true);
        }
    };
    
    this.addNewItemRow = function(oAddButton) {

        // Create row
        var oRow = util.insertRowToTable(this.oTableNewItems);

        // Create cells for the row
        var oCell0 = util.insertCellToRow(oRow);
        var oCell1 = util.insertCellToRow(oRow);

        // Add input text...
        var rowCount = this.oTableNewItems.rows.length;
        //var maxFileSize = document.getElementById("maxFileSize").value;
        //oCell1.appendChild(util.createHTMLElement(jsConst.INPUT, jsConst.HIDDEN, "MAX_FILE_SIZE", null, maxFileSize));
        oCell0.appendChild(util.createHTMLElement(jsConst.INPUT, jsConst.FILE, "mediaFileName" + rowCount));
        

        var oButton = util.createHTMLElement(jsConst.INPUT, jsConst.BUTTON, null, null, "remove", null, jsConst.BUTTON);
//        oButton.setAttribute(jsConst.ONCLICK, "objClub.remoweItemRow('oButton')"); 
        oButton.onclick = function() { objArticle.remoweItemRow(oButton); };
        oCell1.appendChild(oButton);
        
        // If no of media is reached then diable the add button 
        if (this.noOfMedia <= rowCount + this.noOfMediaUsed) {
        	setAddButton(true);
        }
    };

    this.remoweItemRow = function(oBtn) {
        var oRow = oBtn.parentNode.parentNode;
        this.oTableNewItems.deleteRow(oRow.rowIndex);
        
        setAddButton(false);
    };

    this.deletePhoto = function(articleId) {
    	
    	var mediaName = objSlideShow.getMediaName();
    	if (mediaName != -1) {
    		location.href = "/article/?do=private/modifyArticle&method=deleteMedia&mediaName=" + mediaName + "&articleId=" + articleId;
    	}
    };
    
    var setAddButton = function(disabled) {
        var oBtn = document.getElementById("add");
        oBtn.disabled = disabled;
    };
    
	this.deleteArticle = function(articleId) {
		
		if(!confirm("Do you realy want to delete this post!")) {
			return false;
		}
		location.href="/article/?do=private/su/modDelArticle&method=delete&articleId=" + articleId;
		return true;
	};
	
	this.submitForm = function() {
		objRTTitle.setText();
		objRTContent.setText();
		scrObj.submitForm('ArticleForm');
	};
}

/**
 * 
 */
function Club() {

	var chosenCity = "";
	var citySelHelpText = "";
	var other = null;
	
	this.init = function(helpText, chsCity, oth) {
		citySelHelpText = helpText;
		chosenCity = chsCity;
		other = oth;
	};
	
	var country, city, sport;
	this.initiateSelectLists = function(cry, cty, spt) {
		country = cry;
		city = cty;
		sport = spt;

		self.setTimeout(initSelectLists, 500);
	};
	var initSelectLists = function() {
		var inTheList = false;
		var oSelect = document.getElementById("country");
		var i = 0;
		for ( ; i < oSelect.options.length; i++) {
			if (oSelect.options[i].value == country) {
				inTheList = true;
				break;
			}
		}
		if (inTheList == false) {
			oSelect.selectedIndex = 2;
			objClub.showOrHideTextDiv("other", 'countryTextDiv');
		}
		
		inTheList = false;
		oSelect = document.getElementById("city");
		i = 0;
		for ( ; i < oSelect.options.length; i++) {
			if (oSelect.options[i].value == city) {
				inTheList = true;
				break;
			}
		}
		if (inTheList == false) {
			oSelect.selectedIndex = 2;
			objClub.showOrHideTextDiv("other", 'cityTextDiv');
		}
		
		inTheList = false;
		oSelect = document.getElementById("sport");
		i = 0;
		for ( ; i < oSelect.options.length; i++) {
			if (oSelect.options[i].value == sport) {
				inTheList = true;
				break;
			}
		}
		if (inTheList == false) {
			oSelect.selectedIndex = 2;
			objClub.showOrHideTextDiv("other", 'sportTextDiv');
		}
	};
	
	this.getCities = function(oSelect, textDivId) {

		var value = oSelect.options[oSelect.selectedIndex].value;
		if (value != "other") {
			// show the loading until response
			document.getElementById("loader").style.display = jsConst.INLINE;
			document.getElementById("loader").style.visibility = jsConst.VISIBLE;
			ajaxObj.executeAction('/ajaxController.php?do=clubs&IdCountry=' + value, null, readData);
		} else if (value == "other") {
			// Simulate the readData with no data to show options "add to the list in the city selest list" 
			readData(null);
		}
		if (textDivId != null) {
			this.showOrHideTextDiv(value, textDivId);
			// We show the textbox for the city also when we show the textbox for country
			this.showOrHideTextDiv(value, 'cityTextDiv');
		}
	};
	this.showOrHideTextDiv = function(optionValue, textDivId){
        var textDiv = document.getElementById(textDivId);
        
        if (optionValue == 'other'){
            textDiv.style.display = jsConst.BLOCK;
            textDiv.style.visibility = jsConst.VISIBLE;
        } 
        else {
            textDiv.style.display = jsConst.NONE;
            textDiv.style.visibility = jsConst.HIDDEN;
        }
	};
	
	var readData = function (oData) {
		var oSelect = document.getElementById("city");
		var oOption, oInner, selectedIndex = 2; //this index will mark the "add to the list" option
		
		oSelect.innerHTML = "";

		// Add first the help text
		oOption = document.createElement("option");
		oInner  = document.createTextNode(citySelHelpText);
		oOption.value = "";
		oOption.appendChild(oInner);
		oSelect.appendChild(oOption);

		// if other has been initiated then we want to do add these options
		if (other != null) {
			// Add the option "------------------"
			oOption = document.createElement("option");
			oInner  = document.createTextNode("---------------------------------");
			oOption.value = '';
			oOption.disabled = jsConst.DISABLED;
			oOption.appendChild(oInner);
			oSelect.appendChild(oOption);
			
			// Add the option "add to the list"
			oOption = document.createElement("option");
			oInner  = document.createTextNode(other);
			oOption.value = 'other';
			//alert(oOption.value);
			oOption.appendChild(oInner);
			oSelect.appendChild(oOption);
	
			// Add another option "------------------"
			oOption = document.createElement("option");
			oInner  = document.createTextNode("---------------------------------");
			oOption.value = '';
			oOption.disabled = jsConst.DISABLED;
			oOption.appendChild(oInner);
			oSelect.appendChild(oOption);
		}
		
		if (oData != null) {
			selectedIndex = chosenCity != "" ? 2 : 0; // if we have some data then set the index to "choose a city" option
			var nodes   = oData.getElementsByTagName("item");
			for (var i=0, c=nodes.length; i<c; i++) {
				oOption = document.createElement("option");
				oInner  = document.createTextNode(nodes[i].getAttribute("name"));
				oOption.value = nodes[i].getAttribute("id");
		
				if(chosenCity == nodes[i].getAttribute("name")){
					selectedIndex = other != null ? i + 4 : i + 1; // + 4 , because we have the help text and the add to the list text also
				}
					
				oOption.appendChild(oInner);
				oSelect.appendChild(oOption);
			}
		}
		oSelect.selectedIndex = selectedIndex;
		
		//oSelect.onchange = function() {objClub.showOrHideTextDiv(oSelect.options[oSelect.selectedIndex].value, "cityTextDiv");};
		
		// hide the loading now that the response is shown
		document.getElementById("loader").style.display = jsConst.NONE;
		document.getElementById("loader").style.visibility = jsConst.HIDDEN;
	};
	this.resetForm = function() {

		document.getElementById("country").selectedIndex = 0;
		document.getElementById("city").selectedIndex = 0;
		document.getElementById("sport").selectedIndex = 0;
		document.getElementById("clubName").value = "";
	};

	/* Club page functions */
    this.oTableNewItems = document.getElementById("newItems");
    this.noOfMedia = 0;
    this.noOfMediaUsed = 0;
    
    this.setNoOfMedia = function(noOfMedia, noOfMediaUsed) {
    	this.noOfMedia = noOfMedia;
    	this.noOfMediaUsed = noOfMediaUsed;

        var rowCount = this.oTableNewItems.rows.length;
    	if (this.noOfMedia <= rowCount + this.noOfMediaUsed) {
    		if (this.noOfMedia < rowCount + this.noOfMediaUsed) 
    			this.remoweItemRow(document.getElementById("remowe"));
        	setAddButton(true);
        }
    };
    
    this.addNewItemRow = function(oAddButton) {

        // Create row
        var oRow = util.insertRowToTable(this.oTableNewItems);

        // Create cells for the row
        var oCell0 = util.insertCellToRow(oRow);
        var oCell1 = util.insertCellToRow(oRow);

        // Add input text...
        var rowCount = this.oTableNewItems.rows.length;
        oCell0.appendChild(util.createHTMLElement(jsConst.INPUT, jsConst.FILE, "mediaFileName" + rowCount));
        

        var oButton = util.createHTMLElement(jsConst.INPUT, jsConst.BUTTON, null, null, "remove", null, jsConst.BUTTON);
        oButton.onclick = function() { objClub.remoweItemRow(oButton); };
        oCell1.appendChild(oButton);
        
        // If no of media is reached then diable the add button 
        if (this.noOfMedia <= rowCount + this.noOfMediaUsed) {
        	setAddButton(true);
        }
    };
    
    this.goToClub = function(oSelect) {
    	
    	window.location = "#" + $(oSelect).val();
    };

    this.remoweItemRow = function(oBtn) {
        var oRow = oBtn.parentNode.parentNode;
        this.oTableNewItems.deleteRow(oRow.rowIndex);
        
        setAddButton(false);
    };

    this.deletePhoto = function(clubId) {
    	
    	var mediaName = objSlideShow.getMediaName();
    	if (mediaName != -1) {
    		location.href = "/club/?do=private/modifyClubPage&method=deleteMedia&mediaName=" + mediaName + "&clubId=" + clubId;
    	}
    };
    
    var setAddButton = function(disabled) {
        var oBtn = document.getElementById("add");
        oBtn.disabled = disabled;
    };
    
    this.addCityToDB = function(formId) {
    	document.getElementById(formId).action += "&method=addCity";
    	scrObj.submitForm(formId);
		return false;
    };
    
    this.addSportToDB = function(formId) {
    	document.getElementById(formId).action += "&method=addSport";
    	scrObj.submitForm(formId);
		return false;
    };

	this.deleteClub = function(clubId) {
		
		if(!confirm("Do you realy want to delete this post!")) {
			return false;
		}
		location.href="/club/?do=private/su/deleteClub&clubId=" + clubId;
		return true;
	};
	
	this.deleteClubPage = function(clubId) {
		
		if(!confirm("Do you realy want to delete this post!")) {
			return false;
		}
		location.href="/club/?do=private/su/deleteClubPage&clubId=" + clubId;
		return true;
	};
}

function Menu() {

	var menuLanguesDiv = null;
	var mldHidingTop = 0;
	var mldShowingTop = 0;
	var mldHidingLeft = 0;
	var mldShowingLeft = 0;
	var targetElement = null;

	var tlAnimator = new Animator();

	this.showLanguagesOnMenuBar = function(oDiv){

		targetElement = oDiv;
		if(menuLanguesDiv == null) {
			ajaxObj.executeAction('/ajaxController.php?do=languages', readData);
		} else {
			showDiv();
		}
	};
	
	this.hideLanguagesOnMenuBar = function(e){

		if (!e) {
            e = document.parentNode.event;
        }

		var el = e.target != null ? e.target : e.srcElement;

        if(el.id && (el.id == targetElement.id)) {
            return true;
        }
		menuObj.hideDiv();
	};
	
	var intervalId = null;
	this.hideLangueDiv = function(){
		if (intervalId != null)
			clearTimeout(intervalId);
		
		if (menuLanguesDiv != null)
			intervalId = setTimeout(menuObj.hideDiv, 600);
	};
	this.showLangueDiv = function(){
		if (intervalId != null)
			clearTimeout(intervalId);
		
//    	if (!e) var e = window.event;
//    		e.cancelBubble = true;
//    	if (e.stopPropagation) e.stopPropagation();
	};
	

	this.hideDiv = function(){
		
		tlAnimator.animateTop(mldHidingTop , 3);
		tlAnimator.fadeOut();
	    util._removeEventListener(document, 'click', menuObj.hideLanguagesOnMenuBar, false);
	    return true;
	};
	var showDiv = function(){
		tlAnimator.animateTop(mldShowingTop, 3);
		tlAnimator.fadeIn();
	    util._addEventListener(document, 'click', menuObj.hideLanguagesOnMenuBar, false);
	};
	
	var readData = function (oData) {

		menuLanguesDiv = util.createHTMLElement("div");
		menuLanguesDiv.id= "menuLanguesDiv";
		//menuLanguesDiv.onmouseout = menuObj.hideDiv;
		menuLanguesDiv.innerHTML=(oData);
		document.body.appendChild(menuLanguesDiv);

		mldShowingTop = util.getElementTop(targetElement) + 7;
		mldHidingTop = mldShowingTop - menuLanguesDiv.offsetHeight;
        mldHidingLeft = mldShowingLeft = util.getElementLeft(targetElement) + 2;
        
        menuLanguesDiv.style.left = mldShowingLeft + 'px';
        menuLanguesDiv.style.top  = mldHidingTop + 'px';
        
		tlAnimator.initialize(menuLanguesDiv,  mldHidingTop, null, mldHidingLeft, null, 0);
		showDiv();
	};
	
	var langue = 'en';
	this.changeLanguage = function(lang){
		langue = lang;
		tlAnimator.animateTop(mldHidingTop , 3);
		tlAnimator.fadeOut(null, null, setLanguage);
	    util._removeEventListener(document, 'click', menuObj.hideLanguagesOnMenuBar, false);
	};
	var setLanguage = function(){
		location.href = "/?do=home&lng=" + langue;
	};
}

/**
 * 
 */
function Video() {

	this.deleteVideo = function(videoId) {
		
		if(!confirm("Do you realy want to delete this post!")) {
			return false;
		}
		location.href="/video/?do=private/su/deleteVideo&videoId=" + videoId;
		return true;
	};
	
	this.submitForm = function() {
//		objRTTitle.setText();
//		objRTContent.setText();
		scrObj.submitForm('VideoForm');
	};
}
/**
 * 
 * 
 * @param slides
 * @param shownPath
 * @param slideTime
 * @return
 */
function SlideShow(slides, shownPath, slideTime) {
	
	var slides = slides;
	var index = 2;
	var shownPath = shownPath;
	var slideTime = (slideTime != null ? slideTime: 3000);
	
	var image = null;
	var shownOn =  document.getElementById("imgOn");
	var shownOff = document.getElementById("imgOff");
	
	var timerID = null;
	var intervalId = null;
	
	var menuDiv = null;
	var menuAnimator = new Animator();
	var menuAnimation = null;
	this.showMenu = function(oDiv) {

		menuDiv = oDiv;
		if (intervalId != null) {
			clearTimeout(intervalId);
			intervalId = null;
		}
		
		if (menuAnimation == null) {
			menuAnimator.initialize(menuDiv, null, null, null, null, 0);
		}
		menuAnimator.fadeIn();
		menuAnimation = true;
	};
	
	this.hideMenu = function(oDiv) {
		menuDiv = oDiv;
		intervalId = setTimeout(objSlideShow.hMenu, 600);
	};
	
	this.hMenu = function() {
		menuAnimator.fadeOut();
	};

	this.stopBubbling = function(e) {
			if (!e) {
				var e = window.event;
				e.cancelBubble = true;
			}
			if (e.stopPropagation) {
				e.stopPropagation();
			}
	};
	
	/**
	 * Gets the media name that is being shown
	 * 
	 * @return  the media name, empty string if no media is available.
	 */
	this.getMediaName = function() {
		
		if (slides.length == 0)
			return -1;
//		if (index > slides.length - 1)
//			index = 0;
//		return slides[index][1];
		return shownOn.src;
	};
	
	this.initiateShow = function(play) {
		
		document.getElementById("menu").style.opacity = 0;
		shownOn = document.getElementById("imgOn");
		shownOff = document.getElementById("imgOff");
		
		if (play) {
			this.play();
		}
	};
	
	this.play = function() {

		if (slides.length < 2)
			return false;
		if (timerID == null) { // Play
			timerID = self.setInterval(slideshow, slideTime);
			document.getElementById("play").id= "playACTIVE";
			if (document.getElementById("pauseACTIVE"))
				document.getElementById("pauseACTIVE").id= "pause";
		}
	};
	this.pause = function() {
		
		if(timerID != null) {

			self.clearInterval(timerID);
			timerID = null;
			document.getElementById("pause").id= "pauseACTIVE";
			if (document.getElementById("playACTIVE"))
				document.getElementById("playACTIVE").id= "play";
		}
	};
	var stopping = false;
	this.stop = function() {
		
		if(timerID != null)
			self.clearInterval(timerID);

		timerID = null;
		//index = 4;
		
		//location.replace("http://lcl.leadegrigo.com/?do=slide&show=Lea's born");;
		// Roll back minis.
		stopping = true;
		this.rewind(slides.length);
	};

	var count = 0;
	var forwarding = false;
	this.forward = function(cnt, play) {
		
		this.pause();
		forwardShownOff();
		if (play)
			this.play();
	};

//	var animate1 = new Animator();
//	var forwardSlides = function() {
//
//		if(count-- > 0 && index < slides.length) {
//			
//			image = document.getElementById("minis").firstChild;
//			animate1.initialize(image, null, null, null, 150, 100);
//			animate1.animateWidth(0, null, forwardMinis);
//		} else {
//			count=0;
//			forwarding = false;
//
//			var animate2 = new Animator();
//			animate2.initialize(shownOn, null, null, null, null, 100);
//			animate2.fadeOut(null);
//
//			var animate3 = new Animator();
//			shownOff.src = shownPath + slides[index - 4][0];
//			animate3.initialize(shownOff, null, null, null, null, 0);
//			animate3.fadeIn(forwardShownOff);
//		}
//	};

//	var forwardMinis = function() {
//
//		var div = util.createHTMLElement("div", null, null, null, null, null, "mini");
//		var newImage = util.createHTMLElement("img");
//		if(index >= slides.length){
//			index++;
//			newImage.src = "images/Lea/trans.png";
//		} else
//			newImage.src = miniPath + slides[index++];
//		div.appendChild(newImage);
//		
//		document.getElementById("minis").removeChild(image);
//		document.getElementById("minis").appendChild(div);
//		
//		forwardSlides();
//	};
	var forwardShownOn = function() {
		shownOn.src = shownOff.src;
		shownOn.style.display = jsConst.BLOCK;
		shownOn.style.visibility = jsConst.VISIBLE;
		if (navigator.appName.indexOf("Microsoft")!= -1 &&parseInt(navigator.appVersion)>=4) {
			shownOn.style.filter = 'alpha(opacity=100)';
		} else {//navigator.appName.indexOf("Netscape")!=-1 &&parseInt(navigator.appVersion)>=5)
			shownOn.style.opacity = 1;
		}
	};
	var forwardShownOff = function() {
		forwardShownOn();
		shownOff.style.visibility = jsConst.HIDDEN;
		if(index >= slides.length){
			index = 0;
//			self.clearInterval(timerID);
//			timerID = null;
		}
//		shownOff.src = shownPath + slides[index++][0];
		shownOff.src = shownPath + slides[index++];
	};

//	var countR = 0;
//	var rewinding = false;
	this.rewind = function(cnt, play) {
		this.pause();
		rewindShownOff();
		if (play)
			this.play();
	};

//	var animate1 = new Animator();
//	var rewindSlides = function() {
//
//		if(countR-- > 0 && index - 5 >= 0) {
//
//			var div = util.createHTMLElement("div", null, null, null, null, null, "mini");
//			var newImage = util.createHTMLElement("img");
//			if(index - 8 < 0){
//				index--;
//				newImage.src = "images/Lea/trans.png";
//			} else
//				newImage.src = miniPath + slides[index-- - 8];
//			div.appendChild(newImage);
//			div.style.width = "0px";
//			
//			document.getElementById("minis").removeChild(document.getElementById("minis").lastChild);
//			document.getElementById("minis").insertBefore(div, document.getElementById("minis").firstChild);
//
//			animate1.initialize(div, null, null, null, 0, 100);
//			if(stopping)
//				animate1.animateFastWidth(150, 10, rewindSlides);
//			else
//				animate1.animateWidth(150, null, rewindSlides);
//
//		} else {
//			countR=0;
//			rewinding = false;
//			stopping = false;
//
//			var animate2 = new Animator();
//			animate2.initialize(shownOn, null, null, null, null, 100);
//			animate2.fadeOut(null);
//
//			var animate3 = new Animator();
//			shownOff.src = shownPath + slides[index - 4];
//			animate3.initialize(shownOff, null, null, null, null, 0);
//			animate3.fadeIn(rewindShownOff);
//		}
//	};

//	var rewindMinis = function() {
//
//		var div = util.createHTMLElement("div", null, null, null, null, null, "mini");
//		var newImage = util.createHTMLElement("img");
//		if(index < 0){
//			index--;
//			newImage.src = "images/Lea/trans.png";
//		} else
//			newImage.src = miniPath + slides[index--];
//		div.appendChild(newImage);
//		
//		document.getElementById("minis").removeChild(image);
//		document.getElementById("minis").insertBefore(div, document.getElementById("minis").firstChild);
//		
//		rewindSlides();
//	};

	var rewindShownOn = function() {
		shownOn.src = shownOff.src;
		if (navigator.appName.indexOf("Microsoft")!= -1 &&parseInt(navigator.appVersion)>=4) {
			shownOn.style.filter = 'alpha(opacity=100)';
		} else {//navigator.appName.indexOf("Netscape")!=-1 &&parseInt(navigator.appVersion)>=5)
			shownOn.style.opacity = 1;
		}
	};
	var rewindShownOff = function() {
		rewindShownOn();
		if (--index < 0)
			index = slides.length -1;
		shownOff.style.visibility = jsConst.HIDDEN;
		shownOff.src = shownPath + slides[index];
//		shownOff.src = shownPath + slides[index][0];
	};
	
	var slideshow = function() {

		var animate2 = new Animator();
		animate2.initialize(shownOn, null, null, null, null, 100);
		animate2.fadeOut();

		var animate3 = new Animator();
		animate3.initialize(shownOff, null, null, null, null, 0);
		animate3.fadeIn(null, forwardShownOff);
	};
}

/**
 * 
 */
function RTELinkDiv () {
	
	this.objRichtext = null;
	this.linkSpan = null;
	this.animator = new Animator();
	
	this.showDiv = function(linkSpan, objRichtext) {
		this.objRichtext = objRichtext;
		this.linkSpan = linkSpan;
		this.animator.initialize(document.getElementById("RTELinkDiv"), null, null, null, null, 0);
		/* Fixe the sizes for the maow; top - 147, left - 2 */
		document.getElementById("RTELinkDiv").style.left = util.getElementLeft(linkSpan) - 4 + "px";
		document.getElementById("RTELinkDiv").style.top = util.getElementTop(linkSpan) - 147 + "px";
		document.getElementById("RTELinkDiv").style.display = jsConst.BLOCK;
		document.getElementById("RTELinkDiv").style.visibilty = jsConst.VISIBLE;
		this.animator.fadeIn();
		
		util._addEventListener(document, "click", this.closeDiv, false);
	};
	
	this.submitRTELinkForm = function(submit) {
		
		if (submit) {
			var url = document.getElementById("RTELinkForm").linkURL.value;
			if (url == "") {
				alert("Please type your URL");
			}
			if (url.indexOf("://") == -1) {
				url = $("#linkType").val() + url;
			}

			var text = document.getElementById("RTELinkForm").linkText.value;
			text = (text == "" ? url : text);
			
			this.objRichtext.createLinkWithText(text,
												url,
												document.getElementById("RTELinkForm").linkTitle.value);
		}
		util._removeEventListener(document, "click", rteLinkDiv.closeDiv, false);
		rteLinkDiv.animator.fadeOut(0, true);
		document.getElementById("RTELinkForm").linkType.selectedIndex = 1;
		document.getElementById("RTELinkForm").linkText.value = "";
		document.getElementById("RTELinkForm").linkURL.value = "http://";
		document.getElementById("RTELinkForm").linkTitle.value = "";
	};
	
	this.changeURL = function(linkType) {

		var link = document.getElementById("linkURL").value;

		if (link.indexOf("://") == -1) {
			link = $(linkType).val() + link;
		} else {
			var type = link.substr(0, link.indexOf("://")+3);
			link = link.replace(type, $(linkType).val());
		}
		document.getElementById("linkURL").value = link;
	};

    this.closeDiv = function(e) {

    	if (!e) {
            e = document.parentNode.event;
        }

        var el = e.target != null ? e.target : e.srcElement;
        
        // using rteLinkDiv object assumes that it is always used and that no other instances is used to run this method.
        if(el && (el == rteLinkDiv.linkSpan || el.id == "RTELinkDiv"
											|| el.id == "RTELinkFormFieldset"
        									|| el.id == "RTELinkForm"
            								|| el.id == "linkType"
            								|| el.className == "linkTypeOption"
                							|| el.id == "linkText"
                    						|| el.id == "linkURL"
                        					|| el.id == "linkTitle"
                        					|| el.id == "linkTypeLbl"
                            				|| el.id == "linkTextLbl"
                                			|| el.id == "linkURLLbl"
                                    		|| el.id == "linkTitleLbl"
            								|| el.id == "linkBtnRow"
                							|| el.id == "submitBtn"
                    						|| el.id == "cancelBtn")) {
            return true;
        }

		util._removeEventListener(document, "click", rteLinkDiv.closeDiv, false);
		rteLinkDiv.animator.fadeOut(0, true);	
		document.getElementById("RTELinkForm").linkType.selectedIndex = 1;
		document.getElementById("RTELinkForm").linkText.value = "";
		document.getElementById("RTELinkForm").linkURL.value = "http://";
		document.getElementById("RTELinkForm").linkTitle.value = "";
	};
	
	var hideDiv = function() {
		document.getElementById("RTELinkForm").linkType.selectedIndex = 1;
		document.getElementById("RTELinkForm").linkText.value = "";
		document.getElementById("RTELinkForm").linkURL.value = "http://";
		document.getElementById("RTELinkForm").linkTitle.value = "";
		
		document.getElementById("RTELinkDiv").style.top = "-300px";
		document.getElementById("RTELinkDiv").style.display = jsConst.NONE;
		document.getElementById("RTELinkDiv").style.visibilty = jsConst.HIDDEN;
	};
}
var rteLinkDiv = new RTELinkDiv();

/**
 * 
 * 
 * @param editor
 * @param targetTextareaId
 */
function RichText (editor, targetTextareaId) {
	
	var textEditor = editor;
	var targetTextareaId = targetTextareaId;
	
	this.initiate = function() {

		var html = document.getElementById(targetTextareaId).value;

		textEditor.document.designMode="on";
		textEditor.document.open();
		textEditor.document.write('<html><head></head><body>' + html + '</body></html>');
		textEditor.document.close();
		document.getElementById(targetTextareaId).style.visibilty = jsConst.HIDDEN;
		document.getElementById(targetTextareaId).style.display = jsConst.NONE;
	};
	
	this.createLink = function(linkSpan) {
		
		var link = "";
		if (textEditor.document.selection) { // IE
			link = textEditor.document.selection.createRange().text;
//			textEditor.document.execCommand("createLink", true, link);
			
//			range.parentElement().setAttribute("target", "_blank");
		} else {
			link = textEditor.document.getSelection();
		}
		
		/* Fix for Safari */
		link = "" + link;
		
		if (link != "") {
			if (link.indexOf("://") == -1) {
				link = "http://"+link;
			}
			textEditor.document.execCommand("createLink", false, link);
		} else {
			rteLinkDiv.showDiv(linkSpan, this);
//			alert("Please write your link and select the text.");
		}
	};

	this.createLinkWithText = function(text, link, title) {

//		var a = '<a href="'+link+'" title="'+title+'">'+text+'</a>';
//		var value = textEditor.document.body.innerHTML + '&nbsp;<a href="'+link+'" title="'+title+'">'+text+'</a>&nbsp;';
//		textEditor.document.designMode="on";
//		textEditor.document.open();
////		textEditor.document.write("&nbsp;");
//		textEditor.document.write(value);
////		textEditor.document.write(a);
////		textEditor.document.write("&nbsp;");
//		textEditor.document.close();
//		textEditor.document.execCommand("createLink", 0, a);
		textEditor.document.execCommand("inserthtml", false, '&nbsp;<a href="'+link+'" title="'+title+'">'+text+'</a>&nbsp;');
		textEditor.focus();
		
//		 var sSelection = this._oEditor._getSelection(); 
//		  if (sSelection == '') sSelection = ' '; 
//
//		  var sNewElt = '<div>' + sSelection + '</div>';
//
//		  this._oEditor.execCommand('inserthtml', sNewElt);
//
//		  var pos = 1000; //pos determines where to place the cursor. if greater than the length of characters, it will place at the end.
//		  if(this._oEditor.createTextRange) { //IE Specific code
//		        var range = this._oEditor.createTextRange();   
//		        range.move("character", pos);   
//		        range.select();   
//		    } else if(this._oEditor.selectionStart) {  //Works with Firefox and other browsers 
//
//		        this._oEditor.focus();   
//		        this._oEditor.setSelectionRange(pos, pos);   
//		  }  
//		  this._oEditor.focus(); 
	};
	
	this.removeLink = function() {
		
		var link = "";
		if (textEditor.document.selection) { // IE
			link = textEditor.document.selection.createRange().text;
//			range.parentElement().setAttribute("target", "_blank");
		} else {
			link = textEditor.document.getSelection();
		}

		if (link != "") {
//			if(link.indexOf("http://") != 0) {
//				link = "http://"+link;
//				textEditor.document.execCommand("Unlink", 0, link);
				textEditor.document.execCommand("Unlink", false, link);
//			}
		} else {
			alert("Please select link.");
		}
	};
	
	this.fontEdit = function(x,y) {
//		textEditor.document.execCommand(x,"",y);
		textEditor.document.execCommand(x,false,y);
		textEditor.focus();
	};
	
	this.commandState = function(c) {
		var state = document.queryCommandState(c);
        switch (state) {
        case true:
            alert ("The bold formatting will be removed from the selected text.");
            break;
        case false:
            alert ("The selected text will be displayed in bold.");
            break;
        case null:
            alert ("The state of the 'bold' command is indeterminable.");
            break;
        }

        textEditor.document.execCommand(c, false, null);

	};
	
	this.setText = function (){
		document.getElementById(targetTextareaId).value=textEditor.document.body.innerHTML;
	};
}

function SiteMap() {
	
	this.initiate = function() {
//		document.getElementById("boards").style.height = "0px";
//		$("#siteMap #SMboards ul").slideToggle();
		$("#siteMap .topics").click(function() {
			$(this).find("span").toggleClass("showerActive");
			$(this).find("ul").slideToggle();
		});
//		$("#siteMap #SMboards").slideToggle();
		$("#siteMap #showBoards").click(function() {
			$("#siteMap #showBoards").toggleClass("showerActive");
			$("#siteMap #SMboards").slideToggle();
		});
//		$("#siteMap #SMnews").slideToggle();
		$("#siteMap #showNews").click(function() {
			$("#siteMap #showNews").toggleClass("showerActive");
			$("#siteMap #SMnews").slideToggle();
		});
//		$("#siteMap #SMarticles").slideToggle();
		$("#siteMap #showArticles").click(function() {
			$("#siteMap #showArticles").toggleClass("showerActive");
			$("#siteMap #SMarticles").slideToggle();
		});
//		$("#siteMap #SMvideos").slideToggle();
		$("#siteMap #showVideos").click(function() {
			$("#siteMap #showVideos").toggleClass("showerActive");
			$("#siteMap #SMvideos").slideToggle();
		});
//		$("#siteMap #SMclubs").slideToggle();
		$("#siteMap #showClubs").click(function() {
			$("#siteMap #showClubs").toggleClass("showerActive");
			$("#siteMap #SMclubs").slideToggle();
		});
//		$("#siteMap #SMclubs ul").slideToggle();
		$("#siteMap .clubSports").click(function() {
			$(this).find("span").toggleClass("showerActive");
			$(this).find("ul").slideToggle();
		});
	};
}
