jQuery.noConflict();

function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

	date1 = Date.fromString(date1);	
	date2 = Date.fromString(date2);

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 }

//get the relevant query string
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}



jQuery(function(){
	jQuery('#BB_BuyButtonForm').submit();
	
		
	jQuery('#nav_sub').accordion();
	
	jQuery('#gallery').gallerify();
	
	if(typeof(jQuery.fn.colorbox)=='function') {
		jQuery('.item_image>a,.room_size').colorbox({
			opacity: 0.8
		});
	}


	
	jQuery('.action_prices').html("<img src='/images/action_prices.png' />");
	jQuery('.action_discounts').html("<img src='/images/action_discounts.png' />");
	jQuery('.action_removals').html("<img src='/images/action_removals.png' />");
	jQuery('.action_van_hire').html("<img src='/images/action_van_hire.png' />");
	
	
});


jQuery.fn.accordion = function(){
	return this.each(function(){
		
		var sublists = jQuery('ul ul',this).hide();
		var headers = jQuery('h3>a',this);
		
		headers.click(function(){
			var currentlyOpen = jQuery(this).parent().next().is(':visible');
			headers.removeClass('open');
			sublists.filter(':visible').slideUp();
			if (!currentlyOpen) {
				jQuery(this).addClass('open').parent().next().slideDown();
			}
			return false;
		});
		
		jQuery('.on',this).parent().show().prev().children('a').addClass('open');
		
	});
}

jQuery.fn.gallerify = function(){
	return this.each(function(){
		
		var timer;
		var container = jQuery('>div',this).length ? jQuery('>div',this) : jQuery(this);
		var images = jQuery('>ul>li',container);
		var nav = jQuery('<ul id="gallery_nav"></ul>').appendTo(container);
		
		images.eq(0).parent().height(images.eq(0).children('img').height());
		
		images.each(function(){
			nav.append('<li class="pos"><a href="#"><span>'+images.index(this)+'</span></a></li>').children(':first').addClass('on');
		}).not(':first').hide();
		nav.prepend('<li class="prev"><a href="#"><span>Previous</span></a></li>').append('<li class="next"><a href="#"><span>Next</span></a></li>');

		var showImage = function() {
			var clickedIndex = parseInt(jQuery(this).text(),10);
			images.filter(':visible').fadeOut(600);
			images.eq(clickedIndex).fadeIn(1000);
			nav.children().removeClass('on');
			jQuery(this).parent().addClass('on');
		}

		jQuery('.prev>a',nav).click(function(){
			window.clearInterval(timer);
			var current = jQuery('.on',nav);
			var prev = current.prev('.pos').length ? current.prev('.pos').children('a') : jQuery('.pos:last',nav).children('a');
			showImage.call(prev);
			return false;
		});
		
		jQuery('.pos>a',nav).click(function(){
			window.clearInterval(timer);
			showImage.call(this)
			return false;
		});
		
		jQuery('.next>a',nav).click(function(){
			window.clearInterval(timer);
			var current = jQuery('.on',nav);
			var next = current.next('.pos').length ? current.next('.pos').children('a') : jQuery('.pos:first',nav).children('a');
			showImage.call(next);
			return false;
		});
		
		timer = window.setInterval(function(){
			var next = jQuery('.on',nav).next('.pos');
			next.length ? showImage.call(next.children('a')) : showImage.call(jQuery('.pos:first>a',nav));
		},3000);
		
	});
}