//Search
;(function($){
	$.fn.helpSearch = function(options) {
	if(!this.length){
		return;
	}
		var defaults = {
			url: "/help/services/search",
			data:"dynjs=y",
			dataType: "html",
			method: "POST",
			busy: false,
			items_per_page:10,
			//start_index:0,
			current_page:1,
			link_to:"#",
			prev_text:"&laquo;",
			next_text:"&raquo",
			popularTopicWrapperId:"popularTopicWrapper",
			popularTopicId:"popularTopic",
			searchResultTopicId:"searchResultTopic",
			searchKeywordId:"searchKeyword",
			askBtnId:"askBtn",
			searchFormId:"helpSearchForm",
			searchResultPanelId:"searchResultPanel",
			searchKeywordTitleId:"searchKeywordTitle",
			paginationId:"pagination",
			seeAllTopicsId:"seeAllTopics"
		};
		var opts = jQuery.extend(defaults,options);
		$('#'+opts.searchKeywordId).attr("value", "Example: 'Can I pay for tickets with PayPal?'");
		$('#'+opts.searchKeywordId).addClass('readonlystyle');
		$('#'+opts.askBtnId).attr('disabled','true');
		
		function handlePaginationClickHtml(data, jq){
			$('#'+opts.searchResultPanelId).html("");
			$('#'+opts.searchResultPanelId).append(data);
			$("#"+opts.searchResultPanelId).expand();	
			$("#searchlist").accordion();
		}
		$("#"+opts.popularTopicId).accordion();
		
		$("#helpSearch").removeClass("offscreenOnLoad");
		
		//search;
		$('#'+opts.searchKeywordId).focus(function(){
			this.value='';
			$(this).removeClass('readonlystyle');
			$('#'+opts.askBtnId).removeAttr('disabled');
			$(this).unbind();
		});
		$("#"+opts.searchFormId).submit(function(){
			$("#"+opts.popularTopicWrapperId).hide();
			$("#"+opts.searchResultPanelId).expand();
			$("#"+opts.paginationId).ajaxPagi({
				url: opts.url,
				data:opts.data+"&"+$("#"+opts.searchKeywordId).attr("name")+"="+encodeURIComponent($("#"+opts.searchFormId+" #"+opts.searchKeywordId).val()),
				dataType: opts.dataType,
				method: opts.method,
				items_per_page:opts.items_per_page,
				current_page:opts.current_page,
				link_to:opts.link_to,
				prev_text:opts.prev_text,
				next_text:opts.next_text,
				content_id:opts.searchResultPanelId, //the content to replace;
				callback:handlePaginationClickHtml
			});
			return false;
		});		

		$('#'+opts.seeAllTopicsId).click(function(){
			$(".subTabWrapper").tabs("select", 1);
			return false;
		});
	
		
	}
	

})(jQuery);	

;(function($){
	
	
	$.fn.helpBrowse = function(options) {
		if(!this.length){	
			return;
		}
		var defaults = {
			url: "/help/services/browse",
			data:"dynjs=y",
			dataType: "html",
			busyText: "Placeholder: busyText",			
			browseTopicDiv:"browseTopicDiv",
			defaultTopicDiv:"defaultTopicDiv",
			browseNav:"browseNav",
			defaultTopicNav:"defaultTopicNav",
			defaultTopic:"defaultTopic",
			loadingText:"Placeholder: loadingText"	,
			loadingImage:"/resources/mojito/img/common/loading_21.gif",
			errorText:"Placeholder: errorText"		
		};
		var opts = $.extend(defaults,options || {});

		function handleHelpClickJson(data){
			var newcontent = '';
			// Iterate through a selection of the content and build an HTML string;
			$.each(data.items, function(i,item){
					newcontent += '<dt>' + item.id + item.content +'</dt>';
					newcontent += '<dd>' + item.content + '</dd>';
						});
			// Replace old content with new content;
			$('#browseTopic').html(newcontent);
			$("#browseTopic").accordion();
			// Prevent click eventpropagation;
			return false;
		}
		function handleHelpClickHtml(data){
			// Replace old content with new content;
			$('#browseTopicDiv').html(data);
			$("#browseTopic").accordion();
			// Prevent click eventpropagation;
			return false;
		}			
		
		$("#"+opts.browseNav).accordion();
		$("#"+opts.defaultTopic).accordion();		
		
		//browse;
		$("#"+opts.browseTopicDiv).collapse();
		
		$("#browse").removeClass("offscreenOnLoad");
		
		//$("#browse").css("margin-left","0px");
		
		$("#"+opts.browseNav+" dd a").click(function(){
			var obj=$(this);
			topicId = $(this).attr("name");
			$("#"+opts.defaultTopicDiv).collapse();
			$("#"+opts.browseTopicDiv).expand();
			$.ajax({
				type: "POST",
				url: opts.url,
				data: opts.data+"&helpTopicNodeId="+topicId,
				processData: false,
				dataType: opts.dataType,
				success: function(data){
					if(opts.dataType=="html"){
						handleHelpClickHtml(data);
					}
					else{					
						handleHelpClickJson(data);	
					}
				},
				error: function(){
					newcontent="<div>"+$(obj).text()+"</div><dl id='browseTopic'>"+opts.errorText+"</dl>";
					$('#'+opts.browseTopicDiv).html(newcontent);
				}
			});			
			return false;
		});
		$("#"+opts.defaultTopicNav).click(function(){
			$("#"+opts.defaultTopicDiv).expand();
			$("#"+opts.browseTopicDiv).collapse();
			return false;
		});		
	}
})(jQuery);

//searchKeyword query and fire the 'Ask' button
(function($) {

	/**
	 * A simple querystring parser.
	 * Example usage: var q = $.parseQuery(); q.foo returns  "bar" if query contains "?foo=bar"; multiple values are added to an array. 
	 * Values are unescaped by default and plus signs replaced with spaces, or an alternate processing function can be passed in the params object .
	 * http://actingthemaggot.com/jquery
	 *
	 * Copyright (c) 2008 Michael Manning (http://actingthemaggot.com)
	 * Dual licensed under the MIT (MIT-LICENSE.txt)
	 * and GPL (GPL-LICENSE.txt) licenses.
	 **/
	jQuery.parseQuery = function(qs,options) {
		var q = (typeof qs === 'string'?qs:window.location.search), o = {'f':function(v){return unescape(v).replace(/\+/g,' ');}}, options = (typeof qs === 'object' && typeof options === 'undefined')?qs:options, o = jQuery.extend({}, o, options), params = {};
		jQuery.each(q.match(/^\??(.*)$/)[1].split('&'),function(i,p){
			p = p.split('=');
			p[1] = o.f(p[1]);
			params[p[0]] = params[p[0]]?((params[p[0]] instanceof Array)?(params[p[0]].push(p[1]),params[p[0]]):[params[p[0]],p[1]]):p[1];
		});
		return params;
	}
//Then you could use it like so:
//http://www.stubhub.com/help/?searchKeyword=entering-section-row
//q.searchKeyword returns entering-section-row
	
})(jQuery);	

/* ======+====== Bindings ======+====== */

(function($) {
	$(function() {
		// put bindings here;
		
		$("#byphone").removeClass("hideOnLoad");
		$("#contactUsSubTabs").removeClass("hideOnLoad");
		
		// show/hide subject input on load of form; needs to happen fast so use show/hide;
		$('#contactUsForm #topic').livequery(function() {
			if($('#contactUsForm #topic').val().toLowerCase().indexOf("][other]")!=-1){
				$('#subjectInput').show();
			}else{
				$('#subjectInput').hide();
			}
		});
		// show/hide subject input on change of topic input; needs to happen animated so use expand/collapse;
		$('#topic').livequery('change', function() {
			if( $('#topic').val().toLowerCase().indexOf("][other]")!=-1 ){
				$('#subjectInput').expand();
				
			}else{
				$('#subjectInput').collapse();
			}
		});
		
		$('#reloadContactUsForm').livequery('click', function(){
			$("#contactUsFormDynjsTarget").removeAttr("id").load(this.href);
			return false;
		});
		$("#contactUsFormDynjsTarget:has(#contactUsFormDynjsTarget)").livequery(function(){
			$(this).removeAttr("id");
		});

		$("#eventRequest").roundedCorners({wrapperClass:'rc-panel3 rc-panel3-all'});
		
		$(".panel").roundedCorners({wrapperClass:'rc-panel1 rc-panel1-all',overrideCSSRadius:true});
		
		$(".tabBody").roundedCorners({wrapperClass:'rc-panel3 rc-panel3-notl',overrideCSSRadius:true});
		// change panel if pagination div is present (needs gradation at bottom) and reset if not;
		$("#searchResultPanel:visible").livequery(
			function(){
				$(".tabWrapper .rc-panel3").removeClass("rc-panel3").addClass("rc-panel2");
			}, 
			function(){
				$(".tabWrapper .rc-panel2").removeClass("rc-panel2").addClass("rc-panel3");
			}
		);
		
		$('#defaultTopicNav a').livequery( function() {
					$(this).addSelectedClass();
		});
		
		$('dt.omnbrowse').livequery('click', function(){
			omnitureTags("Browse -" + $(this).text(),$(this).text());
		});
		
		$('dt.omnbottomsearch').livequery('click', function(){
			omnitureTags("Bottom Search -" + $(this).text(),$(this).text());
		});
		
		$('dt.omntopsearch').livequery('click', function(){
			omnitureTags("Top Search -" + $(this).text(),$(this).text());
		});
		$('#topic').livequery('change',function() {
			omnitureTags("Contact - "+ $(this)[0].options[this.selectedIndex].text );
			
		});
			

	});
})(jQuery);	

function omnitureTags(section,queryString){
	var s=s_gi(s_account);
	s.linkTrackVars= "eVar16,prop6"
	s.prop6 = section;
	s.eVar16 = queryString;
	window.setTimeout(function() {
	s.tl(this,'o','searchtrack');
	}, 500);
}

