/*---------------------------------------------------------------------

Template Name: 	Online Booking
Version:	 	1.0
Release Date: 	August 08, 2010

File:       	custom.js
Updated:    	2010-08-08

Copyright (c) 2010 Zoran Juric - http://themeforest.net/user/zoranjuric

-----------------------------------------------------------------------

WARNING! DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!

---------------------------------------------------------------------*/

// Preload images
imageObj = new Image();
imgs = ["img/loader.gif", "img/loading.gif"];
for (i = 0; i < imgs.length; i++) imageObj.src = imgs[i];

var infowindow;
var map;
var mapDrawn = false;

var hotels = [];
var markers = [];

// Site object setup
var Site = {};

// scrollToTop() - scroll window to the top
Site.scrollToTop = function (e) {
    $(e).hide().removeAttr("href");
    if ($(window).scrollTop() != "0") {
        $(e).fadeIn("slow")
    }
    var scrollDiv = $(e);
    $(window).scroll(function () {
        if ($(window).scrollTop() == "0") {
            $(scrollDiv).fadeOut("slow")
        } else {
            $(scrollDiv).fadeIn("slow")
        }
    });
    $(e).click(function () {
        $("html, body").animate({
            scrollTop: 0
        }, "slow")
    })
}

// scroll_to_hash() - scroll window to the tag with #hash id 
Site.scroll_to_hash = function(hash) {
	var ot = $(hash).offset();
	jQuery("html, body").animate({ 
		scrollTop: ot.top 
	}, "slow");
}

// setup() - Site init and setup
Site.setup = function () {
    // Open an external link in a new window
    $('a[href^="http://"]').filter(function () {
        return this.hostname && this.hostname !== location.hostname;
    }).attr('target', '_blank');

    // scroll to top on request
    if ($("a#totop").length) Site.scrollToTop("a#totop");
	
	// setup content boxes (Search Filters)
	if ($(".content-box").length) {
		$(".content-box .header").css({
			"cursor": "s-resize"
		});
		// Give the header in content-box a different cursor	
		$(".content-box .header").click(
		function () {
			$(this).parent().find('.section').toggle(); // Toggle the content
			$(this).parent().toggleClass("content-box-closed"); // Toggle the class "content-box-closed" on the content
		});
	}
	

	
	// dropdown menu
	var $menu = $('#nav');		
	$menu.children('li').each(function(){
		var $this = $(this);

		var submenu = $this.find('.dropdown_submenu');
		if (submenu.length) {
			submenu.parents('ul, div').css('z-index', 2000); // z-index hack for IE7
			$this.bind('mouseenter',function(){
				submenu.slideDown().css('z-index', 1000); // z-index hack for IE7
			}).bind('mouseleave',function(){
				submenu.fadeOut('fast');
			});
		}		
	});
	
	// find closeable boxes and add a "close" action
	$('.closeable').each(function(index){
		$(this).prepend( 
			$('<a></a>')
				.attr({href: '#', title: 'Close'})
				.addClass('close')
				.text('x')
				.click(function() {
					$(this).parent().fadeOut();
					return false;
				})
		);
	});

}


// gmapInit() - Init and draw a Google Map
// centerX		: Map center X
// centerY		: Map center Y
// zoom			: Zoom level
// clustering	: Clustering Enable/Disable
// panoramaID	: ID of the Street View panorama holder
Site.gmapInit = function(centerX, centerY, zoom, clustering, panoramaID) {
	// Already initialized?
	if (mapDrawn) return false;
	
	// Default values

	var centerX 	= (centerX == null) ? 27.9944 : centerX;
	var centerY 	= (centerY == null) ? -9.8437 : centerY;
	var zoom 		= (zoom == null) ? 2 : zoom;
	var clustering 	= (clustering == null) ? false : clustering;
	var panoramaID 	= (panoramaID == null) ? "" : panoramaID;
	
	var streetView	= (panoramaID != "");

	(function () {

		google.maps.Map.prototype.markers = new Array();

		google.maps.Map.prototype.addMarker = function(marker) {
			this.markers[this.markers.length] = marker;
		};
		
	})();
	
	var hotelIcon = new google.maps.MarkerImage(
		'img/markers/orange.png', 
		new google.maps.Size(21, 35), 
		new google.maps.Point(0, 0), 
		new google.maps.Point(11, 35)
	);		
	var iconShape = { 
		coord: [14,0,16,1,17,2,18,3,19,4,19,5,20,6,20,7,20,8,20,9,20,10,20,11,20,12,20,13,20,14,19,15,19,16,18,17,18,18,18,19,17,20,17,21,16,22,16,23,15,24,15,25,14,26,14,27,13,28,13,29,12,30,12,31,11,32,11,33,11,34,9,34,9,33,9,32,8,31,8,30,7,29,7,28,6,27,6,26,5,25,5,24,4,23,4,22,3,21,3,20,3,19,2,18,2,17,1,16,1,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,1,5,1,4,2,3,3,2,4,1,6,0], 
		type: 'poly' 
	};
	var hotelIconShadow = new google.maps.MarkerImage(
		'img/markers/orange-shadow.png',
		new google.maps.Size(39, 35),
		new google.maps.Point(0, 0),
		new google.maps.Point(11, 35)
	);
	
	var latlng = new google.maps.LatLng(centerX,centerY);

	var mapOptions = {
		zoom: zoom,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.HYBRID,
		streetViewControl: streetView
	};
	map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	
	// Initialize Fluster and give it an existing map
	if (clustering)
		var fluster = new Fluster2(map); 
	
	
	
	
	function createMarker(hotel, latlng) {
		
		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			shadow: hotelIconShadow,
			icon: hotelIcon,
			shape: iconShape,
			title: hotel[2],
			zIndex: 1
		});
		google.maps.event.addListener(marker, "click", function() {
			if (infowindow) infowindow.close();
			if (hotel[4]) {
			infowindow = new google.maps.InfoWindow({content: '<h5>' + hotel[2] + '</h5><a href="marina/' + hotel[4] + '/' + hotel[3] +'.html">Visit Marina</a>'});
			infowindow.open(map, marker); }
			else { infowindow = new google.maps.InfoWindow({content: '<h5>' + hotel[2] + '</h5>'});
			infowindow.open(map, marker); }
		});
		return marker;
	}
	
	if (clustering) {
		// Set fluster styles for more than 0, 10, 20 and 40 markers
		fluster.styles = {
			0: {
				//image: 'http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0/images/m1.png',
				image: 'img/markers/fluster0.png',
				textColor: '#FFFFFF',
				width: 54,
				height: 54
			},
			10: {
				//image: 'http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0/images/m2.png',
				image: 'img/markers/fluster1.png',
				textColor: '#FFFFFF',
				width: 58,
				height: 58
			},
			20: {
				//image: 'http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0/images/m3.png',
				image: 'img/markers/fluster2.png',
				textColor: '#FFFFFF',
				width: 66,
				height: 66
			},
			40: {
				//image: 'http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0/images/m4.png',
				image: 'img/markers/fluster3.png',
				textColor: '#FFFFFF',
				width: 80,
				height: 80
			}
		};

		// Initialize Fluster
		// This will set event handlers on the map and calculate clusters the first time.
		fluster.initialize(); 
	}
	
	// Do we need street view?
	if (panoramaID != '') {
		var panoramaOptions = {
			position: latlng,
			pov: {
				heading: 34,
				pitch: 10,
				zoom: 1
			}
		};
		var panoramaView = new google.maps.StreetViewPanorama(document.getElementById(panoramaID), panoramaOptions);
		map.setStreetView(panoramaView);
	}
	
	mapDrawn = true;

}

// mapSetCenter() - Relocate/center a Google map
Site.mapSetCenter = function(lat, lng, zoom) {
	map.setCenter(new google.maps.LatLng(lat, lng));
	if (zoom > 0) map.setZoom(zoom);
} 

// Format : "(123) 456-7890" -->

var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 14;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
  phonevalue1 = ParseChar(object.value, zChar);
}

function ParseForNumber2(object){
  phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) { 
  if(e){ 
    e = e 
  } else {
    e = window.event 
  } 
  if(e.which){ 
    var keycode = e.which 
  } else {
    var keycode = e.keyCode 
  }

  ParseForNumber1(object)

  if(keycode >= 48){
    ValidatePhone(object)
  }
}

function backspacerDOWN(object,e) { 
  if(e){ 
    e = e 
  } else {
    e = window.event 
  } 
  if(e.which){ 
    var keycode = e.which 
  } else {
    var keycode = e.keyCode 
  }
  ParseForNumber2(object)
} 

function GetCursorPosition(){

  var t1 = phonevalue1;
  var t2 = phonevalue2;
  var bool = false
  for (i=0; i<t1.length; i++)
  {
    if (t1.substring(i,1) != t2.substring(i,1)) {
      if(!bool) {
        cursorposition=i
        window.status=cursorposition
        bool=true
      }
    }
  }
}

function ValidatePhone(object){

  var p = phonevalue1

  p = p.replace(/[^\d]*/gi,"")

  if (p.length < 3) {
    object.value=p
  } else if(p.length==3){
    pp=p;
    d4=p.indexOf('(')
    d5=p.indexOf(')')
    if(d4==-1){
      pp="("+pp;
    }
    if(d5==-1){
      pp=pp+")";
    }
    object.value = pp;
  } else if(p.length>3 && p.length < 7){
    p ="(" + p; 
    l30=p.length;
    p30=p.substring(0,4);
    p30=p30+") " 

    p31=p.substring(4,l30);
    pp=p30+p31;

    object.value = pp; 

  } else if(p.length >= 7){
    p ="(" + p; 
    l30=p.length;
    p30=p.substring(0,4);
    p30=p30+") " 

    p31=p.substring(4,l30);
    pp=p30+p31;

    l40 = pp.length;
    p40 = pp.substring(0,9);
    p40 = p40 + "-"

    p41 = pp.substring(9,l40);
    ppp = p40 + p41;

    object.value = ppp.substring(0, maxphonelength);
  }

  GetCursorPosition()

  if(cursorposition >= 0){
    if (cursorposition == 0) {
      cursorposition = 2
    } else if (cursorposition <= 2) {
      cursorposition = cursorposition + 1
    } else if (cursorposition <= 4) {
      cursorposition = cursorposition + 3
    } else if (cursorposition == 5) {
      cursorposition = cursorposition + 3
    } else if (cursorposition == 6) { 
      cursorposition = cursorposition + 3 
    } else if (cursorposition == 7) { 
      cursorposition = cursorposition + 4 
    } else if (cursorposition == 8) { 
      cursorposition = cursorposition + 4
      e1=object.value.indexOf(')')
      e2=object.value.indexOf('-')
      if (e1>-1 && e2>-1){
        if (e2-e1 == 4) {
          cursorposition = cursorposition - 1
        }
      }
    } else if (cursorposition == 9) {
      cursorposition = cursorposition + 4
    } else if (cursorposition < 11) {
      cursorposition = cursorposition + 3
    } else if (cursorposition == 11) {
      cursorposition = cursorposition + 1
    } else if (cursorposition == 12) {
      cursorposition = cursorposition + 1
    } else if (cursorposition >= 13) {
      cursorposition = cursorposition
    }

  }

}

function ParseChar(sStr, sChar)
{

  if (sChar.length == null) 
  {
    zChar = new Array(sChar);
  }
    else zChar = sChar;

  for (i=0; i<zChar.length; i++)
  {
    sNewStr = "";

    var iStart = 0;
    var iEnd = sStr.indexOf(sChar[i]);

    while (iEnd != -1)
    {
      sNewStr += sStr.substring(iStart, iEnd);
      iStart = iEnd + 1;
      iEnd = sStr.indexOf(sChar[i], iStart);
    }
    sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

    sStr = sNewStr;
  }

  return sNewStr;
}


