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];
}


function init_questions(extra)
{
	
	jQuery(extra + ".question").mouseover(function() {
		jQuery('.info_popup').html(jQuery(this).attr('attr-info'));
		
	     // if (jQuery(this).hasClass('large'))
			jQuery('.info_popup').addClass('large')
		 // else
		//	jQuery('.info_popup').removeClass('large')
		
		 if (jQuery(this).hasClass('large_high'))
			jQuery('.info_popup').addClass('large_high')
		  else
			jQuery('.info_popup').removeClass('large_high')
		
			 if (jQuery(this).hasClass('padding'))
				jQuery('.info_popup').addClass('padding')
			  else
				jQuery('.info_popup').removeClass('padding')	
		
	    jQuery('.info_popup').show();
	  }).mouseout(function(){
	    jQuery('.info_popup').hide();
	  });

		jQuery(extra + '.question').mousemove(function(e){
		        var x = e.pageX - 27;
		        var y = e.pageY - 82;
		jQuery('.info_popup').css('left',x);
		jQuery('.info_popup').css('top',y);
		    });
}




jQuery(function(){
	
	
	
	
	jQuery("#comp_box .text_field").click(function(){
		if (jQuery(this).val() == "Enter e-mail to join in")
			jQuery(this).val("");
	})
	//jQuery('#testimonial_banner p.testimonial_entry').hide();
	//jQuery('#testimonial_banner p.testimonial_entry:nth-child(1)').show();
	jQuery('#BB_BuyButtonForm').submit();
	
	
	jQuery('.testimonial_nav').click(function(){
		if (jQuery(this).hasClass('next'))
		{
			if (jQuery('#testimonial_banner p.testimonial_entry:visible').next('p.testimonial_entry').length > 0)
				jQuery('#testimonial_banner p.testimonial_entry:visible').hide().next('p.testimonial_entry').show();
			else
			{
				jQuery('#testimonial_banner p.testimonial_entry').hide();
				jQuery('#testimonial_banner p.testimonial_entry:nth-child(3)').show();
			}

		}
		else
		{
			if (jQuery('#testimonial_banner p.testimonial_entry:visible').prev('p.testimonial_entry').length > 0)
				jQuery('#testimonial_banner p.testimonial_entry:visible').hide().prev('p.testimonial_entry').show();
			else
			{
				jQuery('#testimonial_banner p.testimonial_entry').hide();
				jQuery('#testimonial_banner p.testimonial_entry:last').show();
			}
		}
		return false;
	});
		
	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);
		
	});
}
