/**
 * cart.js
 * 
 * Enhancements for shoppingcart handling
 * 
 * load: head
 * 
 * Requires
 * 	jquery
 * 	jquery_ui: effect.highlight
 * 
 * @author AK|Peppered
 */

/**
 * functions to call from HTML
 */
var cart = {};

cart.moveTitlebarToDialogHeader = function(){
	/**
	 * Place titlebar of document loaded in iframe in dialog header
	 */
	var isInIFrame = (window.location != window.parent.location) ? true : false;
	
	if (isInIFrame) {
		var $parentDocument = $(window.parent.document);
		
		// check if dialog header already contains the page header and act accordingly
		var $target = $parentDocument.find('.ui-dialog-titlebar');
		var $element = $target.find('.middleHeader');
		if ($element.length) {
			// remove
			$('#middleColumn .middleHeader:first').remove();
		}
		else {
			//move
			//$('#middleColumn .middleHeader:first').appendTo($target); <-- doesn;t work in IE<8 (iframe?)
			// workaround:
			var $header =  $('#middleColumn .middleHeader:first');
			var $html = $header.wrap('<div/>').parent().html();
			$target.append($html);
			$header.remove();
		}
		
		// remove orderSteps (if there) placed by reserveren (same dialog)
		$target = $parentDocument.find('.ui-dialog-titlebar #orderSteps');
		$target.remove();
	}
};

/**
 * cart.dialogCloseCheckoutConfirm
 * 
 * closing the dialog when checking out, is same as cancelling the order
 * 
 */
cart.dialogCloseCheckoutConfirm = function(){
	var $checkoutDialog = $('#uiDialog-cart');
	
	$checkoutDialog.dialog('option', '_parentRefresh', false); /*@todo*/
	
	$checkoutDialog.bind("dialogbeforeclose", function(e, ui){
		e.preventDefault();
		
		var dialog_id = 'confirmCancelOrder-btn';
		var title = 'Bestelling Annuleren';
		var content = 'Uw bestelling wordt geannuleerd en dit venster zal sluiten.<br />Kies of u uw <strong>winkelmandje</strong> wilt <strong>behouden</strong>, of <strong>legen</strong>.';
		
		var buttons = {
			'Mandje behouden' : {
				action: function(){
					window.parent.$('body').addClass('ui-wait');
					window.parent.window.location = window.parent.window.location.href; /*@todo: do this only when options is set*/
				},
				classes: 'buttonSprite buttonMandjeBehouden'
			},
			'Mandje legen' : {
				action: function(){
					window.parent.$('body').addClass('ui-wait');
					window.parent.window.location = $.url.query.set(window.parent.window.location.href, {clear: '1'});
				},
				classes: 'buttonSprite buttonMandjeLegen'
			}
		};
		
		window.parent.cart.showActionConfirmation(dialog_id, title, content, null, null, buttons);
	});
};


/**
 * cart.showActionConfirmation
 * 
 * Is in fact used when checking out (called from checkout.js)
 * 
 * @param {Object} dialog_id
 * @param {Object} title
 * @param {Object} content
 * @param {Object} action_yes
 * @param {Object} action_no
 * @param {Object} buttons
 * 
 */
cart.showActionConfirmation = function(dialog_id, title, content, action_yes, action_no, buttons) {
	var width = 300;
	if (typeof buttons !== 'undefined' && buttons !== null) {
	}
	else {
		width = 250;
		buttons = {
			'Ja': {
				role: 'submit',
				action:	action_yes,
				classes: 'buttonSprite buttonJa'
			},
			'Nee': {
				action: action_no,
				classes: 'buttonSprite buttonNee'
			}
		};
	}
	
	parent.$.pDialog({ // reference parent to have overlay/containment cover whole window
		id: dialog_id,
		type: 'confirmation',
		title: title,
		content: content,
		modal: true,
		resizable: false,
		bringInFocusOnOutsideclick: true,
		buttons: buttons,
		width: width
	});
};



// domready

$(function(){

// enable adding to cart
$('.buttonInWinkelmandje input.submit').each(function(){
	var $this = $(this);
	$this
		.attr('disabled', false)
		.attr('title', 'Toevoegen aan winkelmandje')
		.attr('src', this.src.replace('_disabled.gif', '.gif'))
		.removeClass('help')
		.siblings('span.help')
	 		.hide()
			.end()
		.imagePreload()
	;
});


// Target showing/event links in bigCart to _parent (iframe dialog) or opener (popup, Chrome)
if (window.opener) {
	$('#bigCart .overviewItem a').click(function(){
		opener.window.location = this.href;
		window.close();
	});
}
else {
	$('#bigCart .overviewItem a').attr('target', '_parent');
}

// pDialog for bigCart
$('#cart .buttonKaartenKopen a').click(function(e) {
	//console.log(this)
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	if (is_chrome) { // workaround for unresolved Chrome iframe/session bug
		$.cookie('toCartOriginatingUrl', window.location.href, { path: '/'} );
		return;
		//window.location = $.url.query.set(this.href, {rt: 'lb'});
		//return false;
		
		var popWin = new PopupWindow();
		//popWin.anchor = $.url.query.set(this.href, {rt: 'lb'});
		popWin.anchor = $(this).attr('href', $.url.query.set(this.href, {rt: 'lb'}));
		popWin.width = 778;
		popWin.height = 580;
		if (popWin.spawn()) {
			e.preventDefault();
		}
	}
	else {
		$.cookie('toCartOriginatingUrl', null, { path: '/'} );
		$.pDialog({
			id: 'cart',
			type: 'iframe',
			extraClass: 'LVP',
			load: $.url.query.set(this.href, {rt: 'lb'}),
			title: lvpDialogConfig.ui_container_title,
			width: lvpDialogConfig.ui_container_width,
			height: lvpDialogConfig.ui_container_height,
			resizable: lvpDialogConfig.ui_container_resize,
			draggable: lvpDialogConfig.ui_container_draggable,
			autoResize: lvpDialogConfig.ui_container_autoresize,
			bringInFocusOnOutsideclick: true,
			parentRefresh: lvpDialogConfig.ui_parent_refresh,
			iframe_header: lvpDialogConfig.iframe_header,
			iframe_footer: lvpDialogConfig.iframe_footer,
			custom_header: lvpDialogConfig.custom_header,
			buttons: {
			}
		});
	}
	
	e.preventDefault();
});


// tooltip
if ($.fn.tooltip) {
	$('span.button-disabled span').tooltip({
		extraClass: "tooltip tooltip-notice",
		inputDisabledHack: true,
		bodyHandler: function(){
			return this.tooltipText;
		}			 
	});
}


// add-to-cart effect
//$('#cart.justAddedItem').effect('highlight', {color: '#84876F'}, 3000);

// hover-class to help IE
$('form.tickets span.button-addToCart').hover(
	function(){
		$(this).removeClass('button-addToCart-noHover').addClass('button-addToCart-hover');
	},
	function(){
		$(this).removeClass('button-addToCart-hover').addClass('button-addToCart-noHover');
	}
);



}); // /domready

