/*
* $Id: main.js 708 2011-08-23 11:41:27Z john $
*/

var lastNavTarget=null;
var refSubMenuTimout=null;

var openSubNav = function(sel){
	if($(sel).is('.waiting')){
		$(sel).removeClass('waiting');	
		$('.open').removeClass('open');
		$(sel).addClass('open');
		$('#sub-navigation-wrapper, .sub-menus').hide();
		var navopen = $(sel).closest('li').attr('id');
		$('#sub-navigation-wrapper, #sub-navigation, #sub-'+navopen).show();
		$('#sub-'+navopen).masonry('reload');
	}
}

$(function(){

    // Reveal the login form
    $('.login').click(function(){
    	$('#login-form').toggle();
		$(this).toggleClass("login-open");
		if($(this).is('.login-open')){
			$('#search-overlay').hide();
		}
		return false;
   	});
	
	$('.sub-menus').masonry({
	  columnWidth: 236, 
	  singleMode: true, 
	  itemSelector: '.ms',
	  resizeable: true
	});
	
	// use a different sub nav action for mobile devices...
	var deviceAgent = navigator.userAgent.toLowerCase();
	var isMobileTouchDevice = deviceAgent.match(/(iphone|ipod|ipad|android|webos)/)==null ? false : true;
	if(isMobileTouchDevice){
	
		/*
		* attach hover and click to the main nav for mobile devices
		*/
		$("#navigation a").click(function(e){
			if(!$(this).is('.open')){
				e.preventDefault();
				$(this).addClass('waiting');
				openSubNav(this);
				return false;
			}
		});
	
	}else{
		
		/*
		* attach hover and click to the main nav for desktop/other devices
		*/
		
		// subnav hover (focus)
		$("#navigation a").hover(
			function(e){
				lastNavTarget=this;
				$(this).addClass('waiting');
				refSubMenuTimout=setTimeout('openSubNav(lastNavTarget);',350);
			},
			function(e){
				$(this).removeClass('waiting');	
			}
		);
		
		// subnav hover (blur)
		$('#container, #sub-navigation-wrapper').mouseleave(function(){
			if(typeof refSubMenuTimout!=='undefined'){
				clearTimeout(refSubMenuTimout);
			}
			$('#sub-navigation-wrapper').removeClass('active').hide();
			$('.sub-menus').hide();
			$('#navigation a').removeClass('open');
		});
	}
	//home hero menu reveal
	var showSearchResults = function(){
		if(this.value.length>2){
			$.ajax({
				type: "POST",
				url: "/search/ajax-search-numarknet",
				data: {searchterm:this.value},
				dataType: "html",
				success: function(data){
					
					$('#login-form').hide();
					$('.login').removeClass('login-open');
					
					$('#search-overlay').remove();
					$('#header-inner').append(data);
					$('#search-overlay').show();
					$('body').click(function(e){
						e.stopPropagation();
						$('#search-overlay').hide();	 
					});	
					
					// ajax search clicks to google analytics
					$(".search-result a").click(function(e){
						_gaq.push(['_trackPageview','/search/numarknet?searchterm='+$('#searchterm').val()+'&search=Search&category=dropdown']);
						_gaq.push(['_trackEvent', 'Quick Search', 'Click', $(this).attr('href')]);
					});
					
				}
			});
		}
	}
	$("#searchterm").click(showSearchResults);
	$("#searchterm").keyup(showSearchResults);
	
	//home hero menu revesal
	$('.showmenu').click(function(){
    	$('#hero-menu').toggle();
		$(this).toggleClass("menu-open");
		return false;
   	});
	
	// stop hero auto rotation on click
	$(".jc-next, .jc-prev").click(function(){
		clearInterval(jcInterval);						   
	});
	
	// catch clicks on nhero nav, hide nav and stop auto rotation 
	$("#hero-menu ul li a").click(function(){
		clearInterval(jcInterval);
		$('.showmenu').click();		   
	});

	// register the first impression on hero baners
	var intInitialBannerID = $(".home-carousel li:first-child").attr('data-id');
	if(intInitialBannerID!=null){
		$.get('/banner/hero/impression/'+intInitialBannerID);
		$('.home-carousel li[data-id="'+intInitialBannerID+'"]').addClass('impression');
	}
	
	//member customise section
	$('.customise').click(function(){
    	$('#member-customise').slideToggle(200);
		$(this).toggleClass('custom-open');
		return false;
   	});
	
	
	// widget
	if($('.member-widgets').length>0){
		
		//
		$('.member-widgets').sortable({
			connectWith: ".member-widgets",
			placeholder: 'widget-space'
		});	
		
		//
		$('.member-widgets').bind('sortstop',function(event, ui){
				
				//
				var arrWidgetData = [];
				
				$('.widget').each(function(i){
					var data = {};					   
					data.id=$(this).attr('data-model-id');				   
					data.x=$(this).parent().attr('data-column-number');
					data.y=i;
					if($(this).is('.collapsed')){
						data.collapsed=1;
					}else{
						data.collapsed=0;
					}
					arrWidgetData.push(data);
				});
				
				//
				$.ajax({
				  type: 'POST',
				  url: '?save-user-config',
				  data: {widgets:arrWidgetData}
				});
				
		});
		
		// hide all collapsed widget content on load
		$('.member-widgets .collapsed .widget-content').hide();
		
	}
	
	// widget collapse
	$('.widget .collapse').click(function(){
		
		//
		var widget = $(this).parent().parent();
			
		//
		if(widget.is('.collapsed')){
			widget.removeClass('collapsed');
			$('.widget-content',widget).show();
		}else{
			widget.addClass('collapsed');
			$('.widget-content',widget).hide();
		}
		
		//
		$(".member-widgets:first-child").trigger("sortstop");
		
		return false;
   	});
	
	// widget remove
	$('.widget .remove').click(function(){
		
		//
		var widget = $(this).parent().parent().remove();
		
		// un-check the selection checkbox
		
		//console.debug('widget-selection-'+widget.attr('data-model-id'));
		
		$('#widget-selection-'+widget.attr('data-model-id')).attr('checked','');
		
		//
		if($('.member-widgets').length>0){
			$(".member-widgets:first-child").trigger("sortstop");
		}else{
			
			//
			$.ajax({
			  type: 'POST',
			  url: '?save-user-config',
			  data: {widgets: 'empty'}
			});
		}
		return false;
   	});
	
	
	//pharmacy select
	$('.pharmacy-select a').click(function(){
    	$('.pharmacy-select-form').toggle();
		$(this).toggleClass('open');
		return false;
   	});
		
	// fancy box help tips
	try{
		$('a.help[href!=]').click(function(e){
			e.preventDefault();
			return false;
		});
		/*$('a.help[href!=]').fancybox({
			titleShow:false,
			overlayColor:'#00929f',
			overlayOpacity:0.8,
			scrolling:'no'
		});*/
	}catch(er){
		// disregard	
	}
	
	//paperwork toggle
	$('#table-controls-top h3 a').click(function(){
    	$('#table-filters').toggle();
		$(this).toggleClass('open');
		return false;
   	});
	
});

