//initiators for different effects on tabs and forms (mainly used in APP.views.element.to_archive_user_login.ctp)
$(document).ready(function() { 
    $("#userTabTrigger").bind('click', function(){
	    $(this).css('width', '320px');
      $(this).next().hide();
      $("#loginFormWrapper").children("p").html('loading...');
      $("#loginFormWrapper").animate({
        width: "270px"
      }, 1000);
      setTimeout(function(){
        $("#loginFormWrapper").children("p").hide();
        $("#submitForm").show();
      }, 1000);
      setTimeout(function(){
        $("#submitFormEmail").focus();
      }, 1100);
      $("#userTabTrigger").unbind('click');
    });	
    // bind 'myForm' and provide a simple callback function 
    $('#archiveUserLoginForm').ajaxForm({ target: '#loginresult', success: function(){
		$('#submitForm').hide();
		$('#loginresult').show();
	} });
	//search results show
	$(".imgRes").mouseenter(function(){
		$(this).prev("div.highlightRes").show();
	});
	$(".highlightRes").mouseleave(function(){
		$(this).hide("fast");
	});
	//right tab triggers - top up and history
	$("#topUpTrigger").click(function(){
		$("#slidingTopUp").toggle('blind');
    if ($("#slidingHistory").is(":visible")) {
      $("#slidingHistory").hide('blind');
    }		
	});
	$("#userHistoryTrigger").click(function(){
    $("#slidingHistory").toggle('blind');
    if ($("#slidingTopUp").is(":visible")) {
      $("#slidingTopUp").hide('blind');
    } else {
      //$("#slidingHistory").load('/archive/user_history')
      loadPiece("/archive/user_history","#slidingHistory");      
    }
  });
  //
  
  //tab popper from jquery plugin written by the Great edim
	$('.movableTab').tabpop({dev: 2});
	//flash message hide animation
	setTimeout(function(){
    $('.message').animate({
      opacity: 0,
      top: '70%'
    });
  }, 3000);
  //$("#userHistoryTrigger").pageSlide({ width: "350px", direction: "left" });  
});

function serveDocument(confMsg, serve) {
    if (serve == true) {
        var conf = confirm(confMsg);
        if(conf){
            setTimeout(function(){
              $('.searchForm:visible').submit();
            }, 1000);
            return true;
        } else {
            return false;
        }
    } else {
        var conf = alert(confMsg);
        return false;
    }
}

/**
 * Loads in a URL into a specified divName, and applies the function to
 * all the links inside the pagination div of that page (to preserve the ajax-request)
 * @param string href The URL of the page to load
 * @param string divName The name of the DOM-element to load the data into
 * @return boolean False To prevent the links from doing anything on their own.
 */
function loadPiece(href,divName) {    
    $(divName).load(href, {}, function(){
        $(divName).find("a").each(function(i){
          if (!$(this).hasClass('noajax')) {
            $(this).bind('click', function(){
              var thisHref = $(this).attr("href");
              loadPiece(thisHref,divName);
              return false;
            });
          }
        });
    });
}
