var map;
var res;
var helpslide = true;
var markerObj = new Array;
var wholesalerbox = false;
var isFilter = false;
var deleteid;
var activeobj;
/* define the coords(x,y) and the zoom level for the countries */ 
var navi_coords = new Object();
navi_coords["global"] = new Array('51.890053935216926','12.744140625',4);
navi_coords["at"] = new Array('51.358061573190916','10.78857421875',6);
navi_coords["de"] = new Array('51.358061573190916','10.78857421875',6);
navi_coords["uk"] = new Array('53.38332836757156','-4.2626953125',6);
navi_coords["es"] = new Array('40.17887331434696','-3.53759765625',6);
navi_coords["it"] = new Array('41.951319946796964','13.4033203125',6);
function endOfVideo()
{
	if(playlist.length > 1)
	{
		vid++;
		if (vid==playlist.length)
		{
			vid=1;
		}
	}
	putdiv(vid);
}

function initialize() 
{
  Effect.SlideUp('wholesalerselect', { duration: 2.0 });
  if (GBrowserIsCompatible()){
    map = new GMap2($('map_canvas'));
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(48.40003249610685, 11.6015625), 4);
    map.setUIToDefault();
	map.setMapType(G_PHYSICAL_MAP);
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    GEvent.addListener(map, "moveend", function(){
		var center = map.getCenter();
		//$("message").innerHTML = center.toString();
	});
    function createMarker(point, id){
      var Icon = new GIcon(baseIcon);
      Icon.image = "http://www.google.com/mapfiles/marker.png";
      markerOptions = { icon:Icon };
      var marker = new GMarker(point, markerOptions);
      
      GEvent.addListener(marker, 'click', function(){ 
      	marker.openExtInfoWindow(
        	map,
            "custom_info_window_red",
            "<div class=\"loading\">Loading...</div>",{
            	ajaxUrl: "ajax.php?id="+id, 
                beakOffset: 3
            }
      	); 
	  });
      //GEvent.addListener(marker, "click", function(){marker.openInfoWindowHtml(html);});
      return marker;
    }
    function loadBox(){
		new Ajax.Request("ajax.php?start=true",{
			onCreate: function(){},
			onSuccess: function(transport,response){
				res = transport.responseText.evalJSON();
				createMarks();
			},
			onFailure: function(){alert("error");}
		});
	}
	function createMarks(){
		for (var i=0;i<res.length;i++){
			var latlng = new GLatLng(res[i]['coord2'],res[i]['coord1']);
			this.markerObj[i] = createMarker(latlng, res[i]['boxid']);
	    	map.addOverlay(this.markerObj[i]);
		}
	}
	loadBox();
  }
}
function openBox(pName,pTitle,pCaption,pWidth,pHeight){
myLightWindow.activateWindow({
	href: pName,
	title: pTitle,
	caption: pCaption,
	width: pWidth,
	height: pHeight
});
}
function moveMap(pos){
	Effect.SlideUp('wholesalerselect', { duration: 0.0 });
	if(pos == "de") $('newsletterbox').style.display = "block";
	else $('newsletterbox').style.display = "none";
	//Effect.SlideUp('newsletterboxselect', { duration: 0.0 });
	map.closeExtInfoWindow();
	map.setCenter(new GLatLng(navi_coords[pos][0], navi_coords[pos][1]), navi_coords[pos][2]);
	if(helpslide){Effect.SlideUp('helpboxselect', { duration: 2.0 });helpslide = false;}
	if(pos != "global"){wholesaler(pos);};
	if(pos == "global"){Effect.SlideUp('wholesalerselect', { duration: 2.0 });$('wholesalerbox').style.display = "none";}
	
	if(this.isFilter) clearFilter();
}
function changeBG(state,obj)
{
	if(state == "in")
	{
		obj.style.backgroundColor = "#E5E5E5";
	}
	if(state == "out")
	{
		if(typeof(this.activeobj) != "object" || this.activeobj !== obj) obj.style.backgroundColor = "transparent";
	}
}
function filterWS(wholesaler,obj,id){
	map.closeExtInfoWindow();
	if(obj !== this.activeobj)
	{
		//first we hide all markers
		for(var i = 0; i<this.markerObj.length;i++) this.markerObj[i].hide();
			new Ajax.Request("ajax.php?wholesaler="+wholesaler,{
				onCreate: function(){},
				onSuccess: function(transport,response){
					useFilter(transport.responseText.evalJSON());
					this.isFilter = true;
				},
				onFailure: function(){alert("error");}
			});
			if(typeof(this.activeobj) != "object") this.activeobj = $('wholetr'+id);
			else{
				this.activeobj.style.backgroundColor = "transparent";
				this.activeobj = $('wholetr'+id);
				$('deleteit'+this.deleteid).style.display = "none";
			}
			this.activeobj.style.backgroundColor = "#E5E5E5";
			this.deleteid = id;
			$('deleteit'+this.deleteid).style.display = "block";
	}
}
function useFilter(obj){
	for(var i = 0; i<obj.length;i++){this.markerObj[obj[i]['boxid'].toString()-1].show();}
}
function clearFilter(){
	for(var i = 0; i<this.markerObj.length;i++) this.markerObj[i].show();
	this.activeobj.style.backgroundColor = "transparent";
	this.activeobj = "null"; //unsetting doenst work in ie... however we set a string and ask for objects when we need it
	$('deleteit'+this.deleteid).style.display = "none";
}
function wholesaler(pos)
{
	new Ajax.Request("ajax.php?country="+pos,{
			onCreate: function(){},
			onSuccess: function(transport,response){
				$('wholesalerselecttext').innerHTML = transport.responseText;
				$('wholesalerbox').style.display = "block";
				//Effect.SlideDown('wholesalerselect', { duration: 2.0 });
				Effect.toggle('wholesalerselect', 'slide');
				createMarks();
			},
			onFailure: function(){alert("error");}
		});
		
}

// call inititalize when page loaded event is fired
var testimonial;
Event.observe(window, 'load', function(){
  initialize();
  setTimeout('checkTestimonial()',100);
});

function checkTestimonial()
{
	if(testimonial == "de")
	{
		openBox("testimonial.php","","",640,465);
	}
}
function getURLParam(strParamName){
		var strReturn = "";
		var strHref = window.location.href;
		if ( strHref.indexOf("?") > -1 ){
			var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
			var aQueryString = strQueryString.split("&");
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
				if(aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1){
					var aParam = aQueryString[iParam].split("=");
					strReturn = aParam[1];
					break;
				}
			}
		}
		return unescape(strReturn);
}
