/**
 * Common2 js - De Kleine Komedie
 * 
 * @author AK
 * 
 * dependancies: jQuery 1.2.6
 * 
 */

// prevent queue timeout
if (typeof Xajax != 'undefined') {
	window.setInterval("xajax_UpdateActivity()", 270000);
}

/**
 * Nyro modal settings & modal anchors
 */

function openNewModal(elts, settings){
	var $anchors = elts.content.find('a');
	$anchors.click(function(){
		var url = $(this).attr('href');
		url += '?requestType=modalWindow';
		$.nyroModalManual({
			forceType: 'form',
			url: url,
			endShowContent: function(elts, settings){ // handle anchors on page
				openNewModal(elts, settings);
			}		
		});
		return false;
	});
}

$(function(){
	
	// ColorBox
	if ($.fn.colorbox) {
		
//		$("#cboxClose").click(function(){ 
//			window.location.href = "/bestellijst/";
//			return false;
//		});

		var event_id;
		
		// set html class on load & close
		$()
			.bind('cbox_load', function(){
		        $('html').addClass('modalWindow-active');
			})
			.bind('cbox_closed', function(){
		        $('html').removeClass('modalWindow-active');
				var ref = window.location.pathname + window.location.search;
				var aPath = window.location.pathname.split('/');
				if (aPath[0] == '') {
					aPath.shift();
				}
				if (aPath[0] == 'programma' && (aPath[1] == '' || aPath[1] === undefined || isNaN(aPath[1]))) {
					ref += '#show-' + event_id;
				}
				//document.cookie = 'bestelRef=' + encodeURIComponent(ref) + '; path=/';
				window.location.href = "/bestellijst/?ref=" + encodeURIComponent(ref);
			})
		;

		$("input.orderbutton").click(function() {
			var $this = $(this);
			event_id = $this.data('event_id');
			$.fn.colorbox({resize:'true',href:$this.data('url'),open:true,width:"800px", height:"90%", opacity:"0.4",iframe:true});
		});
		$("input.afrekenen").click(function() {
			var $this = $(this);
			if (confirm('Let op: verzeker uzelf ervan dat uw winkelmandje de juiste voorstellingen en kaarten bevat.\nAls u tijdens het afrekenen besluit te annuleren, wordt uw gehele winkelmandje geleegd.')) {
				//$.fn.colorbox({resize:'true',href:$this.data('url'),open:true,width:"80%", height:"80%", iframe:true});
				window.location.href = $this.data('url');
				
			}			
		});
	}
	
	if ($.fn.nyroModal) {
		$.fn.nyroModal.settings.closeButton = '<a href="#" class="nyroModalClose" id="closeBut" title="sluiten">sluiten</a>';
		
		$("a[rel='modal']").click(function(){
			var url = $(this).attr('href');			
			if (url.lastIndexOf(".jpg") != -1 || url.lastIndexOf(".gif") != -1 || url.lastIndexOf(".png") != -1 || url.lastIndexOf(".txt") != -1 || url.lastIndexOf(".pdf") != -1) {
				if (url.lastIndexOf(".txt") != -1) {
					var nyroType = 'iframe';
					var width = 600;
					var height = 400;
				}
				else if (url.lastIndexOf(".pdf") != -1) {
					// very tricky to implement in modal; just open it
					window.open(url)
					return false;
				}
				else {
					var nyroType = 'image';
					var width = null;
					var height = null;
				}
				
				$.nyroModalManual({
					url: url,
					forceType: nyroType,
					width: width,
					height: height,
					endShowContent: function(elts, settings){
						var $p = $('<p id="nyroModalSupportLine"></p>');
						$('<a href="#">sluiten</a>').click(function(){
							$.nyroModalRemove();
							return false;
						}).appendTo($p);
						$('<span> &nbsp;|&nbsp; </span>').appendTo($p);
						$('<a href="' + url + '" target="_blank">toon in nieuw venster &#62;</a>').click(function(){
							$.nyroModalRemove()
						}).appendTo($p).parent().appendTo('#nyroModalWrapper');
					}
					
				});
				
			}
			else {
				url += '?requestType=modalWindow';
				$.nyroModalManual({
					url: url,
					endShowContent: function(elts, settings){ // handle anchors on page
						openNewModal(elts, settings);
					}
				});
			}
			return false;
		});
	}
});

// nice selects
 if ($.ppprd.niceSelects.enabled && $.SelectBox) {
	$('.calendar select').selectbox();		
}	

// showList hover
$('.showList .item a.detailAnchor, .showList .item img').hover(function(){
	var $parent = $(this).parents('.item');
	$parent.addClass('item-hover');
}, function(){
	var $parent = $(this).parents('.item');
	$parent.removeClass('item-hover');
});

$('.showList .item img').click(function(){
	var $parent = $(this).parents('.item');
	var href = $parent.find('a.detailAnchor').attr('href');
	if (href != undefined) {
		window.location.href = href;
	}
});

// showList - added to basket
if ($('#main .showList form.showDateSelect .notice-ok').length) {
	var $notice = $('#main .showList form.showDateSelect .notice-ok');
	var $fieldset = $notice.next('fieldset').hide();
	$('<br />').appendTo($notice);
	$('<a href="#" class="readOn">bestel meer</a>')
		.click(function(){
			$notice.remove();
			$fieldset.show();
			return false;
		})
		.appendTo($notice)
	;		
}

/**
 * show (p.detail)
 */	
if ($('body.show').length) {
	// calendar
	var cal = new Calendar();
	cal.monthsType = 'writeOut';
	cal.$form = $('.subItem').find('form');
	cal.init();
	
	// showing options icons
	$('ul#showOptions > li a').doLabelIco();
}
else if ($('body.newsDetail').length) {
	$('a.addthis_button').doLabelIco();
}
	
$('#columnContainer').css('visibility', 'visible');

