//=============================================================================
//Start CurrentPage object
//=============================================================================
function CurrentPage(){}

CurrentPage.prototype.swap = function(el1, el2){
  $(el1).hide();
  $(el2).show();
};

CurrentPage.prototype.updateDimensions = function() {
  var curColumnRightHeight = $('#column_right').height();
  var mainContentHeight = $('#main_content').height();
  if (mainContentHeight < curColumnRightHeight)
    $('#main_content').height(curColumnRightHeight);

}

CurrentPage.prototype.showLoginBox = function(message) {
    if(message) {
        $('#ontop_login .login_message').html(message);
    } else {
        $('#ontop_login .login_message').html('');
    }
    $('#ontop_login').fadeIn(200);
    $('body').scrollTo( $('#ontop_login'), 800, {offset:-10} );

    return false;
}

CurrentPage.prototype.hideLoginBox = function() {
    $('#ontop_login').fadeOut(200);
    return false;
}

CurrentPage.prototype.showLogoutBox = function() {
    $('#progress_bar_container').show();
    $('#progress_bar').hide();
    $('#logout_box_container').show();
    return false;
}

CurrentPage.prototype.showRegisterBox = function() {
    $('#progress_bar_container').show();
    $('#progress_bar').hide();
    $('#register_box_container').show();
    return false;
};

CurrentPage.prototype.filterAjaxResponse = function(response, fullUpdate) {
    if (fullUpdate) {
        $('#products_table').replaceWith(response);
        $('#show_next_page a').removeClass('hidden');
        $('#show_prev_page a').addClass('hidden');

        //set up new aty of total pages of filtered products
        var totalPages = Math.ceil(parseInt($('#total_filtered_products').val()) / DEFAULT_ITEMS_PER_PAGE);
        $('#total_pages').val(totalPages);
    } else {
        $('#products_table>tbody').append(response);
    }

    $('#tpl_filtered_qty').text($('#total_filtered_products').val());
    $('#tpl_total_qty').text($('#total_all_products').val());

    return false;
};
//=============================================================================

var currentPage = new CurrentPage();

$(document).ready(function(){
  //currentPage.updateDimensions();
	$(".rate a").live('click',function(){
		return false;
	});
	
	setRating()
    var docWidth = $(window).width();
    var docHeight = $(window).height();
    $("#ontop_login").css({'width':docWidth+'px','height':docHeight+'px','left':-(docWidth+690)/2+'px'});
    $(".login-container").css({'margin-left':(docWidth-700)/2+'px'});
    $(".products .plus").live("click",function(){
        $(this).toggleClass("minus");
        $(this).parents("tr").nextAll(".hide").toggleClass("hidden");
        return false;
    });
    $('#les_mer').click(function(){currentPage.swap('#text1', '#text2');return false;});
    $('#skjul').click(function(){
        currentPage.swap('#text2', '#text1');
        return false;
    });

    if ($('#credit_cards tr td').length > 0) {
        $.tablesorter.addWidget({
        id: "columnHighlight",
        format: function(table) {
        $("td", table.tBodies[0]).removeClass("sortedeven").removeClass("sortedodd");
        var ascSort = "th." + table.config.cssAsc;
        var descSort = "th." + table.config.cssDesc;

        $(table.tHead).find(ascSort).add($(table.tHead).find(descSort)).each(function() {
            $("tr:visible", table.tBodies[0]).find("td:nth-child(" + ($("thead th", table).index(this) + 1) + ")")
                .filter(':even').addClass("sortedeven").end()
                .filter(':odd').addClass("sortedodd");
        });
      }
    });


    $('#credit_cards').tablesorter({widgets: ['zebra', 'columnHighlight']})
        .tablesorterPager(
                        {container:$('#pager'),
                         positionFixed:false
                        }
        );
  }

  var switcher = $('#diagram_switcher select');
  if (switcher.length > 0) {
    $(switcher).change(function(){
      document.location = this.value;
    });

  }

  $('#logged_no_link').click(currentPage.showLoginBox);
  $('.gotologin').click(currentPage.showLoginBox);
  $('#logged_yes_link').click(currentPage.showLogoutBox);
  $('#register_link').click(currentPage.showLoginBox);
	$('.befirst').click(function(){
		$('body').scrollTo( $('#comment-form'), 800, {offset:-10} );
	});
  $(".tablesorterPager  .select").click(function(){
    $(this).next(".select-list").toggleClass("hidden");
    return false;
  });  
  $(".tablesorterPager .select-list a").click(function(){
    $(this).parent().siblings("select").val($(this).html());
    $(this).parent().siblings("select").trigger("change");
    $(this).parent().prev(".select").html($(this).html());
    $(this).parent().addClass("hidden");
    return false;
  });
});
function showErrorMessage(error) { 
    message = '';
    if(typeof(error) == 'object') {
        $.each(error,function(i, val){
           message = message + i + ': ' + val + '\n';
        });
    } else {
        message = error;
    }
    alert(message);
    return false;
}
function showSuccessMessage(success) {
    message = '';
    if(typeof(success) == 'object') {
        $.each(success,function(i, val){
           message = message + i + ': ' + val + '\n';
        });
    } else {
        message = success;
    }
    alert(message);
    return false;
}
function setRating() {
	if($(".rate").length>0){
		for(var i=0;i<$(".rate").length;i++){
			var rating = $(".rate").eq(i).attr('rating');
			for(var j=0;j<rating;j++){
				$(".rate").eq(i).children('a').eq(j).addClass('nearly-rated');
			}
		}
	}
}
