﻿/* ===========================================================
	 Initialisation code
   ========================================================== */

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Page-load event =-=-=
$j(document).ready(function() {	
	setBackground();
	
	var filterToggler = $j('#filterHolidays .filterToggle');
	
	if(filterToggler != null) {	
		filterToggler.click(function () {
			var controls = $j('#filterHolidays .controls');
			
			if(controls.hasClass('hidden')) {
				controls.show(350);
				controls.removeClass('hidden');
				replaceClass(filterToggler, 'more', 'less');
			}
			else {
				controls.hide(350);
				controls.addClass('hidden');
				replaceClass(filterToggler, 'less', 'more');		
			}
		});
	}
	
	//$j('#thumbnails li:odd').addClass('alt');
	$j('#thumbnails li img').hover(thumbnail_over, thumbnail_out);
	$j('.gallery img').hover(gen_thumbnail_over, gen_thumbnail_out);
	
	bindHolidayClick();
	bindActivityClick();
	bindDestinationClick();
});

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Site functions =-=-=

function setBackground() {
	var catId = $j.query.get('categoryId');

	//  || catId == null || catId == ''
	if (catId == 5 || catId == 7 || catId == 9) {
		$j('html').addClass('winter');
	}
	else
	{
		$j('html').addClass('summer');
	}
}

function bindHolidayClick() {
	var holidays = $j('.holiday');
	
	if(holidays != null) {
		holidays.click(function() {
			
			var ids = $j(this).attr('id'); // .split('_');

			location.href = appRoot + ids;
			
//			switch (ids.length) {
//			    case 4:
//			        location.href = appRoot + ids;
//			        break;
//			    case 5:
//			        location.href = appRoot + "Booking/ViewEvent.aspx?parentId=" + ids[0] + "&categoryId=" + ids[1] + "&eventId=" + ids[2] + "&ref=1";
//			        break;
//			    default:
//			        break;
//			}
////			switch(ids.length)
//			{
//				case 3:
//					location.href = appRoot + "Booking/ViewEvent.aspx?parentId=" + ids[0] + "&categoryId=" + ids[1] + "&eventId=" + ids[2];
//					break;
//				case 4:
//					location.href = appRoot + "Booking/ViewEvent.aspx?parentId=" + ids[0] + "&categoryId=" + ids[1] + "&eventId=" + ids[2] + "&ref=1";
//					break;
//				default:
//					break;
//            }	
//			
//			if(ids.length == 3) {
//				location.href = appRoot + "Booking/ViewEvent.aspx?parentId=" + ids[0] + "&categoryId=" + ids[1] + "&eventId=" + ids[2];
//			}
		});
	}
}

function bindActivityClick() {
	var destinations = $j(".activity");
	
	if(destinations != null) {
		destinations.click(function() {
			var id = $j(this).attr('id');
					
			location.href = appRoot + "Booking/ViewActivities.aspx?activityId=" + id;			
		});
	}
}

function bindDestinationClick() {
	var destinations = $j(".destination");
	
	if(destinations != null) {
		destinations.click(function() {
			var id = $j(this).attr('id');
					
			location.href = appRoot + "Booking/ViewDestinations.aspx?destinationId=" + id;			
		});
	}
}

// preload the images for slower connections
// requires: jquery.preload-1.0.7-min.js
jQuery(function( $ ){	
    $.preload( '#thumbnails img', {
	        find:'thumb/',
	        replace:''
	});
});

jQuery(function( $ ){
    $.localScroll({
       offset: -5
    });
});

// tour gallery image zoom
var bling = false;

function thumbnail_over() {
	bling = true;
	
	if($j("#zoomed img").is(":animated") == false) {
		$j("#zoomed").css("display","block");
	}
	
	$j("#zoomed img").remove();

	$j("#zoomed").append('<img src="' + $j(this).attr("src").replace(/thumb\//i, "") 
		+ '" />');
		
	$j("#zoomed img").css("opacity", "0.0");
	
	$j("#zoomed img").animate({		
		left: "0px",
		right: "-560px",
		opacity: 1.0,
		width: "540px",
		height: "327px"		
	}, 350, function () {
		bling = false;	
	});
}

function thumbnail_out() {
	$j("#zoomed img").animate({		
		left: "560px",
		opacity: 0.0,
		width: "5px",
		height: "3px"
	}, 350, function() {		
		if(!bling) {
			$j("#zoomed").css("display","none");
		}
	});
				
}

var gen_revealed = false;

function gen_thumbnail_over() {
	gen_revealed = true;
    var gallery = $j(this).parent().parent().parent();
    var target = $j(gallery).find('.target');
    var target_img = $j(target).find('img');

    $j(target_img).remove();

    $j(target).css({ background: "#ffffff", borderWidth: "1px" }).append('<img src="' + $j(this).attr("src").replace(/thumb\//i, "")
		+ '" />');

    if ($j(gallery).hasClass("left")) {
        // left-hand animate settings
        $j(target).fadeIn(500);
    }
    // TODO: Generic code for right-hand side
}

function gen_thumbnail_out() {
    var gallery = $j(this).parent().parent().parent();
    var target = $j(gallery).find('.target');
    var target_img = $j(target).find('img');

    if ($j(gallery).hasClass("left")) {
        // left-hand animate settings
        $j(target).hide();        
    }
    // TODO: Generic code for right-hand side
    gen_revealed = false;
}

$j(document).mousemove(function() {
	//console.log("Document mousemove");
	if(gen_revealed == false) {
		$j(".gallery .target img").hide();
		$j(".gallery .target").css({ display: "none", borderWidth: "0px", background: "none" });
	}
});

// MOVED TO FmaLibrary.js =>
//		toggle
//		isFormChanged();
//		isElementChanged();
//		confirmExit();
//		getQuerystringParams();
//		addLoadEvent(func);
//		replaceClass(elm, oldClass, newClass);
//		hideElement(element);


