(function ($) {
	// namespace 'm' as merchandising :)
	var m = {
		
		// storage for variables that will be used throughout the application
		queue: {
			 // what we are searching for ..
			 val: null,
			 // first part of every rightnav box id
			 idBase: "searchMerchandising",
			 // id of boxes to include on page
			 boxes: []			
		},
		
		// attach event listeners
		events: function () {
			// wait for search box to get fully loaded
			$ ('#search-container').ready (function () {
				// now attach event listener to search button
				$ ('#search-button').click (m.getXML);
				$ ('#searchField').keypress (function (e) {
					if (e.keyCode == 13) {
						m.getXML ();
					}
				});
			});	
		},
		
		// innitiate ajax request, grabbing xml for this script
		getXML: function () {
			$.ajax ({
  			 	type: "GET",
   				url: "/businesscenter/cpe/html0/166799.xml",
				dataType: "xml",
				success: function (d) { m.init (d) }
			});
		},
		
		// function that will be called once xml is fully loaded
		// this function will take care of generating of proper rightnav boxes
		// x => xml object
		init: function (x) {
			// need to reset all values in queue in case user click search for multiple times (to avoid using old values)
			this.queue.val = $.trim ($ ('#searchField').val ().toLowerCase());
			this.queue.boxes = [];
			
			// remove all already generated boxes (in case this is not first-time search)
			$ ("div[id^='" + this.queue.idBase + "']").remove();			
			
			// loop through all <set> tags in xml
			$ ("set", x).each (function () {
				// value of current set tag
				var v = $.trim ($(this).attr ("val").toLowerCase());
				var box = $ (this).attr ("box");
				
				// let's check if this value is included in search phrase
				if ( m.inXML (v) ) {
					// split box value (it may be multiple ids, eg 1;2;3; .. )
					box = box.split (";");
					// pop out the last thing in array, it makes trouble with .each loop.. :(
					box.pop();					
					// loop through this array
					$ (box).each (function (i) {
						// is this value already in our queues ? if not, we should add it
						if ($.inArray(box[i], m.queue.boxes) == -1) {
							m.queue.boxes.push ( box[i] );
						}									
					});										
				}									 
			});
			
			
			// now we have array of all boxes we need to generate in rightnav
			// lets simply loop through this array and generate boxes in a row
			
			// make a shortcut - pointer to real boxes array
			var $boxes = this.queue.boxes;
			$ ($boxes).each (function (i) {									   
				// box we need to generate
				var box = $ ("box[id='" + $boxes[i] + "']", x);
				var id = m.queue.idBase + $boxes[i];
				
				// alt tag is a bit complicated as it can be empty, which returns "undefined"
				var alt = box.attr ("imageAlt") ? box.attr ("imageAlt") : "";
				
				// create an empty div and append it to rightnav
				$ ('#ibm-content-sidebar').append ("<div id=\"" + id + '" class="ibm-container">');
				
				$ ('#' + id).html ( '<h2>' + box.attr ("headline") + '</h2>' +
					'<div class="ibm-container-body"><img src="' + box.attr ("image") + '" alt="' + alt + '" />' +
					'<p>' + box.attr ("text") + '</p>' +
					'<ul class="ibm-link-list"><li><a class="' + box.attr ("urlIcon") + '" href="' + box.attr ("url") + '">' + box.attr ("urlText"));
			});
			
		},
							
		// check if this keyword is in xml
		inXML: function (a) {
			// search phrase
			var phrase = m.queue.val;
			// a = current value in xml
			a = a.split (";");
			for (var i = 0; i < a.length; i ++ ) {
				if (phrase.indexOf (a[i]) > -1) {
					return true;
				}
			}
			
			return false;
		}
	};
	
	m.events ();
	
})(jQuery);
			 