/**
 * @author Marco Süß
 * @copyright Copyright � 2004-2007, Zwölf Medien GbR, All rights reserved.
 */

function replaceHeadlines() {
	$$('#content > div > h1:first-of-type').each(function(headlineToReplace) {
		headlineToReplace.replace(
			'<h1><div class="replaced">'+
			headlineToReplace.innerHTML+
			'</div></h1>'
		);
	});

	$$('.box > h1:first-of-type').each(function(headlineToReplace) {
		headlineToReplace.replace(
			'<h1><div class="replaced">'+
			headlineToReplace.innerHTML+
			'</div></h1>'
		);
	});
}

function replaceHeadlinesSifr(){
	sIFR.replaceElement(named({sSelector:"#content > div > h1:first-of-type",sBrowser:"all",
								sFlashSrc:[{version:8,src:"/static/jscripts/sifr/sifr_futura-v8.swf"},
										   {version:6,src:"/static/jscripts/sifr/sifr_futura.swf"}
								],
								sFlashVars:"offsetLeft=0",
								sWmode:"transparent"}));
	sIFR.replaceElement(named({sSelector:"div.box > h1:first-of-type",sBrowser:"all",
								sFlashSrc:[{version:8,src:"/static/jscripts/sifr/sifr_futura-v8.swf"},
										   {version:6,src:"/static/jscripts/sifr/sifr_futura.swf"}
								],
								sFlashVars:"offsetLeft=0",
								sWmode:"transparent"}));
	sIFR.replaceElement(named({sSelector:"h1.dolly",sBrowser:"all",
								sFlashSrc:[{version:8,src:"/static/jscripts/sifr/sifr_futura-v8.swf"},
										   {version:6,src:"/static/jscripts/sifr/sifr_futura.swf"}
								],
								sFlashVars:"offsetLeft=0",
								sWmode:"transparent"}));
}

function openPop(commandOpts) {
    var stdOpts = {
        name: 'Popup',
        uri: '#',
        dependent: 'yes',
        innerWidth: 400,
        innerHeight: 350
    };
    Object.extend(stdOpts, commandOpts);
    var opts = $H(stdOpts);
    var name = opts.unset('name');
    var uri  = opts.unset('uri');
    var windowOptArray = new Array();
    $H(opts).each(function(pair){
        console.log(pair);
        var key = pair.key;
        if (Prototype.Browser.IE) {
            if( pair.key == 'innerHeight' ) key = 'height';
            if( pair.key == 'innerWidth' ) key = 'width';
        }
        windowOptArray.push(key + "=" + pair.value);
    });
    var popup = window.open(uri, name, windowOptArray.join(','));
    /*var popupDocument;
	if (popup.contentDocument) {
		popupDocument = popup.contentDocument;
	} else if (popup.contentWindow) {
		popupDocument = popup.contentWindow.document;
	} else if (popup.document) {
		popupDocument = popup.document;
	}*/
    //alert(popupDocument.width);
    /*popup.document.documentElement.clientHeight = innerHeight;
    popup.document.documentElement.clientWidth  = innerWidth;*/
}

var CitykinosTipps = function(rootElement){
	var self = this;
	self._rootElement = $(rootElement);
	self._currentEffect = null;
	self._baseDuration = 1;
	self.moveUp = function(evt) {
		if (self._respondToEvent(evt)==false) return false;
		var el = self._getInfoDiv(evt);
		self._cancelEffect();
		$(el).morph('margin-top:211px', {
			duration:self._getDuration(),
			afterFinish: self._unlock,
			beforeStart: self._lock,
			transition: Effect.Transitions.sinoidal
		});
	};
	self.moveDown = function(evt) {
		if (self._respondToEvent(evt)==false) return false;
		var el = self._getInfoDiv(evt);
		self._cancelEffect();
		$(el).morph('margin-top:361px', {
			afterFinish: self._unlock,
			duration:self._getDuration(),
			beforeStart: self._lock,
			transition: Effect.Transitions.sinoidal
		});
	};
	self._getDuration = function(){
		return (self._currentEffect != null)? self._currentEffect.position*self._baseDuration : self._baseDuration;
	}
	self._getInfoDiv = function(evt) {
		return $$('#'+self._rootElement.id+' > div:first-of-type')[0];
	};
	self._lock = function(effectObj) {
		self._currentEffect = effectObj;
	};
	self._unlock = function(effectObj) {
		self._currentEffect = null;
	};
	self._cancelEffect = function() {
		if (self._currentEffect != null) {
			self._currentEffect.cancel();
		}
	};
	self._respondToEvent = function(evt) {
		/*if(evt.relatedTarget == null || $(evt.relatedTarget).descendantOf(self._rootElement) || evt.relatedTarget == self._rootElement)
			return false
		else
			return true;*/
		return (evt.relatedTarget != null && !$(evt.relatedTarget).descendantOf(self._rootElement) && evt.relatedTarget != self._rootElement);
	};
	Event.observe(self._rootElement, 'mouseover', self.moveUp);
	Event.observe(self._rootElement, 'mouseout', self.moveDown);
};

function insertSwfObject(){
    var sidebarHeight = $('content').getHeight();
    if( sidebarHeight < 700 )  sidebarHeight = 700;
    if( sidebarHeight > 2880 ) sidebarHeight = 2880;

	var sidebar = new SWFObject("/static/swf/sidebar.swf", "sidebar", "280", "" + sidebarHeight, "8", "#00000000");

	sidebar.addParam("scale", "noScale");
	sidebar.addParam("wmode", "transparent");
    sidebar.addParam("salign", "tl");
	sidebar.write('cinemas_flash');
}

/* An InplaceEditor which restores the invalid value after failure
   without server-side intervention */
var GracefulInPlaceEditor = Class.create(Ajax.InPlaceEditor,{
	handleAJAXFailure: function(transport) {
		this.triggerCallback('onFailure', transport);
	    if (this._valueToSave) {
			this.element.innerHTML = this._valueToSave;
			this._valueToSave = null;
			this.element.addClassName(this.options.errorClassName || 'param_error');
			this.element.title = this.options.errorMessageText  || 'Dieser Wert ist fehlerhaft und wurde nicht gespeichert.';
	    }
	},
	prepareSubmission: function($super) {
		if(this.element.hasClassName('param_error'))
			this.element.removeClassName('param_error');
		this.element.title = this.options.clickToEditText;
		this._valueToSave = $F(this._controls.editor);
		$super();
	}
});

/* Taken from firebugx.js */
if (!window.console || !console.firebug) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

var ZwoelfUtils = new Object();
ZwoelfUtils.String = {
    replaceUmlauts : function() {
        // this requires prototype.js
        var newString = this;
        var nonLatinMap = $H({
            'ä': 'ae',
            'Ä': 'Ae',
            'ö': 'oe',
            'Ö': 'Oe',
            'ü': 'ue',
            'Ü': 'Ue',
            '&auml;': 'ae',
            '&Auml;': 'Ae',
            '&ouml;': 'oe',
            '&Ouml;': 'Oe',
            '&uuml;': 'ue',
            '&Uuml;': 'Ue'
        });
        nonLatinMap.keys().each(function(character){
           newString = newString.gsub(character, nonLatinMap.get(character));
        });
        return newString;
    },
    uc : function() { //just a shorthand
        return this.toUpperCase();
    },
    lc : function() { //just a shorthand
        return this.toLowerCase();
    },
    dc : function() { //just a shorthand
        return this.toLowerCase();
    },
    ucfirst : function() {
        return this.charAt(0).toUpperCase() + this.substr(1, this.length-1);
    },
    ucwords : function() {
        return this.replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
    },
    forceUnderscore : function() {
        return this.toLowerCase().replace(/\s/g, '_');
    },
    forceCamelCase : function() {
        // this requires prototype.js
        return this.forceUnderscore().camelize();
    },
    onlyWord : function() {
        return this.replace(/\W/g, '');
    },
    removeEntities : function() {
        return this.replace(/&[^;]*;/g, '');
    },
    trim : function(charlist) {
        charlist = !charlist ? ' \\s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
        var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
        return this.replace(re, '');
    },
    basename : function() {
        return this.replace(/^.*\/([^\/]*$)/g, '\$1')
    }
};

ZwoelfUtils.Array = {
    includes : function(obj) {
        for(var i = 0; i < this.length; i++) {
            if( this[i] === obj ) return true;
        }
        return false;
    }
}

if (Prototype) {
    Object.extend(window.String.prototype, ZwoelfUtils.String);
    //Object.extend(window.Array.prototype, ZwoelfUtils.Array);
}

