/*----------------------------------------------------


	JSONLOAD.JS


----------------------------------------------------*/


$(document).ready(function(){

	var bodyid = $('body').attr('id');

	// A. Close Response Messages
	//------------------------------------------------------------------------------------------

	$('#msg_response').bind("click", function(){
		if($('#msg_response').is(':visible')){
			$("#msg_response").slideUp();
		}
	});

	// B. Banner Cycle
	//------------------------------------------------------------------------------------------

	if(bodyid == "index"){
		eval($('#banner')
			.cycle({
				fx: 'fade',
				speed: 2000,
				timeout: 6000,
				pause: true
		}));
	}
	
	// C. Sub Navigation
	//------------------------------------------------------------------------------------------

	/*var config = {    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 200,  // number = milliseconds for onMouseOver polling interval    
		over: doOpen,   // function = onMouseOver callback (REQUIRED)    
		timeout: 200,   // number = milliseconds delay before onMouseOut    
		out: doClose    // function = onMouseOut callback (REQUIRED)    
	};
	function doOpen() {
		$('ul:first',this).css('display', 'block');
	}
	function doClose() {
		$('ul:first',this).css('display', 'none');
	}
	$("#nav li").hoverIntent(config);*/
	

	// D. Filtered Browsing
	//------------------------------------------------------------------------------------------	
		
	// Submit filter form
	function ajaxSubmit(obj,paging,sorting,currentlyviewed){
		var formdata = $(obj).parents('form').serialize();	
		if(currentlyviewed != true){ formdata = formdata + "&lastsel="+$(obj).attr('id'); }
		$.ajax({
			url: "/advanced-search-results.php",
			cache: false,
			type: "POST",
			data: formdata,
			success: function(html){
				var breadcrumb = $('#breadcrumb');
				$('#content').html(html);
				$('#content').append(breadcrumb);
				initaliseFilters();
			}
		});
		return false;
	}	
	
	function initaliseFilters(){
		
		// Remove search button
		$("#productfilter input[type=submit]").remove();
		
		// Bind ajax to paging
		// Not done yet as no paging found
		
		// Add currently viewing section
		$('#productfilter > div').prepend('<fieldset id="product-filter-selected"><h4>Selected Filters</h4></fieldset>');
		$('#product-filter-selected').hide();
		
		// Promote selected options
		$('.adv-search-options').each(function(){
			var selected = "";
			var showselected = "";
			
			// Collect current selected options
			$(this).find('li input:checked').parent().each(function(){
				label = $(this).find('label').html();
				var num = label.indexOf("<span>");
				if(num > 0){ label = label.substr(0,num); }
				showselected = showselected + "<span title='" + $(this).find('input').attr('id') + "'>" + trim(label) + "</span>";			
			});
			
			// Add selected options to currently viewing section
			if(showselected){
				$('#product-filter-selected').show();
				$('#product-filter-selected').append(showselected);
				$("#product-filter-selected span").bind('click', function(){
					var id = $(this).attr('title');
					$('#'+id).attr('checked','');
					var obj = $('#'+id);
					ajaxSubmit(obj,1,0,true);
				});
			}
		});
			
		//Hide options where more than 5 exist
		function hideChildren(){
			$('.adv-search-options').each(function(index){
				var children = $(this).find('li').length;
				if(children > 5){
					for(i=6;i<=children;i=i+1){
						$(this).find('li:nth-child('+i+')').hide();
					}
					$(this).find('li:nth-child(5)').after('<li class="view-all">View all</li>').show();
					$('.view-all').click(function(event){
						viewAllClick(this);
					});
				}
			});

		}
		//hideChildren();
		
		// Show/Hide all options
		function viewAllClick(obj){
			$(obj).parent().children().show();
			$(obj).parent().append('<li class="hide">Hide</li>');
			$(obj).parent().children('.view-all').remove();
			$('.hide').click(function(event){
				$(obj).parent().children('.hide').remove();
				hideChildren();
			});
		}

		//Show options for last selected
		var selected = $('#lastsel').val();
		if(selected){
			if($('#'+selected).is(':hidden')){
				var view = $('#'+selected).parent().siblings('.view-all');
				viewAllClick(view);
			}
		}

		// Bind Ajax Function [CLICK EVENT]
		$("#productfilter input").click(function(){
			ajaxSubmit(this,1,0);
		});
		
	}
	
	// If filters exist run initaliseFilters()
	if($('#adv-filter').length > 0){
		initaliseFilters();	
	}


});


// 01. toggleInputbox
//------------------------------------------------------------------------------------------

function toggleInputbox(id,text,userevent){

	var obj = document.getElementById(id);
	if(obj.value == text && userevent != "blur"){
		obj.value = "";
		$('#'+id).addClass("active");
	}else if(obj.value == text){
		$('#'+id).removeClass("active");
	}else if(trim(obj.value) == ""){
		obj.value = text;
		$('#'+id).removeClass("active");
	}else{
		obj.value = trim(obj.value);
	}
	
}

// 02. trim
//------------------------------------------------------------------------------------------

function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

// XXX. Plugins
//------------------------------------------------------------------------------------------

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('(6($){$.J.K=6(f,g){8 5={y:7,l:I,H:0};5=$.u(5,g?{v:f,z:g}:f);8 d,b,k,i;8 h=6(3){d=3.G;b=3.B};8 m=6(3,2){2.4=o(2.4);9((w.x(k-d)+w.x(i-b))<5.y){$(2).D("n",h);2.j=1;c 5.v.t(2,[3])}E{k=d;i=b;2.4=r(6(){m(3,2)},5.l)}};8 C=6(3,2){2.4=o(2.4);2.j=0;c 5.z.t(2,[3])};8 q=6(e){8 p=(e.A=="s"?e.N:e.U)||e.T;R(p&&p!=a){S{p=p.O}P(e){p=a}}9(p==a){c Q}8 3=F.u({},e);8 2=a;9(2.4){2.4=o(2.4)}9(e.A=="s"){k=3.G;i=3.B;$(2).M("n",h);9(2.j!=1){2.4=r(6(){m(3,2)},5.l)}}E{$(2).D("n",h);9(2.j==1){2.4=r(6(){C(3,2)},5.H)}}};c a.s(q).L(q)}})(F);',57,57,'||ob|ev|hoverIntent_t|cfg|function||var|if|this|cY|return|cX||||track|pY|hoverIntent_s|pX|interval|compare|mousemove|clearTimeout||handleHover|setTimeout|mouseover|apply|extend|over|Math|abs|sensitivity|out|type|pageY|delay|unbind|else|jQuery|pageX|timeout|100|fn|hoverIntent|mouseout|bind|fromElement|parentNode|catch|false|while|try|relatedTarget|toElement'.split('|'),0,{}))

