var IBM = {
	defaultSearchString :  'Waarmee kunnen we u helpen?' 
};

$(function() {
IBM.init();
IBM.enhanceForms();
IBM.pollForms();
IBM.searchSubmitProtect();
IBM.component('selector_legend', 'wishitems');
});

IBM.component = function (id, obj) {
	id = document.getElementById(id);
	if (id) {
		IBM[obj].init(id);
	}
};


/**
 * application
 */
IBM.init = function(root) {
	jQuery.fn.extend({
	   check: function() {
	     return this.each(function() { this.checked = true; });
	   },
	   uncheck: function() {
	     return this.each(function() { this.checked = false; });
	   }
	 });
}

IBM.enhanceForms = function(root) {
	var self = this;
	if(!root) root = $(document);
	
	// toggle sections in forms through checkboxes
	if (window.navigator.userAgent.indexOf("MSIE 6.0") == -1) {
		var processedGroups = {};
		$("input.ibmtoggle_switch", root).each(function() {
			
			$(this).click(function() {
				var target = $(this).attr("lb:target");
				if(!this.checked   ) {
				 	$("#subForm_" + target).slideUp();
				 	$("#spanGWQtt_" + target).hide();
				 } else  {
				 	$("#subForm_" + target).slideDown();
				 	$("#spanGWQtt_" + target).show();
				 }
			});
			
			var target = $(this).attr("lb:target");
			if(!this.checked)  {
				$("#subForm_" + target).hide();
				$("#spanGWQtt_" + target).hide();
			}
		});
	}
}

IBM.pollForms = function(root) {
	$("form[id^=pollForm_]").each( function() { 
		var pollId = this.id.substring(9);
		// the ajax for view votes link
		$(this).find("a[rel='poll-viewvotes']").each( function() { 
			$(this).click( function(e) {
				$.get("AjaxPollView?viewResults=true&pollId=" + pollId , {} , function(html){ 
					$("#pollDiv_" + pollId).html( html ).hide().fadeIn("slow");
					BLOKKER.equalizeBoxes();
					IBM.pollForms();
				}); 
				e.preventDefault();
			});	
		} );
		
		
		// the ajax for submit a vote
		$(this).bind("submit", function(e){ 
			
			var params = {}; 
			
			$(this) 
			.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea") 
			.filter(":enabled") 
			.each(function() { params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; }); 
			$("body").addClass("curWait"); 
			$.post(this.getAttribute("action") + "?call=ajax", params, 
				
			function(html){ 
				$("body").removeClass("curWait"); 
				$("#pollDiv_" + pollId).html( html ).hide().fadeIn("slow");
				BLOKKER.equalizeBoxes();
				IBM.pollForms();
			}); 
			e.preventDefault();
		return false; });
	} );
}


IBM.chkbox = {
	subForm : null ,
	box : null,
	
	init : function () {
		$("input.gwActivate[type=checkbox]").each(
			function () {
			var check = $(this);
			box = this;

			check.subForm = $("#subForm_" + check.val() );

			$(check).bind('click', IBM.chkbox.toggleGiftWrap());
		});
	},
	toggleGiftWrap: function() {
		var that = IBM.chkbox, l = 0;
		alert(l);
		//if (0 === l) {
		//	this.subForm.css( {display: 'block'} );
	//		
	//	} else if (1 === l) {
	//		this.subForm.css( { display: 'none' } ) ;
	//	} 
		
	}
	
};

IBM.searchSubmitProtect = function(root) {


	$('form.searchForm').each( function() {
		var thisform = $(this);
		thisform.bind("submit", function(e) {
			var cancelSubmit = false;
			var inputField = thisform.find("input[@name='searchTerm']:first");
			var inputFieldVal = inputField.val().replace(/^\s+|\s+$/g, '');
			if ( inputFieldVal == IBM.defaultSearchString || inputFieldVal == '') {
					cancelSubmit = true;
			}
			if (cancelSubmit) {
				e.preventDefault();
				inputField.focus();
				return false;
			}
		});
	 return true;} );
};




/**
 * components
 */

// product comparison in photo and list view
IBM.wishitems = {
	form : null,
	msg : {
		title           : '',
		compare         : 'Versturen',
		select          : 'Selecteer producten om te versturen',
		compareSelected : 'Verstuur de geselecteerde producten'
	},
	product : {},
	selected : [],
	init : function () {
		var legend = $('#selector_legend');
		this.form = legend.parents('form');
		var that = this;
		$('fieldset.selection').each(
			function () {
				var check = $(this).find('input')[0];
				$(check).bind('click', IBM.wishitems.checkButton);
				var refButton = $('#selector_legend input.submit');
				var	nameValue = (refButton.attr('name') || refButton.attr('id')) + '|' + refButton.val();
				var button = $(
					'<a href="#" title="' + that.msg.title + '" class="button" rel="' +
					nameValue + '"><span><span>' + that.msg.compare + '</span></span></a>'
				);
				$(check).parents('fieldset').append(button);
				button.bind('click', that.submit);
				button.css({display:'none'});
				// the jQuery way is the silly way  :)
				var msg = $('<span class="message">' + that.msg.compare + '</span>');
				$(check).parents('label').append(msg);
				that.product[check.id || check.name] = {
					container : $(this).parents('.product-selection-box'),
					check : check,
					button : button,
					messageBox : msg
				};
			}
		); // $('fieldset.selection').each
		this.setCount();
	},
	submit : function (e) {
		IBM.wishitems.form.submit();
	},
	setCount : function () {
		var that = IBM.wishitems,
			product;
		for (var i in that.product) {
			if (that.product.hasOwnProperty(i)) {
				product = that.product[i];
				if (product.check.checked) {
					//product.container.addClass('keep');
					that.selected.push(i);
				}
			}
		}
		that.toggleStatus();
	},
	checkButton : function () {
		var that = IBM.wishitems;
		var name = this.id || this.name;
		var msg = that.product[name].messageBox;
		var block = that.product[name].container;

		
		if (this.checked) {
			//block.addClass('keep');
			that.selected.push(name);
		} else {
			//block.removeClass('keep');
			that.remove(name);
		}
		that.toggleStatus();
	},
	remove : function (val) {
		var that = IBM.wishitems, selected = that.selected, result = [];
		for (var i = 0, l = selected.length; i < l; i++) {
			if (selected[i] !== val) {
				result[result.length] = selected[i];
			}
		}
		that.selected = result;
	},
	toggleStatus : function () {
		var that = IBM.wishitems, l = that.selected.length;
		if (0 < l) {
			that.loop(function (item, i) {
				if (BLOKKER.inArray(that.selected, i)) {
					item.messageBox.text(that.msg.compareSelected);
					item.button.css({display:''});
				} else {
					item.messageBox.text(that.msg.compare);
					item.button.hide();
					item.button.css({display:'none'});
				}
			});
		} else  {
			that.loop(function (item, i) {
				item.messageBox.text(that.msg.compare);
				item.button.css({display:'none'});
			});
		} 
		
	},
	loop : function (callback) {
		var product = IBM.wishitems.product, i, item;
		for (i in product) {
			if (product.hasOwnProperty(i)) {
				item = product[i];
				callback(item, i);
			}
		}
	}
};


/**
 * helper functions
 */


