jQuery.fn.delegate = function(eventType, rules) {
  return this.bind(eventType, function(e) {
    var target = $(e.target);
    for(var selector in rules)
      if(target.is(selector)) 
        return rules[selector].apply(this, arguments);
  })
};

jQuery.fn.realOffset = function() {
	var that = this[0];
	var x = 0;
	var y = 0;
	do {
		x += that.offsetLeft;
		y += that.offsetTop;
	} while (that = that.offsetParent);
	return {left: x, top: y};
}

/* kills jumping to anchors and selecting their content */
$(function() {
	$("a[href^=#]").live('click', function(e) {
		e.preventDefault();
	}).live('selectstart', function(e) {
		e.preventDefault();
	});
});

jQuery.fn.slideOutRow = function(callback) {
	row = $(this);
	if(row.is('tr'))
	{
		$('td', row).each(function(){
			var el = $(this);
			el.css({
				verticalAlign: 'top'
			});
			var o = $('<span>').addClass('wrap').css({
				display: 'inline-block',
				verticalAlign: 'top',
				overflow: 'hidden'
			});
			var d = $('<span>').addClass('slideOut').css('overflow', 'hidden');
			d.css({
				display: 'inline-block',
				verticalAlign: 'top',
				paddingTop: el.css('paddingTop'),
				paddingBottom: el.css('paddingBottom')
			});
			el.css({
				paddingTop: 0,
				paddingBottom: 0
			});
			d.html(el.html());
			el.html('');
			d.appendTo(o);
			o.appendTo(el);
		});
		
		var t = 0;
		var c = 0;
		
		$('.slideOut', row).each(function(){
			c++;
			$(this).animate({
				marginTop: -row.height(),
				opacity: 0
			}, 300, 'swing', function(){
				row.remove();
				t++;
				if(t==c){
					if(typeof callback != 'undefined')
					{
						callback();
					}
				}
			});
		});
	}
};

function getQuerystring(key, default_)
{
	if (default_==null) default_=false;
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null)
		return default_;
	else
		return qs[1];
}

jQuery.fn.setStartValue = function(startValue) { 
	var me = $(this);
	if(me.val() == '')
	{
		me.val(startValue);
	}
    me.focus(function(){
		if(me.val() == startValue)
		{
			me.val('');
		}
	}).blur(function(){
		if(me.val() == '')
		{
			me.val(startValue);
		}
	});
	return me;
}

function formSetup(url) {
	$("#simplemodal-container form").ajaxForm({
		target: $("#simplemodal-data"),
		url: url,
		success: function() {
			formSetup(url);
		}
	});
}

function setBasketSummary(count, total){
	$('#basket-count').text(parseInt(count));
	$('#basket-total').text(parseFloat(total).toFixed(2));
}

function setShortlistSummary(count){
	$('#wishlist-count').text(parseInt(count));
}

$(document).ready(function(){
	
	$('.toggle-list').click(function(){
		$(this).next('ul').toggle();
	});
	
	$("a[href^=/send-to-friend],a[href^=/wishlist-send-to-friend],#product-rightcol a[href^=/delivery],#product-rightcol a[href^=/returns],#product-rightcol a[href^=/product-advice]").live("click", function(e) {
		var that = $(this);
		$.get(that.attr("href"), "", function(data) {
			var modal = $.modal(data,{
				onShow: function(dialog) {
					$.modal.impl.setPosition();
				},
				overlayClose: true
			});
			$(".simplemodal-close").live("click", function(){
				e.preventDefault();
				modal.close();
			});
			formSetup(that.attr("href"));
		});
		e.preventDefault();
	});
	
	$('#top-nav form').submit(function(e){
		e.preventDefault();
		var $this = $(this);
		var d = $this.formSerialize();
		$.post($this.attr('action'), d, function(data){
			data = eval(data);
			var el = $this.find('input#newsletter-signup-input');
			el.fadeTo(150, 0.1, function(){
				el.val(data.payload).fadeTo(150, 1);
				el.blur();
			}).focus(function(){
				this.select();
			});
		});
	});
	
});