var carouselTransition;
var activeTab=0;

$(document).ready(

	function() {
		/*$("#arriving").datepicker({
			showOn: 'both',
			buttonImage: 'images/calendar_button.gif',
			buttonText: 'Select a date',
			buttonImageOnly: true,
			changeMonth: true,
			changeYear: true,
			yearRange: '<?PHP echo ($i = date("Y", earliest_booking_start())) . ":" . (++$i); ?>',
			defaultDate: new Date(<?php echo date("Y, m - 1, d", $from); ?>),
			minDate: new Date(<?php echo date("Y, m - 1, d", earliest_booking_start()); ?>),
			dateFormat: 'dd M yy',
			duration : 'fast',
			constrainInput: true
		});*/
		
        $(".accordion").accordion({
			header : "h3",
			fillSpace : true
		});
        
		$(".box").mouseenter(function() {
			$(this).addClass('over'); 
			$(this).find("h3").animate({"left" : 0}, 425);
		});
		$(".box").mouseleave(function() {
			$(this).removeClass('over');  
			$(this).find("h3").animate({"left" : 165}, 425);
		});
	
	   $("form").bind("submit", function(event){
          event.stopPropagation();
        });
    
        resetCarouselTransition();
		
    	//Attach events to next / previous
    	$('#carousel_next').bind('click', function(evt) {            
            //Make sure previous button is visible
            resetCarouselTransition();
            carousel_slide('next');
            
        });
    	
        $('#carousel_prev').bind('click', function(evt) {
            resetCarouselTransition();
            carousel_slide('prev');
        });
        
        setupHomePageTabs();
     attachObservers();
    }
);



function setupHomePageTabs() {
    //Attach homepage content tab functionality
    $('#homePageContentTabs').children().each(function(index) {
        $(this).click(function(evt) {
            
            activeTab=index; //Doing it this way because of ie6 hover problem and constraints places on us by the design
                                    
            //Hide all tabbed content
            $('#homePageTabContent').children().hide();
            
            //Remove the active style from the tabs
            $('#homePageContentTabs').children().removeClass('active');
            
            //Show the one we want
            $($("#homePageTabContent").children().get(index)).show();
            
            //Add the active style to the now active tab
            $($("#homePageContentTabs").children().get(index)).addClass('active');
                            
            evt.preventDefault();
            
        });
        $(this).mouseover(function() {
            $(this).addClass('active');
        });
        $(this).mouseout(function() {
            if(index != activeTab) {
                $(this).removeClass('active');
            }
        });
    });
}

function resetCarouselTransition() {

    clearInterval(carouselTransition); 

    //Create transition
    carouselTransition = setInterval(function() {
        carousel_slide('next');  
    }, 10000);
    
}

// trundles our carousel about...
function carousel_slide(direction) {
	
	// get an array of what our slide offsets will be based on the frame widths.
	if(!direction) direction = 'next';
	
	var offsets = new Array();
	var n = 0;

    $("#carousel_items > .carousel_item").each(function() {
		offsets.push(n * -1);
		n += $(this).width();
	});
  
	// get our current offset.
	current = $("#carousel_items").position();
	current = current.left;
   
	// see where that offset sits on our array of offsets.
	for(i=0;i<offsets.length;i++) {
		
        //if(offsets[i] == current) { **PATCH - IE A PIXEL OUT IN POSITION SO GIVING LEWAY - FIX WHEN TIME**
        if((offsets[i] - current) < 10) {
			break;
		}
	}

	// look at the direction they're going and adjust our index accordingly.
	i = (direction == "next") ? (i + 1) : (i - 1);
	i = (i < 0) ? offsets.length - 1 : i;
	i = (i == offsets.length) ? 0 : i;
  
	//Check for the existance of the image we want to look at and add if missing
    /*REINTRODUCE WHEN TIME. ISSUES WITH DYNAMICALLY ADDING CAROUSEL IMAGES
    imgNum=i+1;
	if ($("#carousel_img_"+imgNum).length == 0) {    
        $("<img src=\"images/carousel/"+imgNum+".jpg\" class=\"carousel_img\" id=\"carousel_img_"+imgNum+"\" height=\"260\" width=\"640\" alt=\"\">").appendTo("#carousel_item_"+i);
    }*/

	$('#carousel_prev').show();
    $('#carousel_next').show();

	//Hide next/prev if required
	if(i==0) {
        $('#carousel_prev').hide();
    }
    else if(i==offsets.length-1) {
        $('#carousel_next').hide();
    }
   
	//Perform the slide
	$("#carousel_items").animate({"left" : offsets[i]}, 500);

}

// handles clearing the example text in the search box when they select it.
function clearexample(f) {
	f.select();
}

function attachObservers() {
    
    //Attach jquery modal functionality    
  $('#popupbox').jqm();
    $('#searchButton').click(function() {
	
	
	//if(document.quicksearch.q.value == "" || document.quicksearch.q.value == "London" || document.quicksearch.q.value == "District, Postcode, Attraction"){
		//$('#popupbox').jqmShow(); 
		
	//}else{
		validate_quicksearch();
	//}
	
		
		
		
		
    });
    
	}