var is_opera =  navigator.userAgent.indexOf('Opera') > -1;
var is_safari = navigator.userAgent.indexOf('AppleWebKit/') > -1;
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_mac = navigator.platform.toLowerCase().indexOf('mac') > -1;
var is_linux = navigator.platform.toLowerCase().indexOf('linux') > -1;
var ie6 = false;
var ie7 = false;
var ie8 = false;


$(document).ready(function() {

	if(typeof slideshow_handler == 'object') {
		slideshow_handler.setup();
	}

	if(typeof sidebar_slideshow == 'object') {
		 sidebar_slideshow.setup();
	}   	

	$('a').map(function(i, el){
		if($(el).attr('rel') == 'external') {
			$(el).attr('target', '_blank');
		}
	});
	
	if(typeof field_defaults == 'object') {
		field_defaults.setup();
	}		
	
	$("#gallery-items a, #booking-gallery-container a, #events-entries .thumb a, #sidebar #featured-event .thumb a, #home-event-container .thumb a").fancybox(
			{
				//'width'			: 460,																									
				'href'			: this.href,
				'overlayOpacity' : 0,
				'overlayColor': '#000',
				'padding': 5,
				'titlePosition': 'over',
				'centerOnScroll': true,
				'onClosed': function() {
				},
				'onCleanup': function() {
				}					
			}		
	);

	$(".packages-list a").fancybox(
			{
				//'width'			: 300,
				'href'			: this.href,
				'overlayOpacity' : 0,
				//'type': 'iframe',
				'overlayColor': '#000',
				'padding': 20,
				'titlePosition': 'over',
				'centerOnScroll': false,
				'onClosed': function() {
				},
				'onCleanup': function() {
				},
				'onComplete': function() {
					document.getElementById('package-content').scrollTop = 0;
				}
			}		
	);	
	
	$('#menu-header li').bind('mouseover', function(event) {
		$(event.currentTarget).addClass('over');
	});	
	$('#menu-header li').bind('mouseout', function(event) {
		$(event.currentTarget).removeClass('over');
	});	

	if(is_safari) {
		$('body').addClass('browser-safari');
	}

	if(is_opera) {
		$('body').addClass('browser-opera');
	}

	setTimeout(function(){
		var max_height = 0;

		var all_events = $('#events-entries .event-entry');
		
		all_events.map(function(i, el){
			if($(el).height() > max_height) {
				max_height = $(el).height();
			}
		});

		$('#events-entries .inner').css({'height':max_height});

	}, 100);
		
});


    
var slideshow_handler = {
	cat_items: [],
    container: null,
	items: [],
    total_items: null,
    last_item: -1,
    delay: 4000,
	toggle_odd_even: 0,
	que: [[1,7], [4,6], [2,8], [3,5]],
    
    setup: function()  {
		
		this.container = $('#header-photos');
		if(this.container.length == 0) return;
		
		this.cat_items = $('#header-photos .photos-cat');
		
		setTimeout(function(){ slideshow_handler.changePic(); }, slideshow_handler.delay);
    },
    
    changePic: function() {
		var next_index = this.last_item + 1;

		if(next_index == this.que.length) {
			next_index = 0;
		}

		var next_cat_1 = this.cat_items[this.que[next_index][0]-1];
		var next_cat_2 = this.cat_items[this.que[next_index][1]-1];
		
		var cat_1_last_active = $('.active', next_cat_1);
		var cat_2_last_active = $('.active', next_cat_2);
		
		var cat_1_not_active = $(next_cat_1).find('.thumb[class!="active"]');
		var cat_2_not_active = $(next_cat_2).find('.thumb[class!="active"]');

		$(cat_1_last_active).animate({
			opacity: 0
		  }, 10, function() {
			$(cat_1_last_active).removeClass('active')
		});

		$(cat_2_last_active).animate({
			opacity: 0
		  }, 10, function() {
			$(cat_2_last_active).removeClass('active')
		});
	
		$(cat_1_not_active).css({'opacity':'0.25'});
		$(cat_1_not_active).addClass('active');
		$(cat_1_not_active).animate({
			opacity: 1
		  }, 1000, function() {
			// Animation complete.
		});
		
		$(cat_2_not_active).css({'opacity':'0.25'});
		$(cat_2_not_active).addClass('active');
		$(cat_2_not_active).animate({
			opacity: 1
		  }, 1000, function() {
			// Animation complete.
		});		
		
		this.last_item = next_index;
		setTimeout(function(){ slideshow_handler.changePic(); }, slideshow_handler.delay + 1000);	
		
    }
}




var sidebar_slideshow = {
    container: null,
    total_items: 4,
    last_item: null,
    delay: 6500,
    
    setup: function()  {
		setTimeout(function(){ sidebar_slideshow.changePic(); }, sidebar_slideshow.delay);
		last_item = 0;
    },
    
    changePic: function() {
	var last_pic = $('#sidebar-gallery .active');
	var next_index = this.last_item + 1;
	if(next_index == this.total_items) {
	    next_index = 0;
	}
	
	var last_pic = $('#sidebar-gallery .active');
	$(last_pic).animate({
	    opacity: 0
	  }, 1000, function() {
	    // Animation complete.
	});
	setTimeout(function(){$(last_pic).removeClass('active')}, sidebar_slideshow.delay);

	var next_pic = $('#sidebar-gallery .photo')[next_index];
	$(next_pic).css({'opacity':'0'});
	$(next_pic).addClass('active');
	$(next_pic).animate({
	    opacity: 1
	  }, 1000, function() {
	    // Animation complete.
	});
	
	this.last_item = next_index;
	setTimeout(function(){ sidebar_slideshow.changePic(); }, sidebar_slideshow.delay);
    }
}

var field_defaults = {
	items: ['#email-list-form input[type=text]'],
	setup: function() {

		$(this.items).map(function(i, el){
			
			$(el).attr('rel', $(el).val());

			$(el).bind('focus', function(event) {
				if($(event.currentTarget).val() == $(event.currentTarget).attr('rel') ) {
					$(event.currentTarget).val('');
				}
			});

			$(el).bind('blur', function(event) {
				if($(event.currentTarget).val() == '') {
					$(event.currentTarget).val($(event.currentTarget).attr('rel'));
				}
			});

		})        
	}
}
