/*
Theme Name: Kelontong
Theme URI: http://www.icreativelabs.com/
Description: None.
Version: 1.0.0
Author: iCreative Labs
Author URI: http://www.icreativelabs.com
Tags: blue, black
	Kelontong v1.0.0
	http://www.icreativelabs.com/
	This theme was designed and built by None,
	whose blog you will find at http://www.icreativelabs.com/
	The CSS, XHTML and design is released under GPL:
	http://www.opensource.org/licenses/gpl-license.php
*/
(function(){
	if ( window.ICL ) var ICL = window.ICL;
	
	var ICL = window.ICL = function() {return new ICL.init();};
	
	ICL.apply = function(o, c, defaults){
	    if(defaults){ICL.apply(o, defaults);}
	    if(o && c && typeof c == 'object'){for(var p in c){o[p] = c[p];}}
	    return o;
	};
	
	ICL.apply(ICL, {
		
		boxid: 0,
		version: '1.0.0',
		
		store_sidebar_wishlist: function(data) {
			var container = jQuery('#icl_widget_wishlist_container');
			if (container[0] != null) {
				container.find('.icl_wishlist_sidebar_item').remove();
				
				if (data.length > 0) {
					var i_item = 0;
					var item = {};
					var ID = '';
					
					for(i_item in data) {
						item = container.find('.icl_wishlist_sidebar_item_example').clone();
						item.removeClass('icl_wishlist_sidebar_item_example').addClass('icl_wishlist_sidebar_item');
						item.attr('id', item.attr('id') + data[i_item].ID);
						item.css('display', '');
						item.find('.icl_wishlist_sidebar_item_title').attr('href', data[i_item].permalink).append(data[i_item].title);
						item.find('.icl_wishlist_sidebar_btn_remove').attr('rel', data[i_item].ID).bind('click', function(){
							jQuery('#icl_wishlist_sidebar_product_' + jQuery(this).attr('rel')).submit();
						});
						item.find('.icl_wishlist_sidebar_form').attr('id', item.find('.icl_wishlist_sidebar_form').attr('id') + data[i_item].ID);
						item.find('.icl_wishlist_product_id').val(data[i_item].ID);
						item.appendTo(container);
					}
					
					jQuery('.icl_wishlist_sidebar_form').submit(function(){
						return ICL.remove_sidebar_wishlist(this);
					});
					
					delete item;
					delete i_item;
				}
			}
		},
		
		clear_sidebar_wishlist: function(url) {
			jQuery('#icl_wishlist_sidebar_btn_clear').css('display', 'none');
			jQuery('#icl_wishlist_sidebar_loading_image').css('display', '');
			
			jQuery.ajax({
				type: "GET",
				dataType: "json",
				url: url,
				error: function(http, error){
					jQuery('#icl_wishlist_sidebar_btn_clear').css('display', '');
					jQuery('#icl_wishlist_sidebar_loading_image').css('display', 'none');
				},
				success: function(json){
					if (json) {
						if (json.error.code > 0) {
							alert(json.error.description);
						} else {
							ICL.store_sidebar_wishlist(json.payload);
						}
					}
					
					jQuery('#icl_wishlist_sidebar_loading_image').css('display', 'none');
					jQuery('#icl_wishlist_sidebar_btn_clear').css('display', '');
					
					delete json;
				}
			});
		},
		
		remove_sidebar_wishlist: function(form_node) {
			var product_id = jQuery(form_node).find('.icl_wishlist_product_id').val();
			var container = jQuery('#wishlist_sidebar_' + product_id);
			var fvs = jQuery(form_node).serialize();
			container.find('.icl_wishlist_loading_image').css('display', 'inline');
			container.find('.icl_wishlist_sidebar_btn_remove').css('display', 'none');
			
			jQuery.ajax({
				type: "POST",
				dataType: "json",
				url: 'index.php',
				data: fvs,
				error: function(http, error){
					container.find('.icl_wishlist_loading_image').css('display', 'none');
					container.find('.icl_wishlist_sidebar_btn_remove').css('display', '');
				},
				success: function(json){
					if (json) {
						if (json.error.code > 0) {
							alert(json.error.description);
							container.find('.icl_wishlist_loading_image').css('display', 'none');
							container.find('.icl_wishlist_sidebar_btn_remove').css('display', '');
						} else {
							ICL.store_sidebar_wishlist(json.payload);
							if (jQuery('#action_wishlist_' + product_id)[0] != null) {
								jQuery('#action_wishlist_' + product_id).css('display', '');
							}
						}
					} else {
						container.find('.icl_wishlist_loading_image').css('display', 'none');
						container.find('.icl_wishlist_sidebar_btn_remove').css('display', '');
					}
					
					delete json;
				}
			});
			
			delete fvs;
			delete product_id;
			delete container;
			
			return false;
		},
		
		noop: function() {}
		
	});
})();