$(function(){
/*redirect
    if (navigator.appName == 'Netscape')
    {
        var language = navigator.language;
    }
    else
    {
        var language = navigator.browserLanguage;
    }
    var code = language.substring(0,2);
    var availableSiteAccess = [ 'es' ];
    if( window.location.href == 'http://www.elcidvacationsclub.com/' )
    {
        if( $.inArray( code, availableSiteAccess ) != -1 )
        {
                window.location = 'http://www.elcidvacationsclub.com.mx/';
        }
    }
*/
// Cufon

	Cufon.replace('h1, h2, h3');
	Cufon.replace('.replaced');
	Cufon.replace('.sml');
	Cufon.replace('.big');
	Cufon.replace('.tagline');
	Cufon.replace('.video_caption, .add_header, .email_sign_up')

// main menu
	$('#nav li:hover ul').css({display: 'none'});
	$("#nav ul").css("opacity", "0.9");
	$('#nav li')
		.mouseenter(function(){
				$(this).children('ul').css({display: 'block'});
		})
		.mouseleave(function(){
			$(this).children('ul').css({display: 'none'});
		})
		$('#nav ul').mouseenter(function(){
				$(this).parents('li').addClass('on');
		$('#nav ul').mouseleave(function(){
				$(this).parents('li').removeClass('on');
		})
})
//active page
		var active_page = $('#nav li a.active');
		$('#nav li a').hover(function(){
				if ($(this).hasClass('active')){
					active_page = $(this);
					active_page.removeClass('active');
				}
		},
		function(){
			active_page.addClass('active');
		});

if ($(".right_gallery").length > 0) {

//right header gallery
		$(".right_gallery").css("opacity", "0.9");
// thumb opacity
	$(".right_gallery li a img").css("opacity", "0.85");
	$(".right_gallery li a").hover(
		function(){
			$(this).children("img").css("opacity", "0.95");
		},
		function(){
			$(this).children("img").css("opacity", "0.85");
		}
	);

////gallery - timer----------------------------------
	var thumb_clicked = false;
	var thumb_elements = $(".right_gallery li").length;
	var thumb_to_click = 0;
	
	$.timer(5000, function(timer){
		if (thumb_clicked == true) {
			timer.reset(5000);		
			thumb_clicked = false;
		} else {
			thumb_to_click++;
			if (thumb_to_click >= thumb_elements) thumb_to_click = 0;
			$(".right_gallery li:eq("+thumb_to_click+") a").click();
		}
	});


//display image from gallery on click
	$(".right_gallery li a").click(function () {
		var img_class = $(this).children("img").get(0).className;
		
		$(".viewport img."+img_class+"").css("z-index", "2").fadeIn("slow");
		$(".viewport img").not("."+img_class).css("z-index", "1").fadeOut("slow");		
		
		$('.right_gallery li a').parent('li').removeClass();
		$(this).parent('li').addClass('on');
		thumb_to_click = $(".right_gallery li").index($(this).parent("li"));		
		return false;
	});
	$(".right_gallery li a").mouseup(function(){
		thumb_clicked = true;
	});

}

if ($(".scrollable").length > 0) {

	$('.items').children()[0].style.display='block';
	
	////promo baner - timer------------------------------------
	var item_clicked = false;
	var item_elements = $(".navi a").length;
	var item_to_click = 0;
	
	$.timer(4000, function(timer){
		if (item_clicked == true) {
			timer.reset(4000);		
			item_clicked = false;
		} else {
			item_to_click++;
			if (item_to_click >= item_elements) item_to_click = 0;
			$(".navi a:eq("+item_to_click+")").click();
		}
	});
	
	//promo baner - nav click
		$(".navi a").click(function () {
			var item_class = $(this).children("span").get(0).className;
			
			$(".items > div."+item_class+"").css("z-index", "10").fadeIn("slow");
			$(".items > div").not("."+item_class).css("z-index", "5").fadeOut("slow");		
			
			$('.navi a').removeClass();
			$(this).addClass('active');
			item_to_click = $(".navi a").index($(this));
			return false;
		});
		/*
		$(".right_gallery li a").mouseup(function(){
			thumb_clicked = true;
		});
		*/

}
	
//datepicker
if ($(".datepicker").length > 0) {
	$(function() {
		$(".datepicker").datepicker({showOn: 'button', buttonImage: '/extension/cid/design/cid/images/calendar.gif', buttonImageOnly: true});
	});
};

// thumb opacity
	$(".thumbs li a img").css("opacity", "0.85");
	$(".thumbs li a").hover(
		function(){
			$(this).children("img").css("opacity", "0.95");
		},
		function(){
			$(this).children("img").css("opacity", "0.85");
		}	
	);

//fancybox
//mizan: removed this (.locales ul li a, .offer ul li a)
/*$("ul.thumbs a").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayColor'  :  '#000',
		'titlePosition' : 'over',
		'overlayOpacity': 0.65
	});*/

$(".fancyBox").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'overlayColor'  :  '#000',
		'titlePosition' : 'over',
		'overlayOpacity': 0.65
	});

$(".fancyBoxVideo").fancybox({
		'width'				: 492,
		'height'			: 270,
		'autoScale'     	: true,
		'scrolling'			: 'no',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
		
	});
	

// select dropdown show hide
	$('.select').toggle(
		function(){
			$('.select_drop').fadeIn('fast');
			$(this).addClass('select_up');
		},
		function(){
			$('.select_drop').fadeOut('fast');
			$(this).removeClass('select_up');
		}
	);

// select gallery dropdown show hide
	$('.select_gallery').toggle(
		function(){
			$('.select_gallery_drop').fadeIn('fast');
			$(this).addClass('select_gallery_up');
		},
		function(){
			$('.select_gallery_drop').fadeOut('fast');
			$(this).removeClass('select_gallery_up');
		}
	);

// the stand-alone gallery
	if($('#gallery').length>0) {
		delay = 5000;
		numImages = images.length;
		currentImg = 0;
		nextImg = 1;
		animating = true;
		$('.gallery_total').html(numImages);
		$('.gallery_description').html(captions[0]);
		$('.ctr_left').click(function() {
			if(!animating) {
				nextImg = currentImg - 1;
				showImage(nextImg)
			}
		})
		$('.ctr_right').click(function() {
			if(!animating) {
				nextImg = currentImg + 1;
				showImage(nextImg)
			}
		})
		$('.ctr_play').click(function(){
			if($(this).hasClass('ctr_pause')) {
				$(this).removeClass('ctr_pause');
				rotation = setTimeout(showImage, delay);
			} else {
				$(this).addClass('ctr_pause');
				clearTimeout(rotation);
			}
		})
	
		loadImage(0);
	
	}
});

function loadImage(i) {
	if(i < numImages) {
		$('.gallery_items').append('<img class="image'+i+'"  title="'+alternative_text[i]+'" alt="'+alternative_text[i]+'" src="'+images[i]+'">');
		$('.image'+i)
			.load(function(){
				if(i == currentImg) {
					$(this).fadeIn('slow');
				}
				if(i == numImages-1) {
					animating = false;
					rotation = setTimeout(showImage, delay);
				} else {
					i++;
					loadImage(i);
				}
			});
	}
}

function showImage() {
	animating = true;
	clearTimeout(rotation);

	nextImg %= numImages;
	if(nextImg < 0) {
		nextImg = numImages - 1;
	}

	$('.gallery_description').html(captions[nextImg]);
	$('.gallery_current').html(nextImg+1);
	$('.gallery_items img:eq('+currentImg+')').stop(true,true).css({zIndex: '2'});
	$('.gallery_items img:eq('+nextImg+')').stop(true,true).css({display: 'none', zIndex: '3'}).fadeIn('slow', function(){
		$('.gallery_items img:eq('+currentImg+')').css({display: 'none'})
		currentImg = nextImg;
		nextImg++;
		animating = false;
		rotation = setTimeout(showImage, delay);
	});
	
}


