/**
 * 
 *  Portbooker API. Version 0.7
 *  Calling this JAVSCRIPT MuntiFuncion file to start iframe
 *  Author: Feliu Quetglas for Porbooker.com
 *  
**/

/**
 * Default vars
 */

var id = "pbIframeId";

var width = 750;
var height = 750;
var scroll = "auto";
var pbApi = "key";
var toolbarPosition = 'bottom';

/**
 * Design and colorize vars. All default values
 */
var border = "0px solid #ffffff";
var bgmapcolor = "FFFFFF";
var bgresultscolor = "F7F7F7";
var bgcuadrocolor = "F7F7F7";
var bgiframecolor = "F0F0F0";


/**
 * Language vars
 */

var language = "es";

/**
 * setInitialView Defaults (Europe)
 */

var swlat = 31.653381399664;
var	swlong = -14.0625;
var	nelat = 59.712097173322924;
var	nelong = 37.79296875;

/**
 * setViewLimits Defaults
 */

var swlatlimit = 0;
var swlonglimit = 0;
var nelatlimit = 0;
var nelonglimit = 0; 

/**
 * Set Country and Zone defaults limits
 */

var limit_countries = "";
var limit_zones = "";

/**
 * activeFilter, searchCity and searchCountry Vars
 */

var filter_limit = "";
var limit_city = "";
var limit_country = "";


/**
 * Start with API functions
 */

/**
 * Setter function that passes pbApi Security Var.
 * This var is sended by Portbooker.com if you don't have one, please, contact info@portbooker.com
 * @param {MD5} apikey
 */
function registerWidget(apikey) {
	pbApi = apikey;
}

/**
 * Setter function to define position of Advertising and Portbooker Logo position. If you have contract
 * that defines it would be shown you should not specfy it.
 * @param {Object} position
 */
function setLogoPosition(position) {
	toolbarPosition = position;
}


/**
 * Set Initial view off map using 4 parameters.
 * @param {Float} upperLeftLatitude
 * @param {Float} upperLeftLongitude
 * @param {Float} lowerRightLatitude
 * @param {Float} lowerRightLongitude
 */
function setInitialView(SWLat, SWLong, NELat, NELong) {
	swlat = SWLat;
	swlong = SWLong;
	nelat = NELat;
	nelong = NELong;
}


/**
 * Language Setter, sets language of iframe. If it is note defined it
 * gets the browser language.
 * DEFAULT: es
 * Available:
 * es : Espa�ol
 * en : English
 * de : Deutch
 * fr : Francais
 * it : Italiano
 * @param {String} languagevar
 */
function setLanguage(languagevar) {
	language = languagevar;
}

/**
 * Iframe Setter Background Colors.
 * @param {String} divBackgroundColor (String) Defines map DIV rounded background color and is color used on searches and left map results. (Color without '#')
 * @param {String} portDivBackgroundColor2 (String) Defines Zone search results color. (Color without '#')
 * @param {String} portDivBackgroundColor (String) Defines DIV Map results background color. (Color without '#')
 * @param {String} widgetBackgorundColor (String) Defines iframe general Backround Color. (Color without '#') 
 */
function setBackgroundColors(color1,color2,color3,color4) {
	bgmapcolor = color1;
	bgresultscolor = color2;
	bgcuadrocolor = color3;
	bgiframecolor = color4;
}


/**
 * Setter function for iframe width, the map 
 * width changes depends on this value Default value: 600.
 * @param {Integer} widthSel : Integer
 */
function setWidth(widthSel) {
	width = widthSel;
}

/**
 * Setter function for iframe height, the map 
 * height changes depends on this value. Default value: 600.
 * @param {Integer} widthSel : Integer
 */
function setHeight(heightSel) {
	height = heightSel;
}


function setViewLimits(SWLatlimit,SWLonglimit,NELatlimit,NELonglimit) {
	swlatlimit = SWLatlimit;
	swlonglimit = SWLonglimit;
	nelatlimit = NELatlimit;
	nelonglimit = NELonglimit;
}


/**
 * Activate the geographical restriction, and adds the list of countries to the visible items. 
 * Countries WOULD BE separated by comas. Ex: 20,21,22...
 * There are two types of geographical restriction scopes, by country and by zone, 
 * and both can be activated simultaneously by calling his respective method.
 * 
 * @param {Array} countries - List of countries codes to show. The countries are defined using ISO 3166-1 alpha-2 codes
 */
function addCountryRestriction(countries) {
	limit_countries = countries;
}

/**
 * Activate the geographical restriction, and adds the list of zones to the visible items. 
 * There are two types of geographical restriction scopes, by country and by zone, 
 * and both can be activated simultaneously by calling his respective method.
 * 
 * @param {Array} zones - List of zone codes to show. 
 * The zones names and location for each country can be found at http://www.portbooker.com/en/marinas-map/ 
 * entering in each country you can see in the left the list of zones, and in the map his location. 
 * The zone code can be located inside the URL where point each zone name link (for example in Greece, 
 * the link where points Dodecanese zone is http://www.portbooker.com/zone/35 , then the zone code is 35 
 * for Dodecanese)
 */
function addZoneRestriction(zones) {
	limit_zones = zones;
}

/**
 * Activate Filters on Marina's services
 * @param {String} filter
 */
function activeFilter(filter) {
	filter_limit = filter;
}

/**
 * Redirects home page to Find locations page and limits it
 * @param {String} city
 */
function searchCity(city) {
	limit_city = city;
}

/**
 * Redirets country find, can run this method and searchCity at same time
 * @param {String ISO Code} country
 */
function searchCountry(country) {
	limit_country = country;
}

/**
 * Simply it draws iframe content
 */
function createWidget(widgetDiv) {
    var pbFrame = document.createElement("IFRAME");
    var widgetDIV = document.getElementById(widgetDiv);
	pbFrame.id = id;
    if (width>=600 && width<=1000) {
		pbFrame.width = width;
	} else {
		alert('Alert! You exceed minium or maxium widget width size. 600-1000px permited. Showing at default 750px. Chechk it out please!');
		pbFrame.width = 750;
	}
	if (height>=300 && height<=1000) {
		pbFrame.height = height;
	} else {
		alert('Alert! You exceed minium or maxium widget height size. 300-1000px permited. Showing at default 700px. Chechk it out please!');
		pbFrame.height = 750;
	}
	pbFrame.scrolling = scroll;
	//Deletes border on iexplorer too.
	pbFrame.frameBorder = 0;
	//Formats border
	pbFrame.style.border = border;
	//We select iframe limit only if user selected it.
	if (swlatlimit != 0 & swlonglimit != 0 & nelatlimit != 0 & nelonglimit != 0) {
		map_limit = 1;
	}
	else {
		map_limit = 0;
	}
	
	//alert(map_limit);
	//pbFrame.src = "http://test.portbooker.com/widget/iframe.php?pbApi="+pbApi+"&hl="+language+"&width="+width+"&height="+height+"&logo="+showlogo+"&publi="+showpubli+"&accesos="+accesos+"&bgiframecolor="+bgiframecolor+"&bgcuadrocolor="+bgcuadrocolor+"&bgmapcolor="+bgmapcolor+"&bgresultscolor="+bgresultscolor+"&cuadrobusqueda="+cuadroBusqueda+"&logohref="+logohref;
    pbFrame.src = "http://www.portbooker.com/widget/iframe.php?pbApi="+pbApi+"&hl="+language+"&width="+pbFrame.width+"&height="+pbFrame.height+
				  "&bgiframecolor="+bgiframecolor+"&bgcuadrocolor="+bgcuadrocolor+"&bgmapcolor="+bgmapcolor+"&bgresultscolor="+bgresultscolor+
				  "&publipos="+toolbarPosition+"&swlat="+swlat+"&swlong="+swlong+
				  "&nelat="+nelat+"&nelong="+nelong;
	
	//add limit_countries and limit_zones restrictions
	
	if (limit_countries!='' && limit_zones!='') {
		//When both filter selected only use zone limitand ignore country limit
		pbFrame.src = pbFrame.src+"&limit_zones="+limit_zones;
	} else {
		pbFrame.src = pbFrame.src+"&limit_countries="+limit_countries+"&limit_zones="+limit_zones;
	}	
	
	//We compare vars to send content to iframe
	if (map_limit==1) {
		pbFrame.src = pbFrame.src+"&map_limit="+map_limit+"&swlatlimit="+swlatlimit+"&swlonglimit="+swlonglimit+"&nelatlimit="+nelatlimit+"&nelonglimit="+nelonglimit;	
	} else {
		pbFrame.src = pbFrame.src+"&map_limit="+map_limit;
	}
	
	//Check for activeFilter var. fuelDock only for moment.
	//Added filter parameter for STW Association
	if (filter_limit!='' && filter_limit!='fuelDock' && filter_limit!='stw') {
		alert("NO ACTIVE FILTER SHOWED! \n You select wrong value for activeFilter method please, choose correct one: \n Active Filter:\n================\n - fuelDock -> To show only marinas with Fuel Dock Stations\n\n");
	} else {
		pbFrame.src = pbFrame.src+"&activeFilter="+filter_limit;
	}
	
	//Check for searchCity limit
	if (limit_city!='') {
		pbFrame.src = pbFrame.src+"&searchCity="+limit_city;
	}
	
	//Check for searchCountry limit
	if (limit_country!='') {
		pbFrame.src = pbFrame.src+"&searchCountry="+limit_country;
	}
	
	//alert(pbFrame.src);
	widgetDIV.appendChild(pbFrame);
}