﻿if (!com_isMerged) {
    com.cs.require("com.cs.ui.tabs");
    com.cs.require("com.cs.forms.Validation");
}

dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.TabContainer");





function initWordsRemaining(txtProfile, limitWords, spanWordsRemaining) {
	new com.cs.forms.Validation.WordsLimit(txtProfile, limitWords, spanWordsRemaining);

}


function initMap(divMap, iFrame, lat, lng, content, txtLat, txtLng, editable) {
	dojo.addOnLoad(function() {
      
    onGoogleMapLoaded = function() { //called from iframe
        var map = iFrame.contentWindow.map;
		var marker;
		
		if (editable) {
			map.onClickOnMap = function(latlng){
				txtLat.value = latlng.lat();
				txtLng.value = latlng.lng();
				if (!marker) {
					marker = map.addMarker(latlng.lat(), latlng.lng(), content, false);
				}
				else {
					marker.setLatLng(latlng);
				}
			}
		}
		
		if (lat != 0 && lng != 0) {
			marker = map.addMarker(lat, lng, content, true);			
		}
		//This is since if google maps is hidden BEFORE it is loaded, an error in centering occurs.
		//STRANGE but that is it.  So I show the div of the Map and then hide it again once loaded depending on whether
		//the current tab is in the map section or not
		if (window.location.toString().indexOf("Map") == -1) {
			//divMap.style.display = "none";
		}
		divMap.style.position = "";
		divMap.style.top = "";
    }
    iFrame.src = "/map.html"; 
	if (editable) {
		iFrame.src += "?editable=true";
	}

 });
}


function initDoNotShow(chk) {
	chk = dojo.byId(chk);
	
	chk.onchange = function() {
		if (chk.checked) {
			if (!confirm("Are you sure that you do not want to show your property on Malta Hotel Guide?\r\n\r\nBy selecting this option, your property will not be shown in any search results.")) {
				chk.checked = false;
			}
		}
	}
	
}


function initPanels() {
 	dojo.query("div.panel").forEach(function(div) {
			var panel = new com.mhg.Panel(div);
		}
	);
 }
 
function initTabs(txtSelected){
    dojo.addOnLoad(function() {
        if (!com_isMerged) {
            com.cs.require("com.cs.dojo.dijit.layout.tabcontainer");
        }
        var tabContainer = new com.cs.dojo.dijit.layout.TabContainer("mainTabContainer");


        var txt = dojo.byId(txtSelected);

        var f = function(prevTab, newTab) {
            if (newTab != null) {
                var id = newTab.id;
                txt.value = id;
            }


            //Check if map is here.  This is a fix since google does not center it correctly if the map is not displayed initially

            var iFrame = dojo.query("iframe", newTab.domNode);
            if (iFrame.length > 0) {
                iFrame = iFrame[0];
                if (!iFrame.mapShownOk) {
                    iFrame.mapShownOk = true;

                    iFrame.src = iFrame.src; //Refresh
                }
            }

        }

        dojo.connect(tabContainer, "onChange", f);
        f(null, tabContainer.getSelectedTab());
    });
}


dojo.addOnLoad(function() {

	initPanels();
});
