/***
Toggle boxes and save state
***/
function initSideBoxes() {
	$('div.box').each(function(i){
		var $button = $('div.box a.sbutton').eq(i);
		var $body = $('div.box div.body').eq(i);
		var $box = $(this); 
		var $ident = $(this).attr('id');
		
		if ($.cookie($ident)=='closed') {
			$body.hide();
			$box.addClass('close');
			$button.toggle(
				function() {
					$.cookie($ident,'open');
					$box.removeClass('close');
					$body.slideDown('fast');
				},
				function() {
					$.cookie($ident,'closed');
					$box.addClass('close');
					$body.slideUp('fast');
				});
		}
		else {
			$button.toggle(
				function() {
					$.cookie($ident,'closed');
					$box.addClass('close');
					$body.slideUp('fast');
				},
				function() {
					$.cookie($ident,'open');
					$box.removeClass('close');
					$body.slideDown('fast');
				});
		}
	});
}

/***
Toggle tabs
***/
function changeTab(tabid,setid) {
	if (!setid) var setid = '';
	else setid = '#'+setid+' ';;
	$(setid+'ul#relatedtabs li a.active').each(function(){$(this).removeClass('active');});
	$(setid+'ul#relatedtabs li a#'+tabid+'link').addClass('active');
	$(setid+'div.related').each(function(){$(this).css('display','none');});
	$(setid+'div#'+tabid).css('display','block');
}

/***
Balloon product add popup
***/
function initBalloons()
{
	$('a.imgs').mouseover(function() {
		$('input[name=addtype]')[0].checked = false;
		$('input[name=addtype]')[1].checked = false;
		$('input[name=addtype]')[2].checked = false;
		$('#productId').val($(this).attr('id').substr(1));
		var offset = $(this).offset();
		$('#productBalloon').css({
			left: offset.left + 43 + 'px',
			top: offset.top + 47 + 'px'
		}).bind('mouseleave', function() {
			$('#productBalloon').hide();
		}).show();
	});
}

/***
Balloon product add button action
***/

function addProductToProfile() {
	var addtype = $('input[name=addtype]:checked').val();
	var productid = $('#productId').val();
	if (!addtype) alert('Kies eerst: heb ik, wil ik of had ik.');
	else {
		$.post(
			'/wp-content/themes/mobilyz/actions/addproduct2profile.php', 
			{followactivity:"1",producttype:"hardware",productid:productid,addtype:addtype}, 
			function(data){
				if (data=='1') alert('Product is toegevoegd aan profiel.');
				if (data=='2') alert('Product was al toegevoegd aan profiel.');
				if (data=='3') showPopup(121); /*show logged in popup*/
			});
	}
}

// display greet box when document is ready
jQuery(document).ready(function($){
  // set default referrer if none found
  var ref = "default";
  if(document.referrer){
    ref = document.referrer;
    m = ref.match(/^(http:\/\/[^\/]+)/i);
    if(m){
      ref = m[1].replace(".", "_");
    }
  }
  // send request
  $.ajax({
    type: "GET",
    url: "index.php",
    data: "wpgb_public_action=query&visit_delta="+wpgb_get_delta(ref)+"&closed="+wpgb_get_closed(ref)+"&logged_in="+wpgb_get_logged_in()+"&referrer="+encodeURIComponent(document.referrer)+"&url="+encodeURIComponent(document.location)+"&title="+encodeURIComponent(document.title),
    success: function(resp) {
      if(resp != ''){
        // show greeting message
        $("#greet_block").hide().html(resp).fadeIn("def");
        // bind close action
        $("#greet_block_close").click(function(event){
          event.preventDefault();
          wpgb_set_cookie("wpgb_closed-"+ref,new Date(),wpgb_cookie_exp);
          wpgb_set_cookie("wpgb_closed-"+ref,new Date(),wpgb_cookie_exp);
          $("#greet_block").fadeOut("def");
        });
        // bind search action (if any)
        $("#greet_search_link").click(function(){
          action = $(this).attr("action");
          if(action == "show"){
            $("#greet_search_results").slideDown();
            $(this).attr("action","hide");
            $("#greet_search_link_text_show").hide();
            $("#greet_search_link_text_hide").show();
            $("#greet_search_text_show").hide();
            $("#greet_search_text_hide").show();
          }
          else if(action == "hide"){
            $("#greet_search_results").slideUp("fast");
            $(this).attr("action","show");
            $("#greet_search_link_text_show").show();
            $("#greet_search_link_text_hide").hide();
            $("#greet_search_text_show").show();
            $("#greet_search_text_hide").hide();
          }
        });
      }
    }
  });
  // set cookie
  wpgb_set_cookie("wpgb_visit_last-"+ref,new Date(),wpgb_cookie_exp);
});

/***
Init functions
***/
$(document).ready(function(){
	initBalloons();
	/*initSideBoxes();*/
	initLogin();

	$('#notice a.close').click(function() {
		$('#notice').slideUp('fast');
	});

	$('ul.popupMenu a').hover(function() {
		showPopupMenu($(this).attr('id'));
	}, function() {
		popupMenuTimeout = setTimeout('hidePopupMenu();', 200);
	});
});