/**
 * @var id Id of product to add to cart
 * @var startDate Date of start of product
 * @var course Course of product
 * @var edulevel Level of product
 * @var price Price of Product
 * @var conflictComment Comment when product selected conflicts with other products in cart
 * @var conflictId Id of product conflicting with the product to add
 * @var conflictOpponent Comment to show with conflicting product in cart
 */

function getMovieName(movieName){
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	}
	else {
		return document[movieName]
	}
};

var prettyfy = function(val){
	val = val.toLowerCase().replace(' ','-');
	return val;
};
 
/* Functions called from Flash
**********************************/	
var cartFeedback = function(id, bool){};

var cartAdd = function(id, eduLevelId, edulevel, city, course, startDate, price, conflictId, conflictPartner, conflictComment){
	if(jQuery('#cart-list li#product-'+id).get(0)){ // already exists? don't add again!
	}else{
		jQuery('#cart-list').append('<li id="product-'+id+'" class="loading"></li>');
		if(jQuery('a.cart-pay-link').size() == 0){
			jQuery('#cart-list').after('<br /><a href="/mijn-lyceo/winkelwagen/" title="Betalen" class="more cart-pay-link">betalen</a> ');
			jQuery('#myFlash').after('<p class="cart-pay-link"><a href="/mijn-lyceo/winkelwagen/" title="Betalen" class="more">betalen</a></p>')
		}
		c = conflictId || conflictPartner || conflictComment ? 'conflict':'';
		setTimeout(function(){
			jQuery.get('/mijn-lyceo/load-cart/?iProductId='+id, function(data){				
				jQuery('li#product-'+id).addClass(c).removeClass('loading').html('<span class="location">'+city+'</span><span class="hide eduLevelId">'+eduLevelId+'</span><span class="location">'+startDate.split('-').reverse().join('-')+'</span><span class="vak '+prettyfy(course)+'">'+course+' '+edulevel+'</span><a href="#" title="Verwijder" class="delete-cart">verwijder</a>');
				$('cart-txt').innerHTML = 'U heeft de volgende trainingen in uw winkelmandje';
				getMovieName('flashMovie').toolFeedback(id, 'add', true);
			});
		}, 350);
	}
};

var cartDelete = function (id, mode) {
	var li = jQuery('li#product-'+id), eduLevelId = parseInt(li.find('.eduLevelId').text());
	jQuery.get('/mijn-lyceo/delete-product/'+id+'/', function (data) {	
        li.remove();
		if(mode == 'js'){ // called from JS, update Flash, Flash calls cartFeedback
			getMovieName('flashMovie').toolFeedback(id, 'delete', true);
			getMovieName('flashMovie').toolUpdate(id, eduLevelId, 'delete');
		}else{	
			getMovieName('flashMovie').toolFeedback(id, 'delete', true);
		}
		if(jQuery('#cart-list li').size() === 0){
			$('cart-txt').innerHTML = 'U heeft geen trainingen in uw winkelmandje';
			jQuery('a.cart-pay-link').prev().remove().end().remove();
			jQuery('p.cart-pay-link').remove();
		}
	});
};


/* Functions to Flash
**********************************/	
/*
var toolFeedback = function(id, action, bool){}
var toolUpdate = function(id, action){}
*/
var toolResize = function (h) {
	h2 = h.substring(0, h.indexOf('px',h));
	jQuery('#flashMovie').attr('height',h2).height(h).find('embed').attr('height', h2).height(h);	
};


/* Main
**********************************/	
// bind the click handler to the cart, so deletions are handled (after bubbling)
jQuery(function () {
	jQuery('#cart-list').click(function (ev) {
		if (jQuery(ev.target).hasClass('delete-cart')){
			cartDelete(ev.target.parentNode.id.substring(8), 'js');
			return false;
		}
	});	
	
});