
// if (!console)
	// console = {log: function() {}};

var root = '/';
var loader = $('<div id="loading-float"><div><img src="'+root+'skin/frontend/default/pavels/images/ajax-loader.gif"><br><span>Vennligst vent..</span></div></div>');

// function addToCart(evt, ) {
// 	evt.preventDefault();
// 	alert()
// 	return false;
// }

$.extend($.easing,
{
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	}
});

jQuery.fn.pfadeIn = function(duration, callback) {
	var pos = this.css('top');
	this.stop(true, true);
	this.animate({top: '+=40'}, 0);
	this.fadeIn(duration, callback)
		.animate({top: '-=40'}, 
			{duration: duration, 
				easing: 'easeOutCubic', 
				callback: callback,
				queue: false});
}
jQuery.fn.pfadeInDown = function(duration, callback) {
	var pos = this.css('top');
	this.stop(true, true);
	this.animate({bottom: '+=40'}, 0);
	this.fadeIn(duration, callback)
		.animate({bottom: '-=40'}, 
			{duration: duration, 
				easing: 'easeOutCubic', 
				callback: callback,
				queue: false});
}

function toggleMenu(a, b) {
	
}

function showLoader() {
	loader.hide();
	$('body').append(loader);
	loader.pfadeIn('medium');
}

function hideLoader() {
	loader.fadeOut('fast', function() {
		loader.remove();
	});
}

function topNotification(target, message) {
	$('.top-notification').remove();
	var node = document.createElement('div');
	node.className = 'top-notification';
	node.style.display = 'none';
	$(node).html('<div class="icon up-arrow"></div><p>'+message+'</p></div>');
	var bla = target.append(node);
	$(node).pfadeIn('slow');
	$(node).hover(function() {
		$(node).fadeOut('fast', function() { 
			$(node).remove();
		});		
	});
	setTimeout(function() {
		$(node).fadeOut('fast', function() { 
			$(node).remove();
		});
	}, 3000);
}

// -- Document Load Procedure --
$(document).ready(function() {

	// $('.top-dropdown').each(function(i, dd) {
	// 	$(dd.parentNode).mouseover(function() {
	// 		$(dd).pfadeIn('medium');
	// 	});
	// });
	//   
	// fix for firefox:
	$('.product-box .description').css('bottom','300px');

	$('.product-box')
		.mouseenter(function(ev) {
			$(this).find('.description').pfadeInDown('slow');
			})
		.mouseleave(function(ev) {
			$(this).find('.description').fadeOut('slow');			
			});
	
  if (jQuery.browser.msie) {
    $('a.addToCart').each(function() {
      this._bound = true;
    });
    $('a > button').click(function() {
      // alert(this.parentNode.tagName);
      if (!this.parentNode._bound)
        setLocation(this.parentNode.href);
    });
  }
	
	$('a.addToCart').click(function() {
		// alert(this.href);
		var link = this;
		if (/\/cart\/add/.test(this.href)) {
			var url = this.href.replace('/checkout/cart/add/', '/pavels/cart/add/');
			showLoader();
			// Pavels Todo: Håndtere cookies redirect
			$.get(url, {}, function(data, textStatus) {
				hideLoader();
				if (data.error) {
					if (data.redirect)
						setLocation(data.redirect); 
					else
						setLocation(link.href); 
					// alert(data.error);
					return;
				}
				if (data.notice) {
					if (data.redirect)
						setLocation(data.redirect); 
					else
						setLocation(link.href);
					// alert(data.notice);
					return;
				}
				$('.top-link-cart > a').html('<span class="icon"></span>'+data.link_text);
				$('.top-link-cart .top-dropdown').replaceWith(data.link_dropdown);
				topNotification($('.top-link-cart'), data.message);
			}, 'json');
			return false;
		} else {
			setLocation(this.href);
		}
		return false;
	});
	
	// -- Product View --
	// Add increase btn code
	$('#super-product-table .increase').css('display', 'inline-block').click(function() {
		$(this.parentNode).find('input').val(function(old) {
			return parseInt(this.value)+1;
		});
	});
	
	// $('.jqzoom').CloudZoom();
	// .jqzoom({
	// 		zoomWidth: 265*2,
	// 		zoomHeight: 265*2,
	// 		showEffect: 'fadein'
	// 	});
	
	$('a.lightbox').lightBox({
	   imageLoading:			'/js/pavels_lib/images/lightbox-ico-loading.gif',
	   imageBtnPrev:			'/js/pavels_lib/images/lightbox-btn-prev.gif',	
	   imageBtnNext:			'/js/pavels_lib/images/lightbox-btn-next.gif',	
	   imageBtnClose:			'/js/pavels_lib/images/lightbox-btn-close.gif',
	   imageBlank:				'/js/pavels_lib/images/lightbox-blank.gif'
	});
	
	var smallWindow = document.body.clientWidth<1050;
	function adjustBar() {
		// console.log('adjust');
		if (smallWindow) {
			var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			$('#topBar-container').css('position', 'absolute').css('left', '20px').css('top', (scrollTop-8)+'px');
		} else {
			$('#topBar-container').css('position', 'fixed').css('left', '0px').css('top', '-8px');
		}
	}
	fixMode = true;
	function toggleMode(fixed) {
		if (fixMode == fixed) return;
		if (fixed) {
			$(window).unbind('scroll', adjustBar);	
		} else {
			$(window).bind('scroll', adjustBar);
		}
		adjustBar();		
	}
	if (smallWindow) {
		toggleMode(false);
	}
	$(window).resize(function() {
		smallWindow = document.body.clientWidth<1050;
		toggleMode(!smallWindow);	
	});

});

function setLocation(url) {
	window.location.href = url;
    
}

function popWin(url,win,para) {
    var win = window.open(url,win,para);
    win.focus();
}

// Stub for VarienForm class
function VarienForm() 
{
  
}

// Pavels TODO: I have no idea where this function is used, so it's not tested
function truncateOptions() 
{
	$('.truncated')
		.mouseover(function() {
			this.find('div.truncated_full_value').show('fast');
		})
		.mouseout(function() {
			this.find('div.truncated_full_value').hide('fast');
		});
}

// Pavels NOTE: This seems to add odd/even/first/last classes to a list of elements
// (Tables etc.)
// We do this using CSS. Internet Explorer will fall back to unstyled elements (no big loss)
// Stub added to stop javascript errors
function decorateGeneric(elements, decorateParams)
{

}

// Pavels NOTE: This function uses decorateGeneric to decorate a table
// Stub added to stop javascript errors
function decorateTable(table, options) 
{

}

function decorateList() 
{
  
}

