ys_tmp=new Object();
$(document).ready(function() {
  initCountryNav();

  initLeftNav();
 
  activateTooltips();
  
  initFinderForm();
  
  initTabs();
});


var initCountryNav = function() {
  $('#countrynav ul li').hover(
    function() { $(this).addClass('hover'); },
    function() { $(this).removeClass('hover'); }
  );
}



var initLeftNav = function() {
  $('#leftnav-wrap ul li').hover(
    function() { $(this).addClass('hover'); },
	function() { $(this).removeClass('hover'); }
  );
  $("#lcont ul > li").each(function() {
	 if($(this).find('ul').length>0) $(this).addClass("expand");
  });
}



var activateTooltips = function() {
  yOffset = 10;
  var activated=0;
  $("a.tooltip").hover(function(e) {
	  $(".tooltip_bubble").remove();
	  activated=0;
		this.t = this.title;
		this.title = "";
		var tid="t_"+Math.floor(Math.random()*10000);
		this.tid=tid;
		$("body").append("<p id='"+tid+"' class='tooltip_bubble'>"+ this.t +"</p>");
		xOffset=$('.tooltip_bubble').width()/2;
		$("#"+this.tid)
			.css("top",(e.pageY + yOffset) + "px")
			.css("left",(e.pageX - xOffset) + "px")
			.fadeIn("fast")
		.hover(function(e)  { activated=1; },
			   function() { $(".tooltip_bubble").remove(); }
		);
	},
	function() {
	  this.title=this.t;
	  var tid=this.tid;
		setTimeout(function() { if(activated!=1) $("#"+tid).remove(); }, 200);
	}
  );
}



var addToCart=function(pageId, prop, fn) {
  if(!pageId) return false;
  if(!prop) return false;
  $.get('/wp-content/plugins/spectacular/ajax.php', { 'ys_action' : 'addtocart', 'pgid' : pageId, 'prop' : prop, 'cb' : Math.random() },
  function(data) {
	  if(typeof(fn) == 'function') fn(data);
  }
  , 'json');

  return true;
}



var updateCartNav=function(data) {
  if ($('#cartnav').length < 1) return data.status;

  // update cart count
  if(data.status==true) updateCartCount();
  
  // cart info message
  clearTimeout(window.ys_tmp.tm1);
  $('#cartnav-msg').html('');
  $('#cartnav-msg').html(data.msg).fadeIn('fast');
  window.ys_tmp.tm1=setTimeout(function(){ $('#cartnav-msg').fadeOut('slow'); }, 7000);
  
  // disable option
  if(data.status==true) {
	$('#cartnav select option[value='+data.prop+']').attr('disabled', 'disabled');
  }

  return data.status;
}



var updateCartCount=function() {
  $.get("/wp-content/plugins/spectacular/ajax.php", { 'ys_action': "countcartitems", 'cb' : Math.random() },
  function(data) {
	if(data.msg) $('.ajax-cart-count').html(data.msg);
  }, "json");
} 



var initFinderForm=function() {
	if($('#findercount').length<1) return false;
		
	$("#finderform select").change(updateFinderCount);
	updateFinderCount();
}



var updateFinderCount=function() {
	$('#findercount').css('visibility', 'visible');
	$('#findercount').addClass("loading");
	$.get("/wp-content/plugins/spectacular/ajax.php", { 'ys_action': 'countfinderitems', 'fp' : $("#finderform select#fp").val(), 'fu' : $("#finderform select#fu").val(), 'ff' : $("#finderform select#ff").val(), 'cb' : Math.random() },
	function(data) {
		if(data.msg) {
			$('#findercount').removeClass("loading");
			$('#findercount span').html(data.msg);
		}
		else { $('#findercount').css('visibility', 'hidden'); }
	}, "json");
}



var removeFromCart = function(itemId) {
    $.get("/wp-content/plugins/spectacular/ajax.php", { 'ys_action' : 'removefromcart', 'itid': itemId, 'cb': Math.random() },
    function(data) {
      if(data.js) eval(data.js);
      if(data.status==true) {
        $('table.finder tr.item-'+itemId).remove();
        updateTableCaption();
      }
    }, "json");
}
  


var updateTableCaption=function() {
	$.get("/wp-content/plugins/spectacular/ajax.php", { 'ys_action' : 'countcartitems', 'cb': Math.random() },
			function(data){
				//if(data.js) eval(data.js);
				//if(data.status==true) { 
				$('.ajax-cart-count').html(data.msg);
				//}
	}, "json");
}



var initTabs=function() {
	$('#tabnav li a').click(function() {
		// get id of the tab and parse out the order no.
		try {
			var tabId=$(this).parent().attr('id');
			var tabNo=tabId.match(/.*-(\d+)/)[1]; 
		} catch(e) { }
		
		if(tabNo) {
			$('#tabnav li').removeClass('current-tab-label');
			$(this).parent().addClass('current-tab-label');
			$('.tab-content').removeClass('current-tab-content');
			$('#tab-content-'+tabNo).addClass('current-tab-content');
		}
	});
}