/* GLOBALS */

var browser;
var rollovers = 
{
	story : new Image( 115, 39 ),
	story_images :
	{
		over : "_repository/img/layout_r2_c3_ro.gif",
		out  : "_repository/img/layout_r2_c3.gif"
	},
	shoes : new Image( 73, 39 ),
	shoes_images :
	{
		over : "_repository/img/layout_r2_c4_ro.gif",
		out  : "_repository/img/layout_r2_c4.gif"
	},
	order : new Image( 92, 39 ),
	order_images :
	{
		over : "_repository/img/layout_r2_c6_ro.gif",
		out  : "_repository/img/layout_r2_c6.gif"
	},
	trader : new Image( 92, 39 ),
	trader_images :
	{
		over : "_repository/img/layout_r2_c7_ro.gif",
		out  : "_repository/img/layout_r2_c7.gif"
	},
	angebote : new Image( 89, 39 ),
	angebote_images :
	{
		over : "_repository/img/layout_r2_c8_ro.gif",
		out  : "_repository/img/layout_r2_c8.gif"
	},
	
	contact : new Image( 82, 39 ),
	contact_images :
	{
		over : "_repository/img/layout_r2_c9_ro.gif",
		out  : "_repository/img/layout_r2_c9.gif"
	}
};


/* ADD-ONS */

Array.prototype.pop = function()
{
	var objItem = null;
	
	if ( this.length > 0 )
	{
		objItem = this[this.length - 1];
		this.length--;
	}
	
	return objItem;
};
Array.prototype.push = function()
{
	if ( arguments[0].sort )
	{
		// concatenate the arguments to this array
		for ( var i = 0; i < arguments[0].length; i++ )
			this[this.length++] = arguments[0][i];
	}
	else
	{
		// concatenate the arguments to this array
		for ( var i = 0; i < arguments.length; i++ )
			this[this.length++] = arguments[i];
	}
};
Array.prototype.remove = function( iLoc )
{
	var objItem   = this[iLoc];
	var arrFront  = this.slice( 0, iLoc );
	var arrBack   = this.slice( iLoc + 1, this.length );
	var arrMerged = arrFront.concat( arrBack );
	
	// copy over
	for ( var i = 0; i < arrMerged.length; i++ )
		this[i] = arrMerged[i];
		
	// set the correct length
	this.length = arrMerged.length;
	
	return objItem;
};


/* OBJECTS */

// BrowserData Object

BrowserData = function()
{
	this.appName   = navigator.appName;
	this.userAgent = navigator.userAgent;
	this.version   = navigator.appVersion;
	this.platform  = navigator.platform;
	
	if ( this.appName == "Netscape" )
		this.b = "ns";
	else if ( ( this.appName == "Opera" ) || ( navigator.userAgent.indexOf( "Opera" ) > 0 ) )
		this.b = "opera";
	else if ( this.appName == "Microsoft Internet Explorer" )
		this.b = "ie";
	
	var ua = this.userAgent.toLowerCase();
	
	if ( ua.indexOf( "win" ) > -1 )
		this.p = "win32";
	else if ( ua.indexOf( "mac" ) > -1 )
		this.p = "mac";
	else
		this.p = "other";
		
	this.v = parseInt( this.version );
	
	this.ns    = ( this.b == "ns" && this.v >= 4 );
	this.ns4   = ( this.b == "ns" && this.v == 4 );
	this.ns6   = ( this.b == "ns" && this.v == 5 );
	this.ie    = ( this.b == "ie" && this.v >= 4 );
	this.ie4   = ( this.version.indexOf( 'MSIE 4' )   > 0 );
	this.ie4up = ( document.all != null );
	this.ie5   = ( this.version.indexOf( 'MSIE 5' )   > 0 );
	this.ie55  = ( this.version.indexOf( 'MSIE 5.5' ) > 0 );
	this.ie5x  = /msie 5\.[56789]/i.test( navigator.userAgent );
	this.ie6   = ( this.version.indexOf( 'MSIE 6' )   > 0 );
	this.ie6up = ( this.ie && document.compatMode )? true : false;
	this.opera = ( this.b == "opera" );
	this.dom   = ( document.createElement && document.appendChild && document.getElementsByTagName )? true : false;
	this.def   = ( this.ie || this.dom );
	this.moz   = navigator.productSub; // lame test
	this.apopt = /MSIE ((5\.[56789])|([6789]))/.test( this.userAgent ) && this.platform == "Win32";
};


// DomElement Object

DomElement = function( i, s )
{
	this.elm = document.getElementById( i );
	
	if ( !this.elm )
		this.elm = document.createElement( "DIV" );
	
	this.elm.id = i;

	if ( !s )
		this.elm.style.position = 'absolute';
	else
		this.elm.className = s;

	this.style = this.elm.style;
	return this;
};


DomElement.prototype.setHTML = function( s )
{
	this.elm.innerHTML = s;
};
DomElement.prototype.appendChild = function( o )
{
	this.elm.appendChild( o.elm );
};
DomElement.prototype.setXY = function( x, y )
{
	this.style.left = x;
	this.style.top  = y;
};
DomElement.prototype.setWH = function( w, h )
{
	this.style.width  = w;
	this.style.height = h;
};
DomElement.prototype.getX = function()
{
	return this.elm.offsetLeft;
};
DomElement.prototype.getY = function()
{
	return this.elm.offsetTop;
};
DomElement.prototype.getW = function()
{
	return this.elm.offsetWidth;
};
DomElement.prototype.getH = function()
{
	return this.elm.offsetHeight;
};
DomElement.prototype.killEvents = function()
{
	this.elm.onmouseup = function( e )
	{
		event.cancelBubble = true;
	}
	this.elm.onmouseout = function( e )
	{
		event.cancelBubble = true;
	}
	this.elm.onmouseover = function( e )
	{
		event.cancelBubble = true;
	}
	this.elm.onmousedown = function( e )
	{
		event.cancelBubble = true;
	}
	this.elm.oncontextmenu = function()
	{
		return false;
	}
};
DomElement.prototype.addEventListener = function( evType, fn )
{
	if ( evType.indexOf( 'mouse' ) == 0)
		return this.elm.attachEvent( "on" + evType, fn );
	else
		eval( this["on" + evType] = fn );
};
DomElement.prototype.removeEventListener = function( evType, fn )
{
	if ( evType.indexOf( 'mouse' ) == 0 )
		return this.elm.detachEvent( "on" + evType, fn );
	else
		eval( this["on" + evType] = null );
};
DomElement.prototype.invokeEvent = function( evType, args )
{
	var ret = true;
	
	if ( this["on" + evType] )
		ret = this["on" + evType]( args );
	
	if ( ret && this.parent )
		this.parent.invokeEvent( evType, args );
};

DomElement.getContainerLayerOf = function( element )
{
	if ( !element )
		return null;

	while ( ( element.tagName != 'DIV' ) && element.parentDomElement && ( element.parentDomElement != element ) )
		element = element.parentDomElement;

	return element;
};
// Given a selector string, return a style object by searching
// through stylesheets. Return null if none found (NS6 only)
DomElement.getStyleBySelector = function( selector )
{
	if ( !Base.ns6 )
		return null;
		 
	var sheetList = document.styleSheets;
	var ruleList;
	var i, j;

 	// look through stylesheets in reverse order that
	// they appear in the document
	for ( var i = sheetList.length - 1; i >= 0; i-- )
	{
		ruleList = sheetList[i].cssRules;
		for ( var j = 0; j < ruleList.length; j++ )
		{
			if ( ruleList[j].type == CSSRule.STYLE_RULE && ruleList[j].selectorText == selector )
				return ruleList[j].style;
        }
    }
	
    return null;
};
// Given an id and a property (as strings), return the given
// property of that id.  Navigator 6 will first look for the
// property in a tag; if not found, it will look through the
// stylesheet. Note: do not precede the id with a # -- it
// will be appended when searching the stylesheets.
DomElement.getIdProperty = function( id, property )
{
	if ( Base.ns6 )
	{
		var styleObject = document.getElementById( id );
		if ( styleObject != null )
		{
			styleObject = styleObject.style;
			
			if ( styleObject[property] )
				return styleObject[ property ];
		}
		styleObject = DomElement.getStyleBySelector( "#" + id );
		
        return ( styleObject != null )? styleObject[property] : null;
	}
	else if ( Base.ns4 )
	{
        return document[id][property];
    }
	else if ( Base.ie )
    {
        return document.all[id].style[property];
    }
	else
	{
		return null;
	}
};
// Given an id and a property (as strings), set the given
// property of that id to the value provided. The property
// is set directly on the tag, not in the stylesheet.
DomElement.setIdProperty = function( id, property, value )
{
	if ( Base.ns6 )
    {
		var styleObject = document.getElementById( id );
		
		if ( styleObject != null )
        {
            styleObject = styleObject.style;
            styleObject[ property ] = value;
        }
    }
    else if ( Base.ns4 )
    {
		document[id][property] = value;
    }
    else if ( Base.ie )
	{
		document.all[id].style[property] = value;
    }
};
// Return a division's document
DomElement.getDocument = function( divName )
{
	var doc;

    if ( Base.ns4 )
		doc = window.document[divName].document;
    else if ( Base.ns6 )
		doc = document;
    else if ( Base.ie )
		doc = document;
		
    return doc;
};
DomElement.getElementsByTagName = function( tagName, parentElement )
{
	var d = parentElement? parentElement : document;
	return d.getElementsByTagName && d.getElementsByTagName( tagName ) || d.all && d.all.tags( tagName );
};
DomElement.getElementsByClassName = function( ClassName, tagName, parentElement )
{
	var elements = new Array();
	var d = parentElement? parentElement : document;
	var allElements;

	if ( tagName )
		allElements = d.all && d.all.tags( tagName ) || d.getElementsByTagName && d.getElementsByTagName( tagName );
	else
		allElements = d.all || d.getElementsByTagName( "*" );
 
	for ( var i = 0, len = allElements.length; i < len; i++ )
	{
		if ( allElements[i].className == ClassName )
			elements[elements.length] = allElements[i];
	}

	return elements;
};
DomElement.getClassList = function( tagName, parentElement )
{
	var elements = new Array();
	var d = parentElement? parentElement : document;
	var allElements;

	if ( tagName )
		allElements = d.all && d.all.tags( tagName ) || d.getElementsByTagName && d.getElementsByTagName( tagName );
	else
		allElements = d.all || d.getElementsByTagName( "*" );
		
	for ( var i = 0, len = allElements.length; i < len; i++ )
	{
		var cN = allElements[i].className;

		if ( cN )
		{
			if ( elements[cN] )
			{
				elements[cN][elements[cN].length] = allElements[i];
			} 
			else
			{
				elements[cN] = new Array();
				elements[cN][0] = allElements[i];
			}
		} 
	}

	return elements;
};


// AbstractLayer Object

AbstractLayer = function( lyrName, nestedRef )
{
	// assign id
	this.lyrname = lyrName;

	// if the nestedRef argument is supplied, this constructs the path for Netscape 4.x
	if ( browser.ns4 && nestedRef )
		lyrName = nestedRef + ".document." + lyrName;
	
	// get the layer's object reference
	this.ref = browser.ns4? AbstractLayer.getRef( lyrName ) : AbstractLayer.getRef( lyrName ).style;

	return this;
};


// Returns the current position of the layer, use the CSS
// names, "left" for x and "top" for y.
AbstractLayer.prototype.getPos = function( which )
{
	if ( browser.ns4 )
		return this.ref[which];
	
	if ( browser.ie || browser.ns6 )
		return this.ref[which].split( "px" )[0];
};
// Sets the position of the layer, again, this works with
// CSS names, "left" for x and "top" for y.
AbstractLayer.prototype.setPos = function( which, pos )
{
	if ( this.ref )
		this.ref[which] = pos;
};
AbstractLayer.prototype.getClip = function( which )
{
	if ( browser.ns4 )
		return this.ref.clip[which];
	
	if ( browser.ie || browser.ns6 )
	{
		// strip 
		var clipPos = this.ref.clip.split("rect(")[1].split(")")[0].split("px");
		
		switch ( which )
		{
			case "top" :
				return Number( clipPos[0] );
			
			case "right" :
				return Number( clipPos[1] );
			
			case "bottom" :
				return Number( clipPos[2] );
			
			case "left" :
				return Number( clipPos[3] );
		}
	}
};
AbstractLayer.prototype.setClip = function( left, top, right, bottom )
{
	if ( browser.ns4 )
	{
		this.ref.clip.top    = top;
		this.ref.clip.right  = right;
		this.ref.clip.bottom = bottom;
		this.ref.clip.left   = left;
	}
	
	if ( browser.ie || browser.ns6 )
		this.ref.clip = "rect(" + top +"px " + right +"px " + bottom +"px " + left +"px)";
};
AbstractLayer.prototype.getVisibility = function()
{
	v = this.ref.visibility;
	return v.indexOf( "hid" ) == -1;
};
AbstractLayer.prototype.setVisibility = function( visible )
{
	if ( browser.ns4 )
		this.ref.visibility = visible? "show" : "hide";
	
	if ( browser.ie || browser.ns6 )
		this.ref.visibility = visible? "visible" : "hidden";
};
AbstractLayer.prototype.getzIndex = function()
{
	return this.ref.zIndex;
};
AbstractLayer.prototype.setzIndex = function( zIndex )
{
	if ( this.ref )
		this.ref.zIndex = zIndex;
};
AbstractLayer.prototype.getX = function()
{
	return this.getPos( "left" );
};
AbstractLayer.prototype.getY = function()
{
	return this.getPos( "top" );
};
AbstractLayer.prototype.toString = function( html )
{
	c = ( html != null && html )? "<br>" : "\n" ;
	
	return this.lyrname + c +
		"pos : (x:" + this.getX() + ", y:" + this.getY() + ", z:" + this.getzIndex() + ")" + c +
		"clip : (left:" + this.getClip("left")   + ", top:"    +
						  this.getClip("top")    + ", right:"  +
						  this.getClip("right")  + ", bottom:" +
						  this.getClip("bottom") + ")" + c +
		"vis : " + this.getVisibility();
};
AbstractLayer.prototype.moveTo = function( x, y )
{
	this.setPos( "left", x );
	this.setPos( "top",  y );
};
AbstractLayer.prototype.moveBy = function( dx, dy )
{
	this.setPos( "left", 1 * this.getPos( "left" ) + dx );
	this.setPos( "top",  1 * this.getPos( "top"  ) + dy );
};

// returns the reference to the actual DIV element
AbstractLayer.getRef = function( layerName )
{
	if ( browser.ns4 )
		return eval( "document." + layerName );
		
	if ( browser.ie )
		return eval( "document.all." + layerName );
	
	if ( browser.ns6 )
		return document.getElementById( layerName );
};
AbstractLayer.fixNetscape = function()
{
	if ( !AbstractLayer.getRef( "fixnetscape" ) )
		document.location.reload();
};


// Helper Object

Helper = new Object();

Helper.init = new Function;

Helper.addToFavorites = function()
{
	// TODO
};
Helper.rateArticle = function()
{
	// TODO
};
Helper.openWin = function( file, popname, width, height, statusbar, menubar )
{
	// Note: We forget about the offset stuff for now...
	
	var pop = window.open(
		file,
		popname,
		'toolbar=0,'  +
		'location=0,' +
		'directories=0,' +
		'status='  + ( statusbar || 0 ) + ',' +
		'menubar=' + ( menubar   || 0 ) + ',' +
		'scrollbars=0,' +
		'resizable=0,'  +
		'width='  + width  + ',' +
		'height=' + height + ',' +
		'left='   + ( screen.width  - ( width  || 200 ) ) / 2 + ',' +
		'top='    + ( screen.height - ( height || 200 ) ) / 2
	)
	
	pop.focus()
};
// addon for Bobux site
Helper.openShoePreview = function( id )
{
	if ( id == null )
		return false;
		
	var url = "display.php?id=" + id;
	Helper.openWin( url, "preview", 400, 510, 0, 0 );
	
	return true;
};
Helper.checkEMail = function( email )
{    
	if ( email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1 )
		return true;
	else
		return false;
};


/* SETUP */

window.onload = function()
{
	browser = new BrowserData();

	// IE55 has a serious DOM1 bug... Patch it!
	if ( browser.ie5x )
	{
		document._getElementsByTagName = document.getElementsByTagName;
		document.getElementsByTagName  = function ( sTagName )
		{
			if ( sTagName == "*" )
				return document.all;
			else
				return document._getElementsByTagName( sTagName );
		};
	}

	/*
	if ( browser.ns4 )
		AbstractLayer.fixNetscape();
	*/
	
	Helper.init();
};
