
var MyOverlay = function(marker, html) {
  this.marker = marker;
  this.html = html;
}

$(document).ready(function(){

	scriptSrc = $('script[@src$=jquery.js]').attr('src').replace('_addons/jquery/jquery.js', '');
	
	// General hover image function
	$('.hoverimage').each(hoverMe);

	
	// Search start
	if($('.search').length){ searchActions(); }
	
	// Searchinput reset
	if($('#ctl00_cp_Search1_tbSearch').length){ initSearchReset(); }
	
	// Beach suggestion hover
	if($('div.beach_suggest').length){ beachSuggest(); }
	
	if($('div.prefslider').length){ initSliders(); }
	//if($('div.feedslider').length){ initFeedbackSliders(); } //moved to window.onload in addbeach.aspx & userratingform.ascx
	
	// Clear input search field on forms and certain links
	$("form").submit(clearSearch);
	if ($("#ctl00_cp_lbRegister")) $("#ctl00_cp_lbRegister").click(clearSearch);
	if ($("#ctl00_cp_lbLostPwd")) $("#ctl00_cp_lbLostPwd").click(clearSearch);

	// Start sifr
	var source = {
		src: scriptSrc + '_addons/sifr3-r360/flash/neuesans.swf'
	};	

	sIFR.useDomContentLoaded = false;
	sIFR.activate();
	
	sIFR.replace(source, {
		selector: '.header h2'
		,wmode: 'transparent'
		,css: {
			'.sIFR-root': { 'color': '#019ACD', 'font-size': '22px' }
		}
	});
	
	sIFR.replace(source, {
		selector: '#aboutus h3, #aboutus h2, #contactinfo h3, .add_a_beach .left h3'
		,wmode: 'transparent'
		,css: {
			'.sIFR-root': { 'color': '#019ACD', 'font-size': '22px' }
		}
	});
	
	sIFR.replace(source, {
		selector: 'h6'
		,wmode: 'transparent'
		,css: {
			'.sIFR-root': { 'color': '#019ACD', 'font-size': '16px' }
		}
	});
	
	sIFR.replace(source, {
		selector: '#bluebar h2'
		,wmode: 'transparent'
		,css: {
			'.sIFR-root': { 'color': '#FFFFFF', 'font-size': '32px' }
		}
	});
	sIFR.replace(source, {
		selector: '#bluebar #sideboxesTitle .title'
		,wmode: 'transparent'
		,css: {
			'.sIFR-root': { 'color': '#FFFFFF', 'font-size': '22px' }
		}
	});
	
	$('body').pngFix( );
	
	// Create custom overlay
	if (typeof GOverlay !== 'undefined') {
		
		MyOverlay.prototype = new GOverlay();
		
		// Add listener to map for removing custom overlay on zoom
		//GEvent.addListener(subgurim_GMap1, "zoomend", closeOverlay);
		
		// Init my overlay
		MyOverlay.prototype.initialize = function(map) {
		  var div = document.createElement("div");
		  div.className = 'myInfoWindow';
		  div.innerHTML = '<div class="windowHolder"><div class="infoHolder">'+this.html+'</div></div><img src="/_ui/images/map-pointer.png" width="16" height="12" class="infoPointer" />';
		
		  // offsets based on popup div dimensions
		  offsetX = 107;
		  offsetY = 110;
		
		  div.style.top = (map.fromLatLngToDivPixel(this.marker.getPoint()).y - offsetY) + 'px';
		  div.style.left = (map.fromLatLngToDivPixel(this.marker.getPoint()).x - offsetX) + 'px';
		
		  div.onclick = closeOverlay;
		
		  this._map = map;
		  this._div = div;
		
		  map.getPane(G_MAP_FLOAT_PANE).appendChild(div);  
		}
		
		MyOverlay.prototype.remove = function(){
		  this._div.parentNode.removeChild(this._div);
		}
		
		MyOverlay.prototype.redraw = function() {
		  //haven't had need for this yet
		}
	}
	
});

function hoverTabs() {
	// Menu hover function
	$('#tabber1 .tabbernav li').hover(
		function(){ 
			$(this).addClass('over'); 
		},
		function(){ 
			$(this).removeClass('over');
		}
	);
}

function hoverMe(){
	$(this).hover(
		function(){
			var thisItem = $(this);
			if (thisItem.is("img") || thisItem.is('input')) {
				thisItem.attr("src", thisItem.attr("src").replace(/_out./, "_over."));
			}else if (thisItem.is("a")){
				thisItem.css("background-image", thisItem.css("background-image").replace(/_out./, "_over."));
			}
		},
	  	function(){
			var thisItem = $(this);
			if (thisItem.is("img") || thisItem.is('input')) {
				thisItem.attr("src", thisItem.attr("src").replace(/_over./, "_out."));
			}else if (thisItem.is("a")){
				thisItem.css("background-image", thisItem.css("background-image").replace(/_over./, "_out."));
			}
		}
	);

}

function searchActions(){
	//$('div.search .button').click(function(){
	//	$(this).parent().children('div.dropdown').toggleClass('showing');
	//});
	
	$('div.search li').hover(function(){
		$(this).addClass('over');
	},function(){
		$(this).removeClass('over');
	});
}
var nameloc = '<name or location>';
function initSearchReset(){
	$('#ctl00_cp_Search1_tbSearch').mousedown(function(){
		if($(this).val() == nameloc){ $(this).val(''); }
	});
	$('#ctl00_cp_Search1_tbSearch').blur(function(){
		if(!$(this).val()){ $(this).val(nameloc); }
	});
}

function beachSuggest(){
	$('div.beach_suggest a').hover(function(){
		$('.info', this).show();
	},function(){
		$('.info', this).hide();
	});
}

function initFeedbackSliders(){
	
	var feedWidth = 73.0;
	var feedPoints = 5.0;
	var feedFactor = feedWidth / feedPoints;
	
	// Set the feedback sliders
	$('div.feedslider').each(function(i){
		$(this).Slider({
			accept : '.feedindicator',
			onSlide: function( cordx, cordy, x , y){
				var  par = $(this).parent().parent();
				var score = scoreFromPos(x, feedFactor);
				$('> input.user', par).val(score);
				if($('> input.user', par).val() != 0 && $('> input.defined', par).val() == 0){
				AssignAddSliders();
					$('> input.defined', par).val(1);
					$('> div.feedslider', par).removeClass('beached');
				}
				setFeedSpans(par, score);
			},			
			values: [
				[getFeedVal(i, feedFactor, feedWidth),getFeedVal(i, feedFactor, feedWidth)]
			]
		});
	});
}

function initSliders(){
	
	var feedWidth = 73.0;
	var feedPoints = 5.0;
	var feedFactor = feedWidth / feedPoints;
	
	
	// Set the preference sliders
	$('div.prefslider').each(function(i){
		$(this).Slider({
			accept : '.prefindicator',
			onSlide: function( cordx, cordy, x , y){
				var  par = $(this).parent().parent();
				var score = scoreFromPos(x, feedFactor);
				if($('> input.user', par).val() != score){
                    if(!$($.iDrag.helper.get(0)).is(':visible')){
                        AssignSliders();
                    }
                }
				setPrefSpans(par, score);
				$('> input.user', par).val(score);
				if($('> input.user', par).val() != 0 && $('> input.defined', par).val() == 0){
					$('> input.defined', par).val(1);
				}
			},
			onChange: function () { AssignSliders(); },
			values: [
				[getPrefVal(i, feedFactor),getPrefVal(i, feedFactor)]
			]
		});
	});
}


function scoreFromPos(x, factor){
	return parseInt((x / factor)+0.7);
}

function AssignSliders(){
	preUsr1 = document.getElementById("prefuser1");	   
	preUsr2 = document.getElementById("prefuser2");
	preUsr3 = document.getElementById("prefuser3");
	preUsr4 = document.getElementById("prefuser4");
	document.getElementById("ctl00_cp_tbFamily").value=preUsr1.value;
	document.getElementById("ctl00_cp_tbActivity").value=preUsr2.value;
	document.getElementById("ctl00_cp_tbTranquility").value=preUsr3.value;
	document.getElementById("ctl00_cp_tbParty").value=preUsr4.value;
	 __doPostBack('ctl00$cp$hiddenLink','');    
}

function AssignAddSliders()
	{	  	
	
	    flag = document.getElementById("DivReviewBeach");	   
	    if(!flag)
	    {	    
            preNB = document.getElementById("sliderNB");
            preNS = document.getElementById("sliderNS");
            preSC = document.getElementById("sliderSC");
            preSS = document.getElementById("sliderSS");
            preClean = document.getElementById("sliderClean");
            preWC = document.getElementById("sliderWC");
            document.getElementById("ctl00_cp_hidNB").value=preNB.value;	             
            document.getElementById("ctl00_cp_hidNS").value=preNS.value;          
            document.getElementById("ctl00_cp_hidSC").value=preSC.value;         
            document.getElementById("ctl00_cp_hidSS").value=preSS.value;           
            document.getElementById("ctl00_cp_hidClean").value=preClean.value;        
            document.getElementById("ctl00_cp_hidWC").value=preWC.value;
        }
        else
        {
            preSand = document.getElementById("sliderSand");
            preSea = document.getElementById("sliderSea");
            preClean = document.getElementById("sliderClean");
            preFacilities = document.getElementById("sliderFacilities");
            preFood = document.getElementById("sliderFood");            
            preCrowd = document.getElementById("sliderCrowd");            
            preSafety = document.getElementById("sliderSafety");
            preBathing = document.getElementById("sliderBathing");
            preAccessibility = document.getElementById("sliderAccessibility");
            preSurroundings = document.getElementById("sliderSurroundings");
            prePricelevel = document.getElementById("sliderPricelevel");
            
            document.getElementById("ctl00_cp_URFrom_hidSand").value=preSand.value;	             
            document.getElementById("ctl00_cp_URFrom_hidSea").value=preSea.value;          
            document.getElementById("ctl00_cp_URFrom_hidClean").value=preClean.value;         
            document.getElementById("ctl00_cp_URFrom_hidFacilities").value=preFacilities.value;           
            document.getElementById("ctl00_cp_URFrom_hidFood").value=preFood.value;        
            document.getElementById("ctl00_cp_URFrom_hidCrowd").value=preCrowd.value;
            document.getElementById("ctl00_cp_URFrom_hidSafety").value=preSafety.value;	             
            document.getElementById("ctl00_cp_URFrom_hidBathing").value=preBathing.value;          
            document.getElementById("ctl00_cp_URFrom_hidAccessibility").value=preAccessibility.value;         
            document.getElementById("ctl00_cp_URFrom_hidSurroundings").value=preSurroundings.value;           
            document.getElementById("ctl00_cp_URFrom_hidPricelevel").value=prePricelevel.value;            
        }
	}
	


function getFeedVal(i, slideFactor, slideWidth){
	par = $('div.feedslider').eq(i).parent();
	if($('> input.defined', par).val() != 0){
		$('> div.feedslider', par).removeClass('beached');
		newLoc = ($('> input.user', par).val() * slideFactor);
		return parseInt(newLoc);
	}else{
		//return parseInt(slideWidth * 0.5);
		//return parseInt($('> input.onbeach', par).val() * slideFactor);
	}
}

function getPrefVal(i, slideFactor){
	par = $('div.prefslider').eq(i).parent();
	newLoc = ($('> input.user', par).val() * slideFactor);
	return parseInt(newLoc);
}

function setFeedSpans(par, score){
	onScore = $('> input.onbeach', par).val();
	if($('> div.feedslider', par).is('.beached')){
		onB = 1;
	}else{
		onB = 0;
	}
	$('> div > span', par).each(function(i){
		if(!onB){
			if(i < score){
				if(i < onScore){
					$(this).attr({'class': 'useronbeach'});
				}else{
					$(this).attr({'class': 'user'});
				}
			}else{
				if(i < onScore){
					$(this).attr({'class': 'onbeach'});
				}else{
					$(this).attr({'class': 'none'});
				}
			}
		}else{
			if(i < onScore){
				$(this).addClass('onbeach');
			}else{
				$(this).attr({'class': 'none'});
			}
		}
	});
}

function setPrefSpans(par, score){
	$('> div > span', par).each(function(i){
		if(i < score){
			$(this).addClass('user');
		}else{
			$(this).removeClass('user');
		}
	});
}

function clearSearch() {
	var value = $("#ctl00_cp_Search1_tbSearch").val();
	$("#ctl00_cp_Search1_tbSearch").val("");
}

var currentMarker;
function clickMarker(marker, html) {
  if (typeof MyOverlay !== 'undefined') {
	if (currentMarker) {
	  closeOverlay();
	}
	if (!marker.overlay) {
	  // just recording this for use in the closeOverlay function
	  marker.overlay = new MyOverlay(marker, html);
	}
	currentMarker = marker;
	subgurim_GMap1.panTo(new GLatLng(marker.getPoint().lat()+0.015, marker.getPoint().lng()));
	subgurim_GMap1.addOverlay(marker.overlay);
	//marker.hide();
  } else {
	marker.openInfoWindowHtml(html);
  }
}
function closeOverlay() {
  if (currentMarker) {
	subgurim_GMap1.removeOverlay(currentMarker.overlay);
	//currentMarker.show();
  }
}