var imgArray = new Array();
var trips = 0;
var sliderTimer;
var timerLength = 5000;
var slideSpeed = 800;

function isDefined(property) {
	return (typeof property != 'undefined');
}

function externalLinks() {
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0;i<anchors.length;i++) { 
		var anchor = anchors[i];
		if ((anchor.getAttribute("href")) && (anchor.getAttribute("rel") == "external")) {
			anchor.target = "_blank";
		}
	}
}

function blurLinks() {
	lnks = document.getElementsByTagName("a");
	inpts = document.getElementsByName("input");
	for(i=0;i<lnks.length;i++) {	
		if ((lnks[i].getAttribute("rel") == null) || (lnks[i].getAttribute("rel") == "")) {
			lnks[i].onfocus = new Function("this.blur()");
		}
	}
	for(i=0;i<inpts.length;i++) {
		inpts[i].onfocus= new Function("this.blur()");
	}
}

function slideSwitch(id,speed) {
	if (!speed) {
		speed = slideSpeed;
	}
	var $holder = $(id);
	var $active = $holder.find(".active");
	if ($active.length == 0) {
		$active = $holder.find("li:last");
	}
	var $next = $active.next().length ? $active.next() : $holder.find("li:first");
	$active.addClass("last-active");
	$next.css({opacity: 0.0})
	$next.addClass("active");
	$next.animate({opacity: 1.0}, speed, function() { $active.removeClass("active last-active"); $active.css({opacity: 0.0}) });
}

function stopSlider() {
	clearInterval(sliderTimer);
}

function startSlider() {
	sliderTimer = setInterval("slideSwitch('#Gallery .belt')", timerLength);
}

function accordianInit() {
	if ($(".accordion").length >= 1) {
		var accOpts = {
			collapsible: true,
			autoHeight: false,
			active: false,
			icons: { "header": "closed", "headerSelected": "opened" }
		}
		if ($(".accordianAjax").length >= 1) {
			accOpts.change = function(event, ui) {
					var clicked = $(this).find('.ui-state-active').attr('id');
					$('#'+clicked).load('/widgets/'+clicked);
			}
		}
		if ($(".accordion .accordianContent, .accordion .accordianAjax").length >= 1) {
			$(".accordion .accordianContent, .accordion .accordianAjax").show();
		}
		$(".accordion").accordion(accOpts);
	}
}

function findPopOvers(dest) {
	if (!dest) {
		dest = "";
	} else {
		dest = dest + " ";
	}
	linkSrc = dest + "a.popover";
		$(linkSrc).each(function() {
			var $popURL = $(this).attr("href");
			$(this).attr("href", "javascript:void(0)");
			$(this).click(function() {
				ajaxPopOver($popURL);
			});
		});
	
}

var directionsDisplay;
var directionsService;
var map;

function showMap(lat, lon, mapHolder) {
	if (!mapHolder) {
		mapHolder = "Map";
	}
	if ((document.getElementById(mapHolder)) && (lat != "0") && (lon != "0")) {
		var mapCentre = new google.maps.LatLng(lat, lon);
		var mapOptions = {
			zoom: 12,
			center: mapCentre,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById(mapHolder), mapOptions);
		var marker = new google.maps.Marker({
			position: mapCentre
		});
		marker.setMap(map);
	}
}

function createInfo(title,content,img,url) {
	infoWindow = "<div class=\"infoWindow\">";
	if (img) {
		infoWindow += "<p class=\"image\"><img src=\"" + img + "\" alt=\"" + title + "\" /></p>";	
	}
	infoWindow += "<h2>" + title + "</h2>";
	infoWindow += "<p>" + content + "</p>";
	if (url) {
		infoWindow += "<p class=\"links\"><a href=\"javascript:addToTrip('" + url + "')\">Add to trip planner</a></p>\n";
	}
	infoWindow += "</div>";
	return infoWindow;
}

function loadBigMap(mapID) {
	var myLatlng = new google.maps.LatLng(28.66,77.2287);
	var myOptions = {
		zoom: 3,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById(mapID), myOptions);
	var url = "/json/hotels.json";
	var image = new google.maps.MarkerImage("/images/icons/map_default.png",
		new google.maps.Size(26, 42),
		new google.maps.Point(0,0),
		new google.maps.Point(13, 42)
	);
	$.getJSON(url, {},
	function(data){
		$.each(data.markers, function(i,markers){
			var lati = data.markers[i].lati;
			var long = data.markers[i].long;
			var title = data.markers[i].title;
			var html = data.markers[i].html;
			var img = data.markers[i].img;
			var url = data.markers[i].url;
			var myLatlng = new google.maps.LatLng(parseFloat(lati),parseFloat(long));
			var infowindow = new google.maps.InfoWindow({
				content: createInfo(title, html,img,url)
			});
			var marker = new google.maps.Marker({
				position: myLatlng,
				map: map,
				icon: image
			});
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map,marker);
			});
		});
	});
}

function plotMap(mapArray) {
	directionsService = new google.maps.DirectionsService();
	directionsDisplay = new google.maps.DirectionsRenderer();
	var mapLength = (mapArray.length)-1;
	var startDest = new google.maps.LatLng(mapArray[0][0], mapArray[0][1]);
	var endDest = new google.maps.LatLng(mapArray[mapLength][0], mapArray[mapLength][1]);
	mapType = google.maps.MapTypeId.ROADMAP;
	if (viewType == "satelite") {
		mapType = google.maps.MapTypeId.TERRAIN;
	}
	var myOptions = {
		zoom: 4,
		mapTypeId: mapType,
		center: startDest
	};
	map = new google.maps.Map(document.getElementById(mapHolder), myOptions);
	directionsDisplay.setMap(map);

	var latlngbounds = new google.maps.LatLngBounds();
	var infoWindow;
	var mapCentre;
	var allPoints = new Array();
	var image;
	google.maps.event.addListener(map, 'bounds_changed', function() {
		mapCentre = map.getBounds();
	});

	/* now inside your initialise function */
	infoWindow = new google.maps.InfoWindow({
		content: "holding...",
		boxStyle: { height: "auto"}
	});

	for (var i=0; i<mapArray.length; i++) {
		lat = mapArray[i][0];
		lon = mapArray[i][1];
		var name = mapArray[i][2];
		var summary = mapArray[i][3];
		var img = mapArray[i][4];
		var url = mapArray[i][5];
		var thisPoint;
		point = new google.maps.LatLng(lat,lon);
		allPoints.push(point);
		latlngbounds.extend(point);
		pinSource = "/images/icons/map";
		var letter = String.fromCharCode(97+i);
		if (i < 11) {
			pinSource += "_" + letter;
		}
		pinSource += ".png";
		var image = new google.maps.MarkerImage(pinSource,
			new google.maps.Size(26, 42),
			new google.maps.Point(0,0),
			new google.maps.Point(13, 42)
		);
		thisPoint = new google.maps.Marker({
					position: new google.maps.LatLng(lat,lon),
					map: map,
			icon: image
		});
		thisPoint.name = name;
		thisPoint.summary = summary;
		thisPoint.img = img;
		thisPoint.url = url;
		google.maps.event.addListener(thisPoint, 'click', function () {
			infoWindow.setContent(createInfo(this.name, this.summary, this.img, this.url));
			//alert(infoWindow.height);
			infoWindow.open(map, this);
		});
	}
	map.setCenter(latlngbounds.getCenter(), mapCentre);
}

function loadImages() {
	if (document.getElementById("Gallery")) {
		$("#Gallery .belt").html("");
		for(var i = 0; i<imgArray.length; i++) {
			imgData = imgArray[i];
			imageCaption = (imgData[3]) ? imgData[3] : "";
			imgString = "<li";
			if (i == 0) {
				imgString += " class=\"active\"";
			}
			imgString += "><img src=\"/images/photos/square/" + imgData[0] + "\" width=\"" + imgData[1] + "\" height=\"" + imgData[2] + "\" alt=\"" + imageCaption + "\" title=\"" + imageCaption + "\" /></li>\n";
			$(imgString).appendTo("#Gallery .belt")
		}
		var rightNav = [];
		var leftNav = [];
		leftNav.src = "/images/buttons/left_tiny.png";
		leftNav.left = -20;
		leftNav.top = 18;
		rightNav.src = "/images/buttons/right_tiny.png";
		rightNav.left = 10;
		rightNav.top = 18;
		gallerySlider("PhotoNav",leftNav,rightNav);
		startSlider();
		$("#PhotoNav li").click(function(){
			stopSlider();
			$parent = $(this).parent();
			var $index = parseInt($("#PhotoNav li").index(this));
			var $active = $("#Gallery .belt li").find(".active");
			$next = $("#Gallery .belt li").eq($index);
			$active.addClass("last-active");
			$next.css({opacity: 0.0})
			$next.addClass("active");
			$next.animate({opacity: 1.0}, slideSpeed, function() { $active.removeClass("active last-active"); });
			startSlider();
		});
	}
}

function gallerySlider(galleryID,leftOptions,rightOptions) {
	if (document.getElementById(galleryID)) {
		if ((leftOptions) && (isDefined(leftOptions.src))) {
			leftSrc = leftOptions.src;
		} else {
			leftSrc = "/images/buttons/left_small.gif";
		}
		if ((leftOptions) && (isDefined(leftOptions.left))) {
			leftLeft = leftOptions.left;
		} else {
			leftLeft = -40;
		}
		if ((leftOptions) && (isDefined(leftOptions.top))) {
			leftTop = leftOptions.top;
		} else {
			leftTop = 18;
		}
		if ((rightOptions) && (isDefined(rightOptions.src))) {
			rightSrc = rightOptions.src;
		} else {
			rightSrc = "/images/buttons/right_small.gif";
		}
		if ((rightOptions) && (isDefined(rightOptions.left))) {
			rightLeft = rightOptions.left;
		} else {
			rightLeft = 10;
		}
		if ((rightOptions) && (isDefined(rightOptions.top))) {
			rightTop = rightOptions.top;
		} else {
			rightTop = 18;
		}
		stepcarousel.setup({
			galleryid: galleryID, //id of carousel DIV
			beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
			panelclass: 'panel', //class of panel DIVs each holding content
			autostep: {enable:false, moveby:1, pause:6000},
			panelbehavior: {speed:500, wraparound:false, wrapbehavior: 'pushpull'},
			defaultbuttons: {enable: true, moveby: 1, leftnav: [leftSrc, leftLeft, leftTop], rightnav: [rightSrc, rightLeft, rightTop]},
			contenttype: ['inline']
		});
	}
	if (document.getElementById("VideoSlider")) {
		stepcarousel.setup({
			galleryid: "VideoSlider", //id of carousel DIV
			beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
			panelclass: 'panel', //class of panel DIVs each holding content
			autostep: {enable:false, moveby:1, pause:6000},
			panelbehavior: {speed:500, wraparound:false, wrapbehavior: 'pushpull'},
			defaultbuttons: {enable: true, moveby: 1, leftnav: ['/images/buttons/left.png', 0, 130], rightnav: ['/images/buttons/right.png', -35, 130]},
			contenttype: ['inline']
		});
	}
}

function playVideo(videoURL) {
	$("#PopVideo .videoHolder").html("");
	$("#PopVideo").reveal();
	$('#PopVideo .videoHolder').youTubeEmbed({
		video: videoURL,
		width: 478,
		height: 270,
		progressBar: false
	});
}

function updateTripPlanner(numberItems) {
	if (!numberItems) {
		numberItems = "0";
	}
	tripText = "Trip";
	if (numberItems != 1) {
		tripText += "s";
	}
	$("#TripPlanner .trips strong").html(numberItems);
	$("#TripPlanner .trips span").html(tripText);
}

function showLargeMap() {
	if (document.getElementById("PopMapOver")) {
		loadBigMap("PopMap");
		$("#PopMapOver").reveal();
	} else {
		//alert("An error has occured");
	}
}

function loadFlash() {
	if (document.getElementById("India")) {
		/*$("#India").flash({
			src: "/flash/india.swf",
			width: 230,
			height: 272,
			debug: true
		});*/
		var so = new SWFObject("/flash/india.swf", "Flash Map", "230", "272", "6", "#FFFFFF");
		so.addParam("wmode", "transparent");
		so.write("India");
	}
}

$(document).ready(function() {
	$("#Header #TripPlanner ul").click(function(){
		thisClass = $(this).attr("class");
		$slideContent = $("#SlideWrapper");
		if (thisClass != "open") {
			if (trips >= 1) {
				$(this).attr("class","open");
				$slideContent.slideDown("fast");
			} else {
				$(this).attr("class","open");
				$slideContent.slideDown("fast");
			}
		} else {
			$slideContent.slideUp("fast");
			$(this).removeClass("open");
		}
	});
	if (document.getElementById("SlideHolder")) {
		$("#SlideHolder #SlideWrapper #TripHolder p .close").click(function(){
			$slideContent = $("#SlideWrapper");
			$slideContent.slideUp("fast");
			$("#Header #TripPlanner ul").removeClass("open");
		});	
	}
	if ($(".addToTrip a").length >= 1) {
		$(".addToTrip a").each(function() {
			$addURL = $(this).attr("href");
			$(this).attr("href", "javascript:void(0)");
			$(this).click(function() {
				addToTrip($addURL);
			});
		});
	}
	if ($(".largerMap").length >= 1) {
		$(".largerMap").click(function() {
			showLargeMap("PopMap");
		});
	}
	accordianInit();
	if (document.getElementById("TabContent")) {
		tabPlace = "#TabContent .tabs";
		runBefore = "#TabContent .tabs li .runBefore";
		var tabs = $("#TabContent").tabs();
		if ($(runBefore).length >= 1) {
			tabs.bind("tabsselect", function(event, ui) {
				thisLi = $(".tabs li").get(ui.index);
				thisLink = $(thisLi).find("a");
				if ((thisLink.attr("rel")) && (thisLink.attr("class") == "runBefore")) {
					eval(thisLink.attr("rel") + "()");
				}
			});
		}
		if (document.getElementById("SubTabContent")) {
			var tabs = $("#SubTabContent").tabs();
			if ($("#SubTabContent li a").length > 2) {
				$("#SubTabContent").tabs({ selected: 1 });
			}
		}
	}
	$(".durationChanger").change(function(){
		var $this = $(this);
		var plannerID = $this.attr("id").replace("Duration_", "");
		var plannerDuration = $this.attr("value");
		requestURL = "/ajax/updateDuration.php";
		requestVar = "plannerID=" + plannerID;
		requestVar += "&plannerDuration=" + plannerDuration;
		myRequest = new ajaxObject(requestURL);
		myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
		myRequest.update(requestVar,"POST");
	});
	if ($(".left .jumper li").length > 1) {
			$(".left .jumper li a").click(function() {
			$dest = $("#Content .right .rightText");
			$(".left .jumper li").removeClass("active");
			thisParent = $(this).parent();
			thisURL = $(this).attr("href");
			srcText = $(thisURL).html();
			$dest.html(srcText);
			thisParent.addClass("active")
		});
		$(".left .jumper li:first a").trigger('click');
	}
	findPopOvers();
	externalLinks();
	blurLinks();
	formControls();
	loadFlash();
	if (document.getElementById("FrontMap")) {
		loadBigMap("FrontMap");
	}
	if (document.getElementById("TestimonialGallery")) {
		gallerySlider("TestimonialGallery");
	}
	if (eval(window.adminLoadFunctions) != undefined) {
		adminLoadFunctions();
	}
	if (imgArray.length >= 1) {
		loadImages();
	}
	});


