/**
 * global script containers
 */
var common = {}, home = {}, programma = {}, nieuws = {}, fotos = {}, gastenboek = {};

function isEmpty(str) { if (typeof str == 'undefined' || str === null || str === '' || str == 0) { return true; } return false; }



/**
 * Programma day & stage select enhancements
 */
programma.dayNStageSelectInit = function(){
	var $form = $('#dayNStageForm');
	
	// autoSelect
	var $sel1 = $('#dayNStageForm-day select').autoSelect({
		beforeSubmit: function(_options){
			// prevent submit, and request page manually
			_options.preventSubmit = true;
			document.location.href = '/programma/' + this.value + '/';
		}
	});
	var $sel2 = $('#dayNStageForm-stage select').autoSelect();
		
	if ($sel1.length || $sel2.length) {
		$form.addClass('autoSelect');
	}	
};

var lb_options = {
	imageLoading: '/images/lightbox-ico-loading.gif',
	imageBtnPrev: '/images/lightbox-btn-prev.gif',
	imageBtnNext: '/images/lightbox-btn-next.gif',
	imageBtnClose: '/images/lightbox-btn-close.gif',
	imageBlank: '/images/lightbox-blank.gif'
};


/**
 * common
 */

// common
common.common = function(){
	// imgreplace
	$('body').ImageReplace();

	// anchors (external/popup)
	$("a[rel='external']").click(function(){window.open(this.href); return false});
	var oPopWin = null;
	$("a[rel='popup']").click(function(){
		sHref = $(this).attr('href');
		if (sHref.substr(-1) != '/')
			sHref += '/';
		sHref += '?popup=true';
		if (oPopWin && oPopWin.open && !oPopWin.closed) oPopWin.close();
		oPopWin = window.open(sHref, 'winName', 'width=500, height=500'); return false
	});
	
	common.formNBSubscribe();
};

/**
 * subscribe form
 */
common.formNBSubscribe = function(){
	var sZoekFormVal = '';
	$('#formNBSubscribe input.text').addClass('inactive').val('e-mail adres');
	$('#formNBSubscribe input.text').focus(function(){
		$(this).val(sZoekFormVal).removeClass('inactive');
	});
	$('#formNBSubscribe input.text').blur(function(){
		sZoekFormVal = $(this).val();
		if (sZoekFormVal == '') 
			$(this).addClass('inactive').val('e-mail adres');
	});
};

/**
 * Home
 */
home.welcome = function(){
	$('#welcome div.blockInner[lang="en"]').hide();
	$('#welcome').prepend('<a href="#welcome" id="langToggle" class="IR IRActive en" title="show introduction text in english">en<span class="IR-aid"></span></a>');
	$('#langToggle').click(function(){
		$('#welcome div.blockInner').toggle();
		if ($('#langToggle').text() == 'en')
			$('#langToggle').html('nl<span class="IR-aid"></span>').removeClass('en').addClass('nl').attr('title', 'Toon introductietekst in het Nederlands');
		else
			$('#langToggle').html('en<span class="IR-aid"></span>').removeClass('nl').addClass('en').attr('title', 'Show introduction text in english');
		return false;
	});
};


/**
 * programma detail (artiest)
 */
programma.detail_init = function(){
	$('#artiest img').parent('a').lightBox(lb_options);
};

/**
 * nieuws detail
 */
nieuws.detail_init = function(){
	$('#nieuwsItem img').parent('a').lightBox(lb_options);
};

/**
 * Foto's
 */
fotos.init = function(){
	$('#bodyFotos #albumThumbs a').lightBox(lb_options);
};

/**
 * Gastenboek
 */
gastenboek.reageren = function(){
	var $reagerenWrap = $('#gastenboek #reagerenWrap').hide();
	var $toggleAnchor = $('<a href="#reagerenWrap"><strong>reageer</strong></a>');
	var $toggleContainer = $('<p id="reageerToggle"></p>').append($toggleAnchor)
		.insertAfter('#gastenboek h2')
	;
	//$('#gastenboek h2').after('<p id="reageerToggle"><a href="#reagerenWrap"><strong>reageer</strong></a></p>');
	$(function(){ // nyroModal only available on domready
		if (!$toggleAnchor.nyroModal()) {
			$reagerenWrap.show();
		}
	});
};


/**
 * Image Replace
 * Appends some spans to selection, so with css nice IR can happen)
 * @author AK|Peppered
 */

jQuery.fn.ImageReplace = function(options) {
	var defaults = {
		hover: false
	};
	options = $.extend(defaults, options);
	if (document.styleSheets[0].disabled) return false;
	if (this.length < 1) return false;
	
	if (this.get(0).nodeName.toLowerCase() == 'body') {
		// old style replace via classnames
		$('.IR, .IR-anchors a').append('<span class="IR-aid"></span>');	// single element IR & descendant anchors of an element
		$('.IR.IR-hover span.IR-aid, .IR-anchors.IR-anchors-hover a span.IR-aid').append('<span></span>');	// append another span when it has a hover state
		$('.IR, .IR-anchors').addClass('IRActive');		
	}
	else {
		this.each(function() {
			//$.log(this);
			$(this).addClass('ir');		
			$(this).append('<span class="ir-aid"></span>');	
			if (options.hover == true) {
				$(this).children('span.ir-aid').addClass('ir-aid-hover');
				$(this).children('span.ir-aid').append('<span></span>');
			}
	
			$(this).addClass('irActive');
		});		
	}
}

/**
 * preload images
 */

aImgsPreload = new Array(); // gets filled throughout different .incs
aImgsPreload.push('tab_sponsors.gif','tab_fotos.gif');
for (i = 0; i < aImgsPreload.length; i++) {
	$("<img>").attr("src", '/images/' + aImgsPreload[i]);
}


//dimScreen()
//by Brandon Goldman
jQuery.extend({
    //dims the screen
    dimScreen: function(speed, opacity, callback) {
        if(jQuery('#__dimScreen').size() > 0) return;
        
        if(typeof speed == 'function') {
            callback = speed;
            speed = null;
        }

        if(typeof opacity == 'function') {
            callback = opacity;
            opacity = null;
        }

        if(speed < 1) {
            var placeholder = opacity;
            opacity = speed;
            speed = placeholder;
        }
        
        if(opacity >= 1) {
            var placeholder = speed;
            speed = opacity;
            opacity = placeholder;
        }

        speed = (speed > 0) ? speed : 500;
        opacity = (opacity > 0) ? opacity : 0.5;
        return jQuery('<div></div>').attr({
                id: '__dimScreen'
                ,fade_opacity: opacity
                ,speed: speed
            }).css({
            background: '#000'
            ,height: $(document).attr('height') + 'px'
            ,left: '0px'
            ,opacity: 0
            ,position: 'absolute'
            ,top: '0px'
            ,width: $(document).attr('width') + 'px'
            ,zIndex: 999
        }).appendTo(document.body).fadeTo(speed, opacity, callback);
    },
    
    //stops current dimming of the screen
    dimScreenStop: function(callback) {
        var x = jQuery('#__dimScreen');
        var opacity = x.attr('fade_opacity');
        var speed = x.attr('speed');
        x.fadeOut(speed, function() {
            x.remove();
            if(typeof callback == 'function') callback();
        });
    }
});


/**
 * autoSelect, jQuery plugin
 * 
 * Auto-submit forms when select options have changed
 * Smoothens out (almost) behavioural differences between browsers.
 * Inpsired on: http://themaninblue.com/writing/perspective/2004/10/19/
 * 
 * usage: $selection.autoSelect({options})
 * 
 * @author AK | Peppered
 * @version 1.0.3.mod_wpop
 * 
 */	
;(function($) {	
$.fn.autoSelect = function(options) {
	options = $.extend({
		beforeSubmit: function(){}
	}, options);
	
	// internal options 
	var _options = {
		preventSubmit: false
	};
	
	return this.each(function() {
		var $this = $(this);
					
		$this.validKeyChange = true;		// for browsers that trigger change event when option changed by keyboard (IE)
		$this.initValue = $(this).val();
		
		$this.change(function(e){
			if ($this.validKeyChange && (this.value != $this.initValue)) {		
				$this.initValue = this.value;
				options.beforeSubmit.call(this, _options);
				if (!_options.preventSubmit) {
					this.form.submit();
				}
			}
		});
		
		$this.keydown(function(e){
			// 9=tab, 13=enter, esc=27
			if ((e.keyCode == 13) && this.value != $this.initValue) {						
				$this.validKeyChange = true;
				$this.change();
			}
			else {
				if (e.keyCode == 27 || ((e.keyCode == 13 || e.keyCode == 9) && this.value == $this.initValue)) {
					$this.validKeyChange = false;
					this.value = $this.initValue; // a.o. for Safari when opening select with mouse, using keys & tabbing off
				}
				else {
					$this.validKeyChange = false;
				}
			}
		});
		
		$this.blur(function(e){
			if (this.value != $this.initValue && $this.validKeyChange) {
				$this.initValue = this.value;
				options.beforeSubmit.call(this, _options);
				if (!_options.preventSubmit) {
					this.form.submit();
				}
			}
		});
	});
	
	// private formSubmit
	function submitForm() {
		
	};
};
})(jQuery);




//Form class/object v0.1.2 (peppered/AK)

function Form(id) {
	this.elementId = id;
	this.element = null;
	if (document.getElementById(id))
		this.element = document.getElementById(id);
}


// FormField child object

Form.prototype.FormField = function(id) {

	this.elementId = id;
	this.element = null;
	if (document.getElementById(id))
		this.element = document.getElementById(id);
	this.element.formObject = this; // for referencing in element events
}

Form.prototype.FormField.prototype.setDefaultValue = function(sValue) {
	var sDefaultTxt = sValue;
	this.element.value = sDefaultTxt;
	this.element.onfocus = function(){
		if (this.value == sDefaultTxt)
			this.value = '';
	};
	this.element.onblur = function(){
		if (this.value == '')
			this.value = sDefaultTxt;
	};
}

// enableImgHover (imgbutton fields)
Form.prototype.FormField.prototype.enableImgHover = function() {
	var sImg = this.element.src;
	var sExt = sImg.substring(sImg.length-3);
	var sHoverImg = sImg.substring(0, sImg.length-4) + '_over' + '.' + sExt;
	
	this.img = sImg;
	this.hoverImg = sHoverImg;
	
	var agt=navigator.userAgent.toLowerCase();
	// Note: On IE5, these return 4
	var is_major = parseInt(navigator.appVersion);
	var is_minor = parseFloat(navigator.appVersion);
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var ie_alphaImg = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1 || agt.indexOf("msie 6.") != -1));
	
	if (sExt == 'png' && is_ie && ie_alphaImg) {
		this.element.onmouseover = function() {
			//this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + sHoverImg + "',sizingMethod='image')";
			this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
			this.parentNode.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + sHoverImg + "',sizingMethod='image')";
		};
		this.element.onmouseout = function(){
			//this.runtimeStyle.filter = '';
			this.parentNode.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + sImg + "',sizingMethod='image')";;
		};
		return;
	}

	this.element.onmouseover = this.element.onfocus = function(){this.src = this.formObject.hoverImg};
	this.element.onmouseout = this.element.onblur = function(){this.src = this.formObject.img};
}


