var CS = CS || {};
CS.wishList = CS.wishList || {};
CS.wishListCookie = CS.wishListCookie || {};

window.addEvent('domready', function() {	
	
	CS.bundles();
	CS.dispalyToWishList();
});


/************************************
*	UTILITIES
************************************/

window.log = function () {
	log.history = log.history || [];
	log.history.push(arguments);
	if (this.console) {
		console.log(Array.prototype.slice.call(arguments));
	}
};

CS.switchViews = function( sel ) {
	$('home').setStyle( 'display', 'none' );
	$('connecting').setStyle( 'display', 'none' );
	$(sel).setStyle( 'display', 'inline-block' );
};

CS.parseNumber = function(str) {	
	return parseInt( str.toString().replace(/[^\d\.]/g,'') );
};


/************************************
*	BUNDLES
************************************/

CS.bundles = function() {	
	CS.addBundleClickEvents( $$('.bundle div a') );
	CS.addBundleClickEvents( $$('.lessons div a') );
	CS.addWishListEvents( $$('.lessons table td.col_3 a'), 'addToWishList', true );
	CS.addWishListEvents( $$('.bundle table td.col_3 a'), 'addToWishList', false );
};

CS.addBundleClickEvents = function( selector ) {	
	selector.addEvent('click', function() {	
		
		var p = this.getParent().getElement( 'p' );

		if( this.get( 'class' ) == 'selected' ) {
			this.removeClass( 'selected' );
			p.setStyle( 'display', 'none' );
		}
		else {
			this.addClass( 'selected' );
			p.setStyle( 'display', 'block' );
		}		
		return false;
	});
};

CS.addWishListEvents = function( selector, func, isLesson ) {	
	selector.addEvent('click', function() {	
		
		var topParent = ( !isLesson ) ? this.getParent().getParent() : this.getParent().getParent();
		
		var id 		= topParent.getElements( 'td.col_2' )[0].get('html');
		var name 	= ( !isLesson ) ?  topParent.getElements( 'td.col_1' )[0].getFirst('span').get('html') : topParent.getElements( 'td.col_1 div' )[0].getFirst('span').get('html');
		CS[func]( id, name );
		log( topParent )
		return false;
	});
};

CS.getCookie = function() {	
	
	return JSON.decode( Cookie.read( 'wishList' ) ) || {};
};

CS.addToWishList = function( id, name ) {	
	CS.wishList			= CS.getCookie();
	CS.wishList[id] 	= { 'id':id, 'name':name };
	CS.wishListCookie 	= Cookie.write( 'wishList', JSON.encode( CS.wishList ) );
	log( JSON.decode( Cookie.read( 'wishList' ) ) );
};

CS.removeFromWishList = function( id ) {	
	CS.wishList			= CS.getCookie();
	delete CS.wishList[id];
	CS.wishListCookie 	= Cookie.write( 'wishList', JSON.encode( CS.wishList ));
	CS.dispalyToWishList();
};

CS.dispalyToWishList = function( id, name ) {	
	
	if( $('wish_list') != undefined ) {
		
		var out				= "";
		CS.wishListTemplate = CS.wishListTemplate || '<tr>' + $('wish_list_template').get( "html") + '</tr>';
		CS.wishList 		= JSON.decode( Cookie.read( 'wishList' ) );
		
		for( var key in CS.wishList ) {			
			out += CS.wishListTemplate.split('#name#').join( CS.wishList[key].name ).split('#id#').join( CS.wishList[key].id ) ;
		}
		
		$('wish_list_table_inner').set( 'html', out );		
		CS.addWishListEvents( $$('.wish_list_table_inner a'), 'removeFromWishList', false );
	}
};
