// page init
jQuery.noConflict();
jQuery(function(){
	clearInputs();
	fixTrans();
});
jQuery(window).load(function(){
	initLightbox();
});

// clear inputs
function clearInputs(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
};

// lightbox init
function initLightbox(){
	var days = 30; // [days]
	var delay = 10000; // [1000 - 1sec]
	var opener = jQuery('a.open-popup');
	if(opener.length && !jQuery.cookie('busy')){
		var popup = opener.modalPopup();
		setTimeout(function(){
			popup.show();
			jQuery.cookie('busy', true, {expires:days,path:"/"}); 
		},delay);
	}
};

//lightbox plugin
jQuery.fn.modalPopup = function(options){return new modalPopup(jQuery(this).eq(0),options)}
function modalPopup(link, options) {this.init(link,options)}
modalPopup.prototype = {
	init:function(link,options){
		var el = this;
		//options
		el.options = jQuery.extend({
			fadeSpeed:400,
			closer:'a.btn-close',
			scroll:true,
			wrapper:'#wrapper',
			IE:true,
			zIndex:999
		},options);	
		//popup & default css styles
		if (jQuery.browser.msie && el.options.IE) el.popup = jQuery(link.attr('href')).css({visibility:'hidden'})
		else el.popup = jQuery(link.attr('href')).css({opacity:0,visibility:'hidden'});
		if (el.options.zIndex) el.popup.css({zIndex : el.options.zIndex});
		el.closer = jQuery(el.popup.find(el.options.closer));
		el.popup.visible = false;
		modalPopup.prototype.activePopup = false;
		if (!modalPopup.prototype.firstRun) {
			modalPopup.prototype.firstRun = 'done';
			if (jQuery.browser.msie && jQuery.browser.version < 7) modalPopup.prototype.selects = jQuery('select');
			//create fader
			if (!jQuery('#fader').length) jQuery('body').append('<div id="fader"></div>');
			modalPopup.prototype.fader = jQuery('#fader');
			modalPopup.prototype.fader.css({position:'absolute',top:0,left:0,background:'#000',opacity:0,display:'none'});	
			if (el.options.zIndex) modalPopup.prototype.fader.css({zIndex : el.options.zIndex-1});
			modalPopup.prototype.wrapper = jQuery(el.options.wrapper);
			//fader click event
			modalPopup.prototype.fader.click(function(){
				if (modalPopup.prototype.activePopup == false) el.hideFader()
				else modalPopup.prototype.activePopup.hidePopup(function(){el.hideFader()});
				return false;
			});
			//esc event
			jQuery(document).keydown(function (e) {
				if (e.keyCode == 27) {
					if (modalPopup.prototype.activePopup == false) el.hideFader()
					else modalPopup.prototype.activePopup.hidePopup(function(){el.hideFader()});
					return false;
				}
			});
		}
		
		el.wrapper = modalPopup.prototype.wrapper;
		
		if (jQuery.browser.msie && jQuery.browser.version < 7) {
			el.popupSelects = jQuery('select',el.popup);
			modalPopup.prototype.selects = modalPopup.prototype.selects.not(el.popupSelects);
		}
		
		//open event
		link.click(function(){
			el.show();
			return false;
		});
		//close event
		el.closer.click(function(){
			el.close();
			return false;
		});
		//resize event
		jQuery(window).resize(function(){
			if (el.popup.visible) el.positioning(false);
		});
		if (el.options.scroll) {
			jQuery(window).scroll(function(){
				el.positioning(true);
			});
		}
	},
	
	close:function(){
		var el = this;
		el.hidePopup(function(){el.hideFader()});
	},
	
	show:function(){
		var el = this;
		if (modalPopup.prototype.activePopup == el) {return false;}
		if (modalPopup.prototype.activePopup) {
			modalPopup.prototype.activePopup.hidePopup(function(){
				el.showPopup()
				el.positioning(true);
			});
		} else {
			el.showFader(function(){el.showPopup()});
			el.positioning(true);
		} 
	},
	
	showPopup:function(){
		var el = this;
		el.popup.visible = true;
		modalPopup.prototype.activePopup = el;
		if (jQuery.browser.msie && el.options.IE) el.popup.css({visibility:'visible'})
		else el.popup.stop().css({'visibility':'visible'}).animate({opacity:1},el.options.fadeSpeed)
	},
	hidePopup:function(callback){
		var el = this;
		if (jQuery.browser.msie && el.options.IE) {
			el.popup.css({left:'-9999px',top:'-9999px',visibility:'hidden'});
			el.popup.visible = false;
			modalPopup.prototype.activePopup = false;
			if (jQuery.isFunction(callback)) callback();
		} else {
			el.popup.stop().animate({opacity:0},el.options.fadeSpeed,function(){
				el.popup.css({left:'-9999px',top:'-9999px',visibility:'hidden'});
				el.popup.visible = false;
				modalPopup.prototype.activePopup = false;
				if (jQuery.isFunction(callback)) callback();
			});
		}
	},
	showFader:function(callback){
		var el = this;
		el.fader.stop().css({display:'block'}).animate({opacity:0.65},el.options.fadeSpeed,function(){
			if (jQuery.isFunction(callback)) callback();
		});
		if (jQuery.browser.msie && jQuery.browser.version < 7) modalPopup.prototype.selects.css({'visibility': 'hidden'});
	},
	hideFader:function(){
		var el = this;
		el.fader.stop().animate({opacity:0},el.options.fadeSpeed,function(){
			el.fader.css({display:'none'});
			if (jQuery.browser.msie && jQuery.browser.version < 7) modalPopup.prototype.selects.css({'visibility': 'visible'});
		});
	},
	positioning:function(openFlag){
		var el = this;
		//x offset
		var windowW = jQuery(window).width();
		var popupW = el.popup.outerWidth();
		var wrapperW = el.wrapper.outerWidth();
		
		if (windowW < wrapperW) {
			el.popup.css({left:wrapperW/2-popupW/2})
			el.fader.css({width:wrapperW});
		} else {
			 el.popup.css({left:windowW/2-popupW/2});
			 el.fader.css({width:windowW})
		}
		//y offset
		var docH;
		var windowH = jQuery(window).height();
		var wrapperH = el.wrapper.outerHeight();
		if (windowH < wrapperH) docH = wrapperH
		else docH = windowH;
		
		var popupH = el.popup.outerHeight();
		if (openFlag) {
			var popupH = el.popup.outerHeight();
			if (popupH < windowH) el.popup.css({top:windowH/2-popupH/2+jQuery(window).scrollTop()});
			else if (jQuery(window).scrollTop()+popupH > docH){
				el.popup.css({top:docH-popupH});
			} else {
				el.popup.css({top:jQuery(window).scrollTop()});
			}
		}
		el.fader.css({height:docH});
	}
};

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

// clear inputs plugin
function clearFormFields(o){
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
};

// IE6 png fix
var transparentImage = "http://46.4.15.53/p2h_wordpress/tagesgeldkonto/wp-content/themes/tagesgeldkonto/images/transparent.gif";
function fixTrans(){
	if (typeof document.body.style.maxHeight == 'undefined') 
	{
		var imgs = document.getElementsByTagName("img");
		for (i = 0; i < imgs.length; i++)
		{	
			if (imgs[i].src.indexOf(transparentImage) != -1)
			{
				return;
			}
			if (imgs[i].src.indexOf(".png") != -1)
			{
				var src = imgs[i].src;
				imgs[i].src = transparentImage;
				imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
				imgs[i].style.display = "inline-block";
			}
		}	
	}
};
