/**
 * Code for switching and persisting browse layout.
 */
if (!console) var console = {log: function() {}};

// set up Argos Product Comparison namespace
if (!argos) var argos = {};
if (!argos.lister) argos.lister = {};

argos.lister.view = {
	layout : "listview",	
		
	// determines display type of the product lister in browse/search
	switchViewEvent : function() {			
	
		// body always loads with the listview class by default // if cookie is set to gallery view, change body class 
		if (argos.lister.view.layout != "listview") {	
			var viewElem = "#switchview";
			// proceed when body.listview is ready 			
			$(viewElem).removeClass('listview');
			$(viewElem).addClass('galleryview');		
				
			// substitute special class when in gallery view to overide legacy css
			$('.browselister .product .special').removeClass('special').addClass('specialremoved');	
			
			// change the viewselector link state once the first product is ready
			$.onElementReady(viewElem, function() {		
				$('.viewselector').removeClass('listview').addClass('galleryview');
			});
		}
	},
	
	
	
	// applies new additional sizing class to galleryview <ol>s if condition is met
	gallerySizeAdjust : function() {									
		$('#switchview .browselister .withproductflags').parent().addClass('withproductflags');
	},

	init : function () {			

 		//alter the sort/paging form items
 		$(".search #listerrhs .sortoptions .listerpageoptions input.submit").hide(); 
 		if ($.browser.msie && /6.0/.test(navigator.userAgent)){
 			$(".search #listerrhs .sortoptions .listerpageoptions").css({width:"310px"}); 
 			$(".search #listerrhs .sortoptions form.showall").css({width:"335px"}); 
 		}
 		$(".search #listerrhs .sortoptions .listerpageoptions").css({'min-width':"310px"}); 
 		
 		$(".search #listerrhs .sortoptions .listerpageoptions label").css({paddingLeft:"5px"});
		$(".search #listerrhs .sortoptions .listerpageoptions label.sorting").css({marginLeft:"5px"});
		
		
		//prep sort/paging selects for autosubmission
		$(".search #listerrhs .sortoptions .listerpageoptions select.sortBy").bind("change", function(){
			var formToSubmit = $(this).parent().get(0).form;
			$(formToSubmit).submit();
		});
			
		//show the list/gallery view switch 		
 		$('.viewselector').show(); 		
		
		// if the listview/galleryview links are clicked, change parent container classes accordingly 
		// set cookie value accordingly also (cookie to expire on close of session) 	
		$ ('a.linklist').click(function() {
			$('.viewselector').removeClass('galleryview').addClass('listview');
			// JamesB - revert specialremoved class to special when in list view 
			$('.browselister .product .specialremoved').removeClass('specialremoved').addClass('special');
			// now change the view
			$('.galleryview').removeClass('galleryview').addClass('listview');		
			argos.page.setCookie("argosswitchview","listview",0);
			return false;
		});
				
		$ ('a.linkgallery').click(function() { 	
			$('.viewselector').removeClass('listview').addClass('galleryview');
			// substitute special class when in gallery view to overide css 
			$('.browselister .product .special').removeClass('special').addClass('specialremoved');
			// now change the view
			$('.listview').removeClass('listview').addClass('galleryview');		
			argos.page.setCookie("argosswitchview","galleryview",0);
			return false;
			
		});
		
		
	} // end initEvents()
}

// set the saved or default layout - need to execute before body element ready
argos.lister.view.layout = argos.page.getCookie("argosswitchview") || "listview";

$(document).ready(  	
	function() {	
		//  only show the list / gallery view selector if JavaScript is enabled  
		argos.lister.view.init();			
	}
);


if (!argos.expandingmenu) argos.expandingmenu = {};

argos.expandingmenu.expandingmenudisplay = function(toggleAreaNode, options){	

	var toggleAreaNode = toggleAreaNode || null;
	if(!toggleAreaNode) return;
		
	var config = {
		//defaults
		TOGGLE_OBJECT:'a.expander',
		TOGGLE_IGNORE_CLASS:'expander_ignore',
		MENU_OBJECT_CLASS:'expandermenu',
		CLICKABLE_OBJECT_CLASS:'toggleobject', 
		TOGGLE_INSERT_HTML: null, //must have clickable class already applied
		TOGGLE_INSERT_HTML_POSITION: 'after', 
		TOGGLE_INSERT_HTML_OPEN_HTML: null, 
		TOGGLE_INSERT_HTML_CLOSE_HTML: null, 
		TOGGLE_CLOSED_CLASS: 'expanderclosed',
		TOGGLE_OPEN_CLASS: 'expanderopen'		
	};

	// shorten namespace
	var expandingmenu = argos.expandingmenu.expandingmenudisplay;
	
	if(typeof options === "object") {
		config.TOGGLE_OBJECT = (typeof options.TOGGLE_OBJECT === "string") ? options.TOGGLE_OBJECT : config.TOGGLE_OBJECT;
		config.TOGGLE_IGNORE_CLASS = (typeof options.TOGGLE_IGNORE_CLASS === "string") ? options.TOGGLE_IGNORE_CLASS : config.TOGGLE_IGNORE_CLASS;
		config.MENU_OBJECT_CLASS = (typeof options.MENU_OBJECT_CLASS === "string") ? options.MENU_OBJECT_CLASS : config.MENU_OBJECT_CLASS;
		config.CLICKABLE_OBJECT_CLASS = (typeof options.CLICKABLE_OBJECT_CLASS === "string") ? options.CLICKABLE_OBJECT_CLASS : config.CLICKABLE_OBJECT_CLASS;
		config.TOGGLE_INSERT_HTML = (typeof options.TOGGLE_INSERT_HTML === "string") ? options.TOGGLE_INSERT_HTML : config.TOGGLE_INSERT_HTML;
		config.TOGGLE_INSERT_HTML_POSITION = (typeof options.TOGGLE_INSERT_HTML_POSITION === "string") ? options.TOGGLE_INSERT_HTML_POSITION : config.TOGGLE_INSERT_HTML_POSITION;
		config.TOGGLE_INSERT_HTML_OPEN_HTML = (typeof options.TOGGLE_INSERT_HTML_OPEN_HTML === "string") ? options.TOGGLE_INSERT_HTML_OPEN_HTML : config.TOGGLE_INSERT_HTML_OPEN_HTML;
		config.TOGGLE_INSERT_HTML_CLOSE_HTML = (typeof options.TOGGLE_INSERT_HTML_CLOSE_HTML === "string") ? options.TOGGLE_INSERT_HTML_CLOSE_HTML : config.TOGGLE_INSERT_HTML_CLOSE_HTML;
		config.TOGGLE_CLOSED_CLASS = (typeof options.TOGGLE_CLOSED_CLASS === "string") ? options.TOGGLE_CLOSED_CLASS : config.TOGGLE_CLOSED_CLASS;
		config.TOGGLE_OPEN_CLASS = (typeof options.TOGGLE_OPEN_CLASS === "string") ? options.TOGGLE_OPEN_CLASS : config.TOGGLE_OPEN_CLASS;
	}
	
	//ensure a corresponding expanding menu relationship exists for the toggle
	var toggles = $(toggleAreaNode).find(config.TOGGLE_OBJECT), toggle, relatedMenu, matchVal,toggleInsert,toggleClickable;

	for(var i=toggles.length-1; i>=0;i--) {
	
		toggle = toggles[i];
		//skip if ignore class applied via markup
		if($(toggle).hasClass(config.TOGGLE_IGNORE_CLASS)) continue;

		//match toggle to menu
		matchVal = $(toggle).attr("rel");
		relatedMenu = $(toggleAreaNode).find("#"+matchVal)[0] || null;
		
		// skip if no related menu
		if(!relatedMenu) continue;
	
		//insert additional markup into toggle if exists
		if(config.TOGGLE_INSERT_HTML){
			switch(config.TOGGLE_INSERT_HTML_POSITION) {				
				case "before" :
					$(toggle).prepend(config.TOGGLE_INSERT_HTML);
					break;
				default :
					$(toggle).append(config.TOGGLE_INSERT_HTML);
			}
			//make the inserted html the toggling item
			toggleInsert = $(toggle).find("."+config.CLICKABLE_OBJECT_CLASS)[0];
			toggleClickable = toggleInsert;
			withInsert = true;

		} else {
			//make the main element the toggling item
			toggleClickable = $(toggle).addClass(config.CLICKABLE_OBJECT_CLASS);
			withInsert = false;
		}
		
		//if page output has forced menus to open on load, then do not close them
		if ($(toggle).hasClass(config.TOGGLE_OPEN_CLASS)) {
		
			if(config.TOGGLE_INSERT_HTML_CLOSE_HTML && toggleInsert){
				$(toggleInsert).html(config.TOGGLE_INSERT_HTML_CLOSE_HTML);	
			}	
			//$(toggleClickable).attr("title","hide menu");	

		} else {
		
			$(toggle).addClass(config.TOGGLE_CLOSED_CLASS);
			$(relatedMenu).hide();
			if(config.TOGGLE_INSERT_HTML_OPEN_HTML && toggleInsert){
				$(toggleInsert).html(config.TOGGLE_INSERT_HTML_OPEN_HTML);	
			}	
			//$(toggleClickable).attr("title","show menu");
			
		}
		new toggleHandler(toggleClickable,toggle,relatedMenu,withInsert);
	}
	
	function toggleHandler(toggleClickable,toggle,relatedMenu,withInsert) {
	
		$(toggleClickable).bind("click", function(){
	
			//is this the inserted element or main element 
			if (toggleClickable === toggle){
				activeToggle = toggleClickable
			} else {
				activeToggle = toggle;
			}
			
			//hide/show menu (and update toggle insert html if required)
			if(applyOpenClasses(activeToggle)) {
				if(withInsert){
					$(toggleClickable).html(config.TOGGLE_INSERT_HTML_CLOSE_HTML);
				}
				$(relatedMenu).show();
				
			} else {
				if(withInsert){
					$(toggleClickable).html(config.TOGGLE_INSERT_HTML_OPEN_HTML);
				}
				$(relatedMenu).hide();
			}	
			return false;
		});
	}
	
	// returns true if menu needs is opening
	function applyOpenClasses(activeToggle) {
	
		var activeToggle = activeToggle;
		var activeState = false;
		//reverse classes
		if($(activeToggle).hasClass(config.TOGGLE_CLOSED_CLASS)){
			activeState = true;	
			$(activeToggle).removeClass(config.TOGGLE_CLOSED_CLASS);
			$(activeToggle).addClass(config.TOGGLE_OPEN_CLASS);
		} else {
			//if the toggle does not have closed class force it closed so second click will correct
			$(activeToggle).removeClass(config.TOGGLE_OPEN_CLASS);
			$(activeToggle).addClass(config.TOGGLE_CLOSED_CLASS);			
		}	
		return activeState;
	}
}
