/*-------------------------------------------------------------*/
/* Copyright 2008 42 Pixel b.v.							       */
/* Progammierung: Leonhard Kronast                             */
/*-------------------------------------------------------------*/

/* Globale Variablen */

var gaydgets = [];
var px = 'px';
var overimg;

var isIE = (navigator.userAgent.indexOf('MSIE') > -1);
var isSafari = (navigator.userAgent.indexOf('Safari') > -1) || (navigator.userAgent.indexOf('iPhone') > -1) || (navigator.userAgent.indexOf('iPod') > -1)
var pageID = self.location.href.indexOf('/start') > -1 ? 'StartPage' : null;
var isAni = (top.grnav && top.grnav.isAni) ? true : false;
var borderImg = new Image();
if (isIE && navigator.userAgent.toLowerCase().indexOf('msie 6.') > -1)
    borderImg.src = '/images/pix.gif';
else
    borderImg.src = '/art/royal/images/icons/iconborder.png';

function showImg(which){
	var w,h,td,ld;
	if( which != null ){
		overimg.width  = 75;
		overimg.src           = which
		overimg.style.display = 'block';
		overimg.visible = true;
		top.addEvent(overimg, 'error', hideImg);
	}
	else{
	    hideImg();
	}
}

function hideImg() {
    top.removeEvent(overimg, 'error', hideImg);
    overimg.visible = false;
	overimg.style.display = 'none';
}

function pos(obj){
	//gibt Positionsangaben eines übergebenen Objekts zurück
	var xPos = 0;
	var yPos = 0;
	var parentObj = obj;
	while(parentObj != null){
		yPos     += parseInt(parentObj.offsetTop,10);
		xPos     += parseInt(parentObj.offsetLeft,10);
		parentObj = parentObj.offsetParent;
	}
	return {x:xPos,y:yPos};
}
function $_(objname){
	return ( document.getElementById(objname) ) ? document.getElementById(objname) : false;
}

function gaydget(el, nr) {
    top.addEvent(el, 'click', MCE);
		this.nr = nr;
		this.id = el.id;
		this.el = el;
		top.addEvent(el.firstChild,'mouseover',IconToggle);
		top.addEvent(el.firstChild,'mouseout',IconToggle);
		this.parent = el.parentNode;
		this.marginTop = 0;
		this.hiddenHeight = 48;
		this.visibleHeight = 0;
		this.srcUrl = el.getAttribute('srcUrl') == '' ? null : el.getAttribute('srcUrl');
		this.closed = true;
		for (var i= 0;i<el.getElementsByTagName('DIV').length;i++){
			if (el.getElementsByTagName('DIV')[i].className == 'gaydgetHidden'){
				this.hiddenContent = el.getElementsByTagName('DIV')[i];
				}
			if (el.getElementsByTagName('DIV')[i].className == 'gaydgetVisible')
				this.visibleContent = el.getElementsByTagName('DIV')[i];
		}
		this.loaded = (this.visibleContent.firstChild.innerHTML!='');
		this.checkSize = function(){
			var aktHeight = Math.ceil((this.visibleContent.scrollHeight)/56) * 56;
			return aktHeight;
		}
		this.maxHeight = 0;
		this.motion = null;
		this.intoView = true;
		this.subTitle = this.hiddenContent.firstChild.innerHTML;
		this.loadRSS = function() {
		    if (this.srcUrl == null) {
		        this.slide();
		        return;
		    }
		    if (this.loaded && !this.closed)
		        this.slide();
		    this.loaded = false;
		    this.hiddenContent.firstChild.innerHTML = '<p>' + waitText + '</p>';
		    this.maxHeight = 0;
		    if (this.srcUrl.indexOf('lg=') == -1) {
		        this.srcUrl = this.srcUrl + (this.srcUrl.indexOf('?') > -1 ? '&lg=' : '?lg=') + top.grnav.uGUID;
		    }
		    if (this.srcUrl.indexOf('gaydgets/getRss.asp') > -1) {
		        top.addEvent(document, 'mousemove', MME);
		    }
	        this.visibleContent.style.textAlign = 'center';
	        this.visibleContent.firstChild.innerHTML = waitImg;
	        sendRequest(this.srcUrl, gaydgets[nr].getLoad);
		}
		this.scrollIntoView = function(){

		}	
		this.getLoad = function() {
			var cont = document.createElement('DIV');
			cont.innerHTML = arguments[0];
			while (cont.hasChildNodes() && !cont.firstChild.hasChildNodes())
				cont.removeChild(cont.firstChild);
			for (var i = 0;i < cont.firstChild.childNodes.length;i++){
				if (cont.firstChild.childNodes[i].className == 'GRRSSTitle'){
					gaydgets[nr].hiddenContent.firstChild.innerHTML = gaydgets[nr].subTitle; 
				} 		
				if (cont.firstChild.childNodes[i].className == 'GRRSSText'){
					gaydgets[nr].visibleContent.style.textAlign = '';
					gaydgets[nr].visibleContent.firstChild.innerHTML = cont.firstChild.childNodes[i].innerHTML;
				}
				if (cont.firstChild.childNodes[i].className == 'GRRSSScript'){
					eval(cont.firstChild.childNodes[i].innerHTML);
				}
				
			}
			gaydgets[nr].loaded = true;
			gaydgets[nr].closed = true;
			gaydgets[nr].slide();
			};
		this.slide = function() {
			    if (this.motion != null)
			        window.clearTimeout(this.motion);
			    if (!this.loaded && this.srcUrl != null) {
			        this.loadRSS();
			        return;
			    }
			    if (this.maxHeight == 0) {
			        this.visibleContent.style.visibility = 'hidden';
			        this.visibleContent.style.display = 'block';
			        this.maxHeight = Math.ceil((this.visibleContent.scrollHeight) / 48) * 48;
			        this.visibleContent.style.overflow = 'hidden';
			        this.visibleContent.style.height = '24px';
			        this.visibleContent.style.visibility = 'visible';
			    }
			    this.maxHeight = this.maxHeight < 72 ? 72 : this.maxHeight;
			    var Timeout = 2;
			    var step = this.maxHeight / 7;
			    if (this.closed) {
			        var aktPos = pos(this.el)
			        if (this.intoView && (aktPos.y - document.body.scrollTop) < 0)
			            window.scrollTo(0, aktPos.y - 24);
			        step *= (1 - this.visibleHeight / this.maxHeight);
			        step = Math.ceil(step);
			        while ((step - 2) > (this.maxHeight - this.visibleHeight))
			            step--;
			        if (this.marginTop == 0) {
			            for (var i = 0; i < gaydgets.length; i++) {
			                if ((i != this.nr) &&
							(gaydgets[i].parent == this.parent) &&
							(!gaydgets[i].closed)) {
			                    gaydgets[i].slide();
			                }
			            }
			            this.hiddenContent.style.display = 'none';
			            this.visibleContent.style.display = 'block';
			        }
			        if (!isAni) {
			            this.visibleHeight = this.maxHeight;
			            this.visibleContent.style.height = this.maxHeight + 'px';
			            this.hiddenContent.style.display = 'none';
			            this.visibleContent.style.display = 'block';
			            this.marginTop = 24;
			            this.el.style.marginTop = '24px';
			            this.Icon.style.marginTop = '-20px';
			            this.el.style.height = (24 + this.visibleHeight) + 'px';
			            if (!isSafari)
			                this.visibleContent.style.overflow = 'auto';
			            this.motion = null;
			            fixOverflow();
			            if (pageID != null)
			                top[pageID + this.parent.className] = this.nr;
			            this.closed = false;
			            return;
			        }
			        if (this.marginTop <= 24) {
			            this.marginTop += 2;
			            this.marginTop = this.marginTop > 24 ? 24 : this.marginTop;
			            this.el.style.marginTop = this.marginTop + 'px';
			            this.Icon.style.marginTop = 4 - this.marginTop + 'px';
			        } else {
			            Timeout -= 1;
			        }
			        if (this.visibleHeight <= this.maxHeight) {
			            this.visibleHeight += step;
			            this.visibleHeight = this.visibleHeight > this.maxHeight ? this.maxHeight : this.visibleHeight;
			            if (this.visibleHeight >= 24) {
			                this.visibleContent.style.height = this.visibleHeight + 'px';
			            }
			            if (this.visibleHeight > 48) {
			                this.el.style.height = (24 + this.visibleHeight) + 'px';
			            }
			        } else {
			            if (!isSafari)
			                this.visibleContent.style.overflow = 'auto';
			            Timeout -= 1;
			        }
			        if (Timeout > 0 && step > 0) {
			            this.motion = setTimeout('gaydgets[' + this.nr + '].slide()', 1);
			        } else {
			            this.motion = null;
			            fixOverflow();
			            if (pageID != null) {
			                top[pageID + this.parent.className] = this.nr;
			            }
			            this.closed = false;
			        }
			    } else {
			        step *= (this.visibleHeight / this.maxHeight);
			        step = Math.ceil(step);
			        while (step - 1 > this.visibleHeight)
			            step--;
			        if (!isAni) {
			            this.visibleHeight = this.maxHeight;
			            this.visibleContent.style.height = this.maxHeight + 'px';
			            this.hiddenContent.style.display = 'block';
			            this.visibleContent.style.display = 'none';
			            this.marginTop = 0;
			            this.el.style.marginTop = '0px';
			            this.Icon.style.marginTop = '4px';
			            this.el.style.height = '48px';
			            this.motion = null;
			            fixOverflow();
			            if ((pageID != null) && (top[pageID + this.parent.className] == this.nr))
			                top[pageID + this.parent.className] = -1;
			            this.closed = true;
			            return;
			        }
			        if (this.marginTop > 0) {
			            this.marginTop -= 2;
			            this.marginTop = this.marginTop < 0 ? 0 : this.marginTop;
			            this.el.style.marginTop = this.marginTop + 'px';
			            this.Icon.style.marginTop = 4 - this.marginTop + 'px';
			        } else {
			            Timeout -= 1;
			        }
			        if (this.visibleHeight > 24) {
			            if (!isSafari)
			                this.visibleContent.style.overflow = 'hidden';
			            this.visibleHeight -= step;
			            this.visibleHeight = this.visibleHeight < 24 ? 24 : this.visibleHeight;
			            if (this.visibleHeight >= 24)
			                this.visibleContent.style.height = this.visibleHeight + 'px';
			            this.el.style.height = (24 + this.visibleHeight) + 'px';
			        } else {
			            this.el.style.height = '48px';
			            Timeout -= 1;
			        }
			        if (Timeout > 0 && step > 0)
			            this.motion = setTimeout('gaydgets[' + this.nr + '].slide()', Timeout);
			        else {
			            this.motion = null;
			            this.hiddenContent.style.display = 'block';
			            this.visibleContent.style.display = 'none';
			            fixOverflow();
			            if ((pageID != null) && (top[pageID + this.parent.className] == this.nr))
			                top[pageID + this.parent.className] = -1;
			            this.closed = true;
			        }
			    }
			}
		this.Icon = el.getElementsByTagName('IMG')[0];
	}
	
	function fixOverflow(){
		if(typeof(document.body.style.overflowX) == 'undefined'){
			var wDIV = document.getElementsByTagName('DIV');
			var i = 0;
			if ((document.body.clientWidth - document.body.scrollWidth) < 0){
				for (i=0;i<wDIV.length;i++){
					if(wDIV[i].offsetWidth == 596 || wDIV[i].style.width == '596px' ){
						wDIV[i].style.width = '590px';
					}
					if (wDIV[i].offsetWidth == 334 || wDIV[i].style.width == '334px'){
						wDIV[i].style.width = '328px';
					}
				}
			} else {
				for (i=0;i<wDIV.length;i++){
					if(wDIV[i].style.width == '590px'){
						wDIV[i].style.width = '';
					}
					if (wDIV[i].style.width == '328px'){
						wDIV[i].style.width = '';
					}
				}
			}
		} 
	}
	

	
function MCE(ev){
    ev = ev || window.event;
	var target = ev.target || ev.srcElement;
	var i;
	if (target.nodeName == 'SPAN')
	    target = target.parentNode;
	if ((target.nodeName == 'IMG' && target.className == 'gaydgetIcon') ||
		(target.nodeName == 'P' && target.className == 'headerP')){
		for (i=0;i<gaydgets.length;i++){
			if ((target.nodeName == 'IMG'  && gaydgets[i].id == target.parentNode.id ) ||
				(target.nodeName == 'P' && gaydgets[i].id == target.parentNode.parentNode.parentNode.id)){
			    gaydgets[i].slide();
			    top.stopEvent(ev);
				break;
			}
		}
	} else {
		if (target.nodeName == 'IMG' && target.className == 'gaydgetReload'){
			for (i=0;i<gaydgets.length;i++){
				if (gaydgets[i].id == target.parentNode.parentNode.id){
				    gaydgets[i].loadRSS();
				    top.stopEvent(ev);
					break;
				}
			}
		}
    }
}

    function IconToggle(ev) {
        ev = ev || window.event;
        var target = ev.target || ev.srcElement;
        if (target.className == 'gaydgetIcon') {
            target.src = (target.src.indexOf('/images/pix.gif') > 0)?borderImg.src:'/images/pix.gif';         
            }
    }
	
	function WLE(){
	    top.removeEvent(this,'load',WLE);
		var cont = document.getElementsByTagName('DIV');
		var i=0,j=0,found = 0,openID = -2;
		var aktArea = '';
		overimg = $_('overimg');
		overimg.visible = false;
		for (i=0;i<cont.length;i++){
			if (cont[i].className == 'gaydget'){
				if (gaydgets.push){
					gaydgets.push(new gaydget(cont[i],found));
				} else {
					gaydgets[found] = new gaydget(cont[i],found);
				}
				found++;
            }
            if ((cont[i].className.indexOf('Col') == cont[i].className.length - 3) && cont[i].hasChildNodes()) {
                for (j = 0; j < cont[i].childNodes.length; j++) {
                    if (cont[i].childNodes[j].className == 'Teaser') {
                        setImageGaydgets(cont[i].childNodes[j]);
                    }
                }
            }
        }
		for (i=0;i<found;i++){
			if (aktArea != gaydgets[i].parent.className){
			    gaydgets[i].parent.style.display = 'block';
			    if (pageID != null) {
			        aktArea = gaydgets[i].parent.className;
			        openID = -2;
			        if (typeof top[pageID + aktArea] != 'undefined' &&
					!isNaN(top[pageID + aktArea])) {
			            openID = parseInt(top[pageID + aktArea], 10);
			        } else {
			            top[pageID + aktArea] = -1;
			        }
			    }
            }
            if (openID > -2) {
			    if (openID == i) {
			        gaydgets[i].slide();
			    }
			} else {
				if (gaydgets[i].el.getAttribute('isOpen') == "1")
					gaydgets[i].slide();
			}
		}
	}
	

	

	function MME(ev){
		if (overimg && overimg.visible){
			ev = ev || window.event;
			var myPos = mouseCoords(ev);
			overimg.style.top = 15 + myPos.y + px;
			overimg.style.left = 15 + myPos.x + px;

        }
	}

	function mouseCoords(ev){
		if(ev.pageX || ev.pageY){return {x:ev.pageX, y:ev.pageY};}
		return {x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
				y:ev.clientY + document.body.scrollTop  - document.body.clientTop};
}

function hideDate(el) {
    el = el.firstChild;
    if (el.style.display == 'block')
        el.style.display = 'none';
    else
        el.style.display = 'block';
}

top.addEvent(this, 'load', WLE);

function changeVid(ad) {
    var i;
    for (i = 0; i < gaydgets.length; i++) {
        if (gaydgets[i].srcUrl != null && gaydgets[i].srcUrl.indexOf('FeaturedVideo') > -1) {
        	if (ad == 0)
                gaydgets[i].srcUrl = '/start/gaydgets/getgaydget.asp?action=FeaturedVideo&Adult=0';
            else
                gaydgets[i].srcUrl = '/start/gaydgets/getgaydget.asp?action=FeaturedVideo&Adult=1';
            gaydgets[i].loadRSS();
            break;
        }
    }
   }



function setImageGaydgets(container) {
    if (container.getAttribute('pos') != null) {
        var newPos = container.getAttribute('pos');
        newPos = 2 * (isNaN(newPos) ? 1 : parseInt(newPos, 10)) -1;
        if (newPos != 0) {
            for (var aktPos = 0; aktPos < container.parentNode.childNodes.length; aktPos++) {
                if (container.parentNode.childNodes[aktPos] == container)
                    break;
            }
            newPos = aktPos + newPos<0?0:aktPos + newPos;
            newPos = newPos > container.parentNode.childNodes.length - 1 ? container.parentNode.childNodes.length - 1 : newPos;
            if (container.parentNode.childNodes[newPos].className == 'gaydgetBottom')
                newPos++;           
            container.parentNode.insertBefore(container, container.parentNode.childNodes[newPos]);
        }
    }
    if (container.getElementsByTagName('IMG').length == 1) {
        var myImg = container.getElementsByTagName('IMG')[0];
        if (container.getElementsByTagName('OBJECT').length == 1) {
            myImg.style.display = 'none';
            myImg = container.getElementsByTagName('OBJECT')[0];
        }
        var height = Math.ceil(myImg.height / 24);
        container.style.height = (24*height + (isIE ? 0 : 8)) + 'px';
        myImg.style.margin = '0px';
        myImg.style.marginBottom = (isIE ? 8 : 0) + 'px';
        myImg.style.border = '0px none red';
    } else {
        container.style.display = 'none';
    }
   }

   function deleteFromList(userID) {
   	if ($_("uimkl" + userID).getAttribute('state') == "0") {
   		$_("uimk" + userID).style.position = "relative";
   		changeOpac(100, "uimk" + userID);
   		shiftOpacity("uimk" + userID, 200);
   		sendRequest('/start/gaydgets/peopleIKnow.scr.asp?s=0&UserID=' + userID, deletedFromList("uimkl" + userID));
   	}
   	else {
   		$_("uimk" + userID).style.position = "relative";
   		changeOpac(0, "uimk" + userID);
   		shiftOpacity("uimk" + userID, 200);
   		sendRequest('/start/gaydgets/peopleIKnow.scr.asp?s=1&UserID=' + userID, deletedFromList("uimkl" + userID));
   	}
}

function deletedFromList(userID, text) {
	if ($_(userID).getAttribute("state") == "0"){
		$_(userID).innerHTML = $_(userID).getAttribute("altText");
		$_(userID).setAttribute("state", "1", 0);
		$_(userID).blur();
	}
	else{
		$_(userID).innerHTML = $_(userID).getAttribute("defText");
		$_(userID).setAttribute("state","0",0);
		$_(userID).blur();
	}
}
var fader;
function opacity(id, opacStart, opacEnd, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	if (opacStart > opacEnd) {
		for (i = opacStart; i >= opacEnd; i--) {
			fader = setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
			timer++;
		}
	} else if (opacStart < opacEnd) {
		for (i = opacStart; i <= opacEnd; i++) {
			fader = setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
			timer++;
		}
	}
}

function changeOpac(iopacity, id) {
	clearTimeout(fader)
	if (document.getElementById(id)) {
		//document.getElementById("debug").innerHTML = opacity + " - " + id;;
		var object = $_(id).style;
		object.opacity = (iopacity / 100);
		object.MozOpacity = (iopacity / 100);
		object.KhtmlOpacity = (iopacity / 100);
		object.filter = "alpha(opacity=" + iopacity + ")";
	}
}
function shiftOpacity(id, millisec) {
	clearTimeout(fader);
	if ($_(id).style.opacity == 0) {
		opacity(id, 20, 100, millisec);
	} else {
		opacity(id, 100, 20, millisec);
	}
} 
