/*
 * Album object
 *
 */

//construct
function album_class(){

	//properties
	var contents = new Array();		//contents of current album
	var nonce = new String();		//wordpress nonce

}

//add photo to album
album_class.prototype.add = function(post_id){

	/**
	 * debug message
	 */
	var msg = 'Unfortunately we are updating our system \n' +
		'So we have disabled the photo cart \n' +
		'Please feel free to browse \n' +
		'And check back later \n';
	//alert(msg); return;
	
	//start loader
	jQuery('#button-'+post_id).hide();
	jQuery('#loader-'+post_id).show();

	//vars
	//photo = new String(jQuery('#post-'+post_id+' .thumbnail').attr('id'));
	photo = jQuery('#image-'+post_id+' .photo').attr('src');
	ar = photo.split('/');
	photo = ar[2];

	//ajax server
	jQuery.getJSON(
		'/wp-admin/admin-ajax.php',
		{
			type: 'POST',
			photo: photo,
			post_id: post_id,
			action: 'bpi_album_add',
			cat: album.current_cat,
			wp_nonce: album.nonce
		},
		function(json){

			//stop loader
			jQuery('#button-'+json.post_id).show();
			jQuery('#loader-'+json.post_id).hide();

			//build html
			html = '<div class="album-item" id="album-item-'+json.post_id+'">';
			html += '<img src=\"/thumbnails/'+json.photo+'\"/>';
			html += json.slug;
			html += ' |<a href=\"javascript:album.remove(\''+json.post_id+'\')\">Remove</a>|';
			html += '</div>';

			//append thumb to cart
			jQuery('#album').append(html);
		}
	);

	//show menu
	jQuery('#album-menu').show();
}

//remove item
album_class.prototype.remove = function(post_id){

	//confirm
	if(!confirm('Remove Photo from cart?')) return;

	jQuery.getJSON(
		'/wp-admin/admin-ajax.php',
		{
			type: 'POST',
			action: 'bpi_album_photo_remove',
			post_id: post_id,
			wp_nonce: album.nonce
		},
		function(json){

			if(json.ok){
				alert('#album-item-'+json.post_id);
				jQuery('#album-item-'+json.post_id).remove();
			}
			else alert('error removing photo');
		}
	)

}

//empty cart
album_class.prototype.empty = function(){

	//confirm
	if(!confirm('Are you sure you want to empty your cart?\nAll photos in cart will be removed')) return false;

	//ajax server
	jQuery.getJSON(
		'/wp-admin/admin-ajax.php',
		{
			type: 'POST',
			action: 'bpi_album_empty',
			wp_nonce: album.nonce
		},
		function(json){

			//remove nodes
			if(json.ok){
				jQuery('#album > .album-item').each(function(){
					jQuery(this).remove();
				});
			}
			else alert('error emptying your cart');
		}
	);

	//hide menu
	jQuery('#album-menu').hide();
}

//agree to terms and conditions
album_class.prototype.conditionsAgree = function(){

	if(jQuery('#t-and-c:checked').val()) return true;
	else{
		alert('You must agree to the terms and conditions');
		return false;
	}
}

/*
 * side bar menu
 *		this must be created after the album class
 */

//construct
function menu_class(){

	//get current cat id from album class
	this.current_cat = album.current_cat;

	//time out handle
	this.timer = 'test';

	this.init = function(){
		//mouse over events
		jQuery('.category-list li').hover(this.over,this.out);
		this.show_current();
	}
}

//current cat
menu_class.prototype.show_current = function(){
	
	if(menu.current_cat){
		
		//show marker, and sub-menu
		jQuery('img','.cat-item-'+menu.current_cat).remove();
		jQuery('.cat-item-'+menu.current_cat).append('<img src="/images/menu-bubble-arrow.gif"/>');
		var sub = jQuery('ul', '.cat-item-'+album.current_cat);
		bpi_show_menu(sub);
	}
}

//mouse over
menu_class.prototype.over = function(){

	//clear timer
	clearTimeout(menu.timer);

	//if no children return
	if(!jQuery('ul',this).length) return;

	//clear any current menus
	menu.hide_all();

	//show arrow
	jQuery(this).append('<img src="/images/menu-bubble-arrow.gif"/>');
	
	//show menu
	bpi_show_menu(jQuery('ul',this));
}

//mouse out
menu_class.prototype.out = function(){
	
	menu.timer = setTimeout("menu.hide_all(true)", 3000);
}

//hide all menus
menu_class.prototype.hide_all = function(reset){

	//hide all
	id = jQuery('.menu-bubble > ul').attr('class');
	jQuery('.category-list li img').remove();
	jQuery('.cat-item-'+id).append(jQuery('.menu-bubble ul'));

	//if all, then show current cat
	if(reset) menu.show_current();
}

