$(function(){
	
	var colorBoxOnComplete = function (){
		if('ontouchstart' in document.documentElement){
			$('#cboxOverlay').width($('#wrapper').width());
			$('#cboxOverlay').height($('#wrapper').height()+15);
		}
	};
	
	if(screen.width < 1024){
		$('#rightContainer').css({
				margin		: '0px 0px 0px 221px', 
				overflow 	: 'scroll', 
				height		: '100%'
			});
		$('.menuContainer').css({ position: 'absolute' } );
	}
	
	if('ontouchstart' in document.documentElement){
		$('.menuContainer #menu .nav li a').click(function (e){
			e.preventDefault();
		});
	}else{
		$('.submenu-container .close_submenu').hide();
	}
	
	$('.listing .collapsed').hide();
//	$('.show .collapsed').slideDown({duration: 500, easing: 'easeOutQuad'});
	$('.listing .open').click(function (){
		var container = $(this).closest('h4').next('.collapsed');
		$('.collapsed').stop().slideUp({duration: 300, easing: 'easeInQuad'});
		if(container.hasClass('show')){
			$(this).removeClass('show');
			$('.collapsed').removeClass('show');
		}else{
			$('.collapsed, .listing .open').removeClass('show');
			container.addClass('show').slideDown({duration: 500, easing: 'easeOutQuad'});
			$(this).addClass('show');
		}
	}).first().click();
	
	$(".print-btn").click(function(){
		window.print();
		//$('head').append('<link href="/css/print.css" type="text/css" rel="stylesheet"><link href="/css/en-us/print.css" type="text/css" rel="stylesheet">');
	});
	
	dynamicRecord.init(function(){
		$(this).find('.recordHead .mark').html('-');
	},function(){
		$(this).find('.recordHead .mark').html('+');
	});	
	$('.annual-report .recordHead:first').click();
	$('.side-highlight').each(function(){
		$(this).find('.cycle').cycle({prev:$(this).find('.prev'),next:$(this).find('.next')});
		$(this).find('.cycle_slow').cycle({timeout: 8000, prev:$(this).find('.prev'),next:$(this).find('.next')});
	});
	/*$('.cycle').filter(function(){return $(this).parent('.img-ie-valign').length<=0}).each(function(){
		$(this).cycle({prev:$(this).siblings('.control').find('.prev'),next:$(this).siblings('.control').find('.next')});
	})*/
	if($('.side-highlight .cycle img, .side-highlight .cycle_slow img').length < 2){
		$('.side-highlight .prev, .side-highlight .next').hide();
	}
	$('.zoom').colorbox({
			maxWidth: '75%', 
			maxHeight: '90%', 
			onComplete: colorBoxOnComplete
		});
	$('.colorbox_ifream, .corporate-video a').click(function (e){
		$.colorbox({
			iframe:true, 
			href: $(this).attr('href'), 
			width: '625px',
			height: '505px' , 
			scrolling: false, 
			onComplete: colorBoxOnComplete
		});
		e.preventDefault();
	});
	$('#milestones .nav').jcarousel({ 
		scroll: 1, 
		start: $('#milestones .nav li.selected').index(), 
		initCallback: function (){
			$('#milestones .year').css('visibility', 'visible');
		}
	});
	fontSize.init();
	$('#font-increase').click(function(e){
		e.preventDefault()
		fontSize.set(fontSize.get()+2)
	})
	$('#font-decrease').click(function(e){
		e.preventDefault()
		fontSize.set(fontSize.get()-2)
	}) 

	fontContrast.init()
	$('#contrast').click(function(e){
		e.preventDefault()
		fontContrast.set(!fontContrast.get())
	})
	$(".print").click(function(){window.print();});
	
	$('a.apply').colorbox({
			iframe:true,
			width:"550",
			height:"480",
			scrolling:false, 
			onComplete: colorBoxOnComplete
		});
	
	searchBox.init($('#formSearchArea .fieldKeyword'),$('#formSearchArea .cue').attr('value'))
	
	$('.menuContainer #menu .nav li a:not(#menu-group-business)').mouseover(function (){	
			$('#submenu-special').hide();
			$('#submenu-basic').show();
		if(!$('.submenu-container').hasClass('show')){
			$('.submenu-container').addClass('show').css({opacity: 1.0}).stop().hide().fadeIn(300);
		}else if(!$('#submenu-basic').hasClass('show')){
			$('#submenu-special').removeClass('show').stop().fadeOut(300, function (){
				$('#submenu-basic').addClass('show').stop().hide().css({opacity: 1.0}).fadeIn(300);
			});
		}else{
			$('#submenu-special').hide();
			$('#submenu-basic').show();
		}
		var item = $('#submenu-basic').find('.'+$(this).attr('id'));
		item.addClass('hover').stop().animate({ backgroundColor: "#ffffff" }, 300);
		item.find('h2').stop().animate({ color: "#DF0E23" }, 300);
		return false;
	});
	
	$('.menuContainer #menu .nav li a:not(#menu-group-business)').mouseout(function (){	
		var item = $('#submenu-basic').find('.'+$(this).attr('id'));
		item.removeClass('hover').stop().animate({ backgroundColor: "#1E0E0E" }, 300);  
		item.find('h2').stop().animate({ color: "#FFFFFF" }, 300);
	});
	
	$('.menuContainer #menu .nav li a#menu-group-business').mouseover(function (e){	
		if(!$('.submenu-container').hasClass('show')){
			$('#submenu-basic').hide();
			$('#submenu-special').show();
			$('.submenu-container').show().css({opacity: 1.0}).addClass('show').stop().hide().fadeIn(300);
		}else if(!$('#submenu-special').hasClass('show')){
			$('#submenu-basic').removeClass('show').stop().fadeOut(300, function (){
				$('#submenu-special').addClass('show').stop().hide().css({opacity: 1.0}).fadeIn(300);
			});
		}else{
			$('#submenu-basic').hide();
			$('#submenu-special').show();
		}
		e.stopPropagation();
	});
	
	
	$('.submenu-container').mouseover(function (e){
		e.stopPropagation();
	});

	$('.submenu-container .close_submenu').click(function (){
		$('.submenu-container>div.show').removeClass('show').stop().fadeOut(300, function (){
			$('.submenu-container').removeClass('show').hide();
		});
	});
	
	$('body').mouseover(function (){
		$('.submenu-container>div.show').removeClass('show').stop().fadeOut(300, function (){
			$('.submenu-container').removeClass('show').hide();
		});
	});
	
	$('.submenu .menu_ul > li').hover(function (){
		$(this).addClass('hover').stop().animate({ backgroundColor: "#ffffff" }, 300);
		$(this).find('h2').stop().animate({ color: "#DF0E23" }, 300);
	}, function (){
		$(this).removeClass('hover').stop().animate({ backgroundColor: "#1E0E0E" }, 300);  
		$(this).find('h2').stop().animate({ color: "#FFFFFF" }, 300);
	});
	
	$('.submenu .menu_ul .submenu_ul li a').mouseover(function (){
		var img = $(this).parent().find('img');
		if(img.length > 0){
			$(this).closest('.submenu_ul').closest('li').children('img').attr('src', img.attr('src'));
		}
	});
	
	$('.hopeWellNews .button').click(function(){
		latestNewsMenu.open()
		$(this).parent().hide();
		$('.news .cycle').each(function(){
			$(this).cycle({fx:'scrollHorz',timeout:0,prev:$(this).siblings('.control').find('.prev'),next:$(this).siblings('.control').find('.next')});
		})
	})
	$('.news-container .button').click(function(){
		latestNewsMenu.close(function(){
			$('.hopeWellNews .buttonArea').show();
			$('.news .cycle').each(function(){
				$(this).cycle('destroy');
			});
		})
		
	})
	
	$('#selectArea .otherSelect').click(function (e){
		$('#selectArea .selectionBox').addClass('show').show();
		$('#selectArea .selection').jScrollPane();
		e.stopPropagation();
	});
	$('#selectArea .close').click(function (e){
		$('#selectArea .selectionBox').removeClass('show').hide();
		e.stopPropagation();
	});
	
	$('#selectArea .selection').click(function (e){
		e.stopPropagation();
	});
	
	$('body').click(function (){
		if($('#selectArea .show').length > 0){
			$('#selectArea .show').removeClass('show').hide();
		}
	});
	
	$('form').submit(function(){
		
		var failField = $(this).find('.check').filter(function(){return !$(this).parent().hasClass('tips')}).validation()
		
		if(failField.length>0){
			var warningMsg = ''
			var lineSeparator = ($('.message_box').length > 0) ? '<br/>' : '\n';
			failField.each(function(){
				var dummy =  $(this);
				warningMsg += $('#warningMsg .fail'+dummy.attr('class').split('fail')[1].split(' ')[0]+'.'+dummy.attr('id')).text()+lineSeparator;
			})
			if($('.message_box').length > 0){
				$('.message_box').html(warningMsg);
			}else{
				alert(warningMsg);
			}
			return false;
		}
	})
	
	$('#preview_year').change(function (){
		window.location = $(this).val();
	});
	
	/*
	$('.hopeWellNews').mouseover(function(){
		$('.submenu-item-holder').removeClass("hover");
		$('.submenu-container').hide();
		$('.nav a').removeClass();
	});
	$('#wrapper').mouseleave(function(){
		$('.submenu-item-holder').removeClass("hover");
		$('.submenu-container').hide();
		$('.nav a').removeClass();
	});
	$('#menu .nav a').mouseover(function(){
	if($(this).attr("id") === "menu-group-business") {
			$('.submenu').hide();
			$('.nav a').removeClass();
			$('.submenu-container').show();
			$(this).addClass("current");
			$('#submenu-special').show();

	}else {
			$('.submenu').hide();
			$('.nav a').removeClass();
			$('.submenu-container').show();
			$(this).addClass("current");
			$('#submenu-basic').show();
			var id = $(this).attr("id").substr(5);
			$('.submenu-item-holder').removeClass("lock");
			$("#"+id).addClass("lock");
			$("#"+id).css("background-color", "");
			
	}

	});
	$('.submenu-container').mouseleave(function(){
		$('.submenu-container').hide();
		$('.nav a').removeClass();
	});
	
       $('.submenu-item-holder').mousestop(function() {

			$(this).delay(100);
			if($(this).attr("class").indexOf("hover") > 0 || $(this).attr("class").indexOf("lock") > 0) {
			} else {
				 $(this).queue(function () {
				$(this).animate({ backgroundColor: "#ffffff" }, 300).addClass("hover");
				$(this).dequeue();
			 });
			}	
       });
	$('.submenu-item-holder').mouseleave(function(){

		 if($(this).attr("class").indexOf("lock") != "-1" ) {
		 } else {
		 $(this).queue(function () {
			$(this).animate({ backgroundColor: "#1E0E0E" }, 300).removeClass("hover");  
			$(this).dequeue();
		});
		}
	});
	$('#submenu-special a').mouseenter(function(){
		var img_name = $(this).attr("id");
		if($(this).attr("id") != "") {
			$(this).parent().find("img").attr("src", webroot+"img/menu-img/"+img_name+".jpg");
		} else {
		}
	});
	
	*/
	
});

var mapUrl = false;
var mapCenter = false;

$(window).load(function (){
	if(typeof mapCenter  != 'undefined'){
		if(mapUrl){
			$.ajax({
				url: mapUrl, 
				type: 'GET', 
				dataType: 'xml', 
				error: function(xml){
					initializeMap("maps", mapCenter); 
				}, 
				success: function (xml){
					$(xml).find('building').each(function(i, elm){
						points.push({
							name: $(this).children("name").text(), 
							address: $(this).children("address").text().replace(/\n[\s]*/g,"<br>"), 
							x: $(this).children("x").text(), 
							y: $(this).children("y").text(), 
							img: $(this).children("image").text()
						});
						
					});
					initializeMap("maps", mapCenter); 
				}
			});
		}else{
			initializeMap("maps", mapCenter); 
		}
		
		$('.map_btn').click(function (){
			$('body').addClass('map');
			$.colorbox({
				innerWidth: '80%', 
				innerHeight: '80%',
				html: '<div id="map-area"></div>', 
				onComplete: function (){
//					colorBoxOnComplete();
					initializeMap("map-area", mapCenter);
				}, 
				onClose: function (){
					$('body').removeClass('map');
				}
			});
		});
	}
});

var points = [];


var initialLocation;
var browserSupportFlag =  new Boolean();

function setMarkers(map, locations, id) {
  // Add markers to the map

  // Marker sizes are expressed as a Size of X,Y
  // where the origin of the image (0,0) is located
  // in the top left of the image.

  // Origins, anchor positions and coordinates of the marker
  // increase in the X direction to the right and in
  // the Y direction down.
  var image = new google.maps.MarkerImage('/img/my_custom_icon.png',
      // This marker is 30 pixels wide by 44 pixels tall.
      new google.maps.Size(30, 44),
      // The origin for this image is 0,0.
      new google.maps.Point(0, 0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(15, 44));
  var shadow = new google.maps.MarkerImage('/img/my_custom_icon_shadow.png',
      // The shadow image is larger in the horizontal dimension
      // while the position and offset are the same as for the main image.
      new google.maps.Size(40, 29),
      new google.maps.Point(0, 0),
      new google.maps.Point(0, 29));
      // Shapes define the clickable region of the icon.
      // The type defines an HTML <area> element 'poly' which
      // traces out a polygon as a series of X,Y points. The final
      // coordinate closes the poly by connecting to the first
      // coordinate.
  var shape = {
      coord: [ 20,56, 1,21, 1,14, 4,6, 8,4, 16,1, 26,1, 32,4, 36,7, 38,11, 40,16, 40,22, 21,56 ],
      type: 'poly'
  };
  for (var i = 0; i < locations.length; i++) {
    var beach = locations[i];
    var myLatLng = new google.maps.LatLng(beach.x, beach.y);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        shadow: shadow,
        icon: image,
        shape: shape,
        zIndex: i
    });
	
	if(id == 'maps'){
		if(beach.img){
			marker.contentHtml = '<div class="address"><div class="name"><img src=' + beach.img + ' class="map_img" />'+beach.name+'</div><div class="clear">'+beach.address+'</div></div>';
		}else{
			marker.contentHtml = '<div class="address"><div class="name">'+beach.name+'</div>'+beach.address+'</div>';
		}
	}else{
		if(beach.img){
			marker.contentHtml = '<div class="address"><div><img src=' + beach.img + ' class="map_img_large" /><h3>'+beach.name+'</h3>'+beach.address+'</div>';
		}else{
			marker.contentHtml = '<div class="address"><div class="name">'+beach.name+'</div>'+beach.address+'</div>';
		}
	}
	
	var infoWin = null;
	google.maps.event.addListener(marker, 'click', function(e) {
		if(infoWin){
			infoWin.close();	
		}
		infoWin = new google.maps.InfoWindow({
			content: this.contentHtml,
			position: this.getPosition()
		});
		infoWin.open(map);
	});
	
  }
}

function initializeMap(id, mapCenter) {
	if(!document.getElementById(id)){
		return ;
	}
	var zoom = mapCenter.zoom;
	if(id == 'maps' && zoom > 4){
		zoom = zoom - 1;
	}
	var myOptions = {
		zoom: zoom,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById(id), myOptions);
	
	initialLocation = new google.maps.LatLng(mapCenter.x, mapCenter.y);
	map.setCenter(initialLocation);
	
	setMarkers(map, points, id);
	
}




