var ie=/MSIE (\d+\.\d+);/.test(navigator.userAgent);

var kmlRepository = "http://www.geotrips.eu/tracks/";
var tourSkeletonKml = kmlRepository + 'tourSkeleton.kml'; 
var tourSkeleton = null;
var tourLongitude = null;
var tourLatitude = null;



function Media() {
}
//List of possible media types
Media.PanoramaInternal = "PanoramaInternal";
Media.PicasaFlashVideo = "PicasaFlashVideo";
Media.YoutubeVideo =  "YoutubeVideo";
Media.MediaPlayerVideo =  "MediaPlayerVideo";


//PANORAMA
function PanoramaInternal(picasaTag, photo) {
	this.type = Media.PanoramaInternal;
	this.PICASA_TAG = "geotrips:pano_int:";
	
	if (!picasaTag.match(this.PICASA_TAG)) return;
	//alert(picasaTag);
	var fov = picasaTag.replace(this.PICASA_TAG,'').split('x');
	if (fov.length < 2) return;

	//TODO: also starting an maximal zoom
	this.xfov = fov[0];
	this.yfov = fov[1];
	this.init_xfov = fov[2];
	this.max_xfov = fov[3];
	this.min_xfov = fov[4];
	
	this.photo = photo; //back reference to get full image url
	
	this.value = true;
}

//TODO - move here mediaPanoramaInternalLayer
PanoramaInternal.prototype.getHook = function() {
	var resultHTML = 
		'<a href="javascript:showMedia(\'' + this.photo.id + '\', \'' + this.type + '\')"> <img src="http://google-maps-icons.googlecode.com/files/panoramic.png"/> ' + 
		//'<img src="images/btn_click_here.gif"/>Enter Panorama' +
		'</a>';
	return resultHTML;
}


// Just code wrappers, should wrapped by div and stylled in invoker html
PanoramaInternal.prototype.getEmbedCode = function() {
	var media_x=576;
	var media_y=432;
	
	var panoSrc = this.photo.imgFullUrl;
	if (ie) { //IE doesnt hadnel picasa attachement mode for full size pictures
		var path = this.photo.imgFullUrl.split('/');
		// TODO: Depenedency on global variable tripId
		panoSrc = kmlRepository + tripId + '/panoramas/flash/' + path[path.length-1];
	}
	//alert(panoSrc);
	var src = 'http://www.geotrips.eu/pan0.swf?panoSrc=' +  panoSrc;
	var init_xfov = this.init_xfov;
	var max_xfov = this.max_xfov;
	var min_xfov = this.min_xfov;
	
	var init_xfov = this.init_xfov; 
	if (!init_xfov) {
		init_xfov = 70;
	}
	
	var max_xfov = this.max_xfov; 
	if (!max_xfov) {
		max_xfov = 90;
	}
	
	var min_xfov = this.min_xfov; 
	if (!min_xfov) {
		min_xfov = 20;
	}
		
	
	
	if (this.xfov) {
		//var maxfov =  Math.round(this.xfov/3.5);
		src = src + "&panHFOV=" + this.xfov + "&maxFOV=" + max_xfov + "&FOV=" + init_xfov + "&minFOV=" + min_xfov;
	}

	if (this.yfov) {
		src = src + "&panVFOV=" + this.yfov;
	}			 
	//alert(src);
	//$("#panoramaInternal").attr("src", src); // me do nut modify existing (prepared) div, but create whole new content
	//alert($("#panoramaInternal").attr("src"));	
	
	var html = 
	// attachement are cached first	
	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + 
	'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"' + 
	'width="100%" height="100%" title="Panorama">' +
	'<param name="allowFullScreen" value="true" />' +
    '<param name="movie" value="' + src + '"/>' +
    '<param name="quality" value="high" />' +
    '<param name="BGCOLOR" value="#000000" />' +
	'<embed id="panoramaInternal" src="' + src +'"' +
	' 	width="100%" height="100%"' + 
	'	allowFullScreen="true"' + 
	'	quality="high"  ' + 
	'	pluginspage="http://www.macromedia.com/go/getflashplayer"' +  
	'	type="application/x-shockwave-flash" bgcolor="#000000">' + 
	'</embed>' + 
	'</object>';

	return html;
}

PanoramaInternal.prototype.getGMapMarkerOptions = function() {
	var photoIcon = new GIcon(G_DEFAULT_ICON);
	photoIcon.iconSize = new GSize(32, 37);
        photoIcon.imageMap = [0,0, 32,0, 32,37, 0,37];
        photoIcon.iconAnchor = new GPoint(16, 37);
	photoIcon.image = "http://google-maps-icons.googlecode.com/files/panoramic.png";	
	// Set up our GMarkerOptions object
	var markerOptions = {icon:photoIcon, zIndexProcess : function () {return 600;}};
	return markerOptions;
}



//VIDEO
function PicasaFlashVideo(mediaXmlNode, photo) {
	this.type = Media.PicasaFlashVideo;
	var c = mediaXmlNode;
	
	if (!c.getAttribute("medium") || c.getAttribute("medium") != "video") return;
	if (!c.getAttribute("type") || c.getAttribute("type") != "application/x-shockwave-flash") return;
	//if (!c.getAttribute("type") || c.getAttribute("type") != "video/mpeg4") return;
	
	this.url = c.getAttribute("url");
	
	this.photo = photo;
	this.value = true;
}

PicasaFlashVideo.prototype.getHook = function() {
	var resultHTML = 
		'<a href="javascript:showMedia(\'' + this.photo.id + '\', \'' + this.type + '\')"> <img src="http://google-maps-icons.googlecode.com/files/video.png"/> ' + 
		//'<img src="images/btn_click_here.gif"/>Enter Panorama' +
		'</a>';
	return resultHTML;
}

//TODO: make configurable + renderIcon + sethook (javascript)
PicasaFlashVideo.prototype.getEmbedCode = function() {
	//alert(this.url);
	var html = '<embed height="507" width="640"' +
		'src="http://video.google.com/googleplayer.swf?videoUrl=' +
		escape(this.url) +
		'&hl=en_US&autoplay=yes"' +
		'type="application/x-shockwave-flash"' +
		'flashvars="fs=true&playerMode=normal" allowfullscreen="true"'+
		'allowscriptaccess="always" bgcolor="#fff" quality="best" salign="TL"'+
		'scale="noScale" swliveconnect="true" wmode="opaque"'+
		'style="width: 100%; height: 100%;" />';

//	<object
//	style="width: 400px; height: 326px;" id="VideoPlayback" align="middle"
//	type="application/x-shockwave-flash"
//	data="http://video.google.com/googleplayer.swf?videoUrl=http%3A%2F%2Fvp.video.google.com%2Fvideodownload%3Fversion%3D0%26secureurl%3DiQAAAC5dDu0Dy9j7O5ytV4Gx-_gp37Kb1SfTIyRhZ9AnwXxAOC_6CUNuLubWIeDa7yU1ZV506d4w8qjTx6nHcPGSq-2RjRNNDUirncSTyh_nfwlYOVqEmyfW4KrFsJQg2aaKIvnnTM7VcXO_w61JRNIoEsA83AIKLMgP2vxfzVHS3OIQY9p1hpwKq2aCOmkAizuZ_w%26begin%3D0%26len%3D188199&thumbnailUrl=http%3A%2F%2Fvideo.google.com%2FThumbnailServer%3Fcontentid%3D785f4a36c7f33cf4%26second%3D5%26itag%3Dw320%26urlcreated%3D1137193860%26sigh%3DMj8xsDGxZ5rT4nZx4X-a0WyRPc0&playerId=-8947518926298275439&playerMode=embedded">
//	<param name="allowScriptAccess" value="sameDomain" />
//	<param name="movie"
//		value="http://video.google.com/googleplayer.swf?videoUrl=http%3A%2F%2Fvp.video.google.com%2Fvideodownload%3Fversion%3D0%26secureurl%3DiQAAAC5dDu0Dy9j7O5ytV4Gx-_gp37Kb1SfTIyRhZ9AnwXxAOC_6CUNuLubWIeDa7yU1ZV506d4w8qjTx6nHcPGSq-2RjRNNDUirncSTyh_nfwlYOVqEmyfW4KrFsJQg2aaKIvnnTM7VcXO_w61JRNIoEsA83AIKLMgP2vxfzVHS3OIQY9p1hpwKq2aCOmkAizuZ_w%26begin%3D0%26len%3D188199&thumbnailUrl=http%3A%2F%2Fvideo.google.com%2FThumbnailServer%3Fcontentid%3D785f4a36c7f33cf4%26second%3D5%26itag%3Dw320%26urlcreated%3D1137193860%26sigh%3DMj8xsDGxZ5rT4nZx4X-a0WyRPc0&playerId=-8947518926298275439&playerMode=embedded" />
//	<param name="quality" value="best" />
//	<param name="bgcolor" value="#ffffff" />
//	<param name="scale" value="noScale" />
//	<param name="wmode" value="window" />
//	<param name="salign" value="TL" />
//	</object>

	return html;
}


PicasaFlashVideo.prototype.getGMapMarkerOptions = function() {
	var photoIcon = new GIcon(G_DEFAULT_ICON);
	photoIcon.iconSize = new GSize(32, 37);
        photoIcon.imageMap = [0,0, 32,0, 32,37, 0,37];
        photoIcon.iconAnchor = new GPoint(16, 37);
        photoIcon.image = "http://google-maps-icons.googlecode.com/files/video.png";
	// Set up our GMarkerOptions object
	var markerOptions = {icon:photoIcon, zIndexProcess : function () {return 500;}};
	return markerOptions;
}

//YOUTUBE Video
function YoutubeVideo(picasaTag, photo) {
	this.type = Media.YoutubeVideo;
	this.PICASA_TAG = "geotrips:youtube:";

	if (!picasaTag.match(this.PICASA_TAG)) return;
	//alert(picasaTag);
	var url = picasaTag.replace(this.PICASA_TAG,'');
	this.url = url;

	this.photo = photo; //back reference to get full image url

	this.value = true;
}

//TODO: make configurable + renderIcon + sethook (javascript)
YoutubeVideo.prototype.getEmbedCode = function() {
	//alert(this.url);
        var url = escape(this.url) + "?rel=0&autoplay=1&fs=1";
        var html='<object width="100%" height="100%">' +
            '<param name="movie" value="' +  'http://www.youtube.com/v/' + url + '"> </param>' +
            '<param name="allowFullScreen" value="true"></param>' +
            '<param name="allowscriptaccess" value="always"></param>' +
            '<embed src="' + 'http://www.youtube.com/v/' + url + '" ' +
            'type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"' +
            'width="100%" height="100%">' +
            '</embed></object>'
	return html;
}


YoutubeVideo.prototype.getHook = function() {
	var resultHTML =
		'<a href="javascript:showMedia(\'' + this.photo.id + '\', \'' + this.type + '\')"> <img src="http://google-maps-icons.googlecode.com/files/video.png"/> ' +
		//'<img src="images/btn_click_here.gif"/>Enter Panorama' +
		'</a>';
	return resultHTML;
}


YoutubeVideo.prototype.getGMapMarkerOptions = function() {
	var photoIcon = new GIcon(G_DEFAULT_ICON);
        photoIcon.iconSize = new GSize(32, 37);
        photoIcon.imageMap = [0,0, 32,0, 32,37, 0,37];
        photoIcon.iconAnchor = new GPoint(16, 37);
	photoIcon.image = "http://google-maps-icons.googlecode.com/files/video.png";
	// Set up our GMarkerOptions object
	var markerOptions = {icon:photoIcon, zIndexProcess : function () {return 500;}};
	return markerOptions;
}

//MediaPlayer Video
function MediaPlayerVideo(picasaTag, photo) {
	this.type = Media.MediaPlayerVideo;
	this.PICASA_TAG = "geotrips:mediaplayer:";

	if (!picasaTag.match(this.PICASA_TAG)) return;
	//alert(picasaTag);
	var url = picasaTag.replace(this.PICASA_TAG,'');
	this.url = url;

	this.photo = photo; //back reference to get full image url

	this.value = true;
}

//TODO: make configurable + renderIcon + sethook (javascript)
MediaPlayerVideo.prototype.getEmbedCode = function() {
	//alert(this.url);
        var url = escape('http://' + this.url);
        //TODO: only for specific customer - make general support&licence
        var html='<object width="100%" height="100%">' +
            '<param name="movie" value="' +  'http://www.mikroregionkahan.cz/mediaplayer.swf' + '</param>' +
            '<param name="flashVars" value="file=' + url + '.flv' + '&amp;autostart=true">' + '</param>' +
            '<param name="allowFullScreen" value="true"></param>' +
            '<param name="allowscriptaccess" value="always"></param>' +
           '<embed src="' + 'http://www.mikroregionkahan.cz/mediaplayer.swf"' +
             ' flashvars="file=' + url + '.flv' +
             '&amp;autostart=true"' +
            'type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"' +
            'width="100%" height="100%">' +
            '</embed></object>'
	return html;
}


MediaPlayerVideo.prototype.getHook = function() {
	var resultHTML =
		'<a href="javascript:showMedia(\'' + this.photo.id + '\', \'' + this.type + '\')"> <img src="http://google-maps-icons.googlecode.com/files/video.png"/> ' +
		//'<img src="images/btn_click_here.gif"/>Enter Panorama' +
		'</a>';
	return resultHTML;
}


MediaPlayerVideo.prototype.getGMapMarkerOptions = function() {
	var photoIcon = new GIcon(G_DEFAULT_ICON);
        photoIcon.iconSize = new GSize(32, 37);
        photoIcon.imageMap = [0,0, 32,0, 32,37, 0,37];
        photoIcon.iconAnchor = new GPoint(16, 37);
	photoIcon.image = "http://google-maps-icons.googlecode.com/files/video.png";
	// Set up our GMarkerOptions object
	var markerOptions = {icon:photoIcon, zIndexProcess : function () {return 500;}};
	return markerOptions;
}



// SPECIFIC MAP ICON TYPES - determine marker options from metadata
function SpecificMapIcon(picasaTag, photo) {
	this.PICASA_TAG = "geotrips:icon_type:";

	if (!picasaTag.match(this.PICASA_TAG)) return;
	//alert(picasaTag);
	this.type = picasaTag.replace(this.PICASA_TAG,'');

        this.options = getMapIcon(this.type);

	this.value = true;
}


//Get specific map Icons - determined by creation - not object method call
function getMapIcon(iconType) {
    var photoIcon = new GIcon(G_DEFAULT_ICON);
    photoIcon.iconSize = new GSize(32, 37);
    photoIcon.imageMap = [0,0, 32,0, 32,37, 0,37];
    photoIcon.iconAnchor = new GPoint(16, 37);
    // Set up our GMarkerOptions object

   //default
    photoIcon.image = "http://google-maps-icons.googlecode.com/files/video.png";
    var deafultZindex = 400;
    if (iconType == "hotel") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/hotel.png";
    } else if (iconType == "skiareal") {
        photoIcon.image = "http://google-maps-icons.googlecode.com/files/skiing.png";
        deafultZindex = 700;
    } else if (iconType == "party") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/party.png";
       deafultZindex = 401;
    } else if (iconType == "music") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/music-classical.png";
       deafultZindex = 402;
    } else if (iconType == "winetasting") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/winetasting.png";
       deafultZindex = 403;
    } else if (iconType == "gourmet") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/gourmet.png";
       deafultZindex = 404;
    } else if (iconType == "restaurant") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/restaurant.png";
       deafultZindex = 405;    
    } else if (iconType == "parking") {
       photoIcon.image = "http://www.geotrips.eu/images/parking-icon.png";
       deafultZindex = 406;
    } else if (iconType == "farmersmarket") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/market-farm.png";
       deafultZindex = 407;
    } else if (iconType == "jewelry") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/jewelry.png";
       deafultZindex = 408;
    } else if (iconType == "vegetarian") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/vegetarian.png";
       deafultZindex = 409;
    } else if (iconType == "cascade-i") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/cascade-i.png";
       deafultZindex = 410;
    } else if (iconType == "cascade-ii") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/cascade-ii.png";
       deafultZindex = 411;
    } else if (iconType == "cascade-ii-plus") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/cascade-ii-plus.png";
       deafultZindex = 412;
    } else if (iconType == "cascade-iii") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/cascade-iii.png";
       deafultZindex = 414;
    } else if (iconType == "cascade-iv") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/cascade-iv.png";
       deafultZindex = 415;
    } else if (iconType == "cascade-v") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/cascade-v.png";
       deafultZindex = 416;
    } else if (iconType == "cascade-x") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/cascade-x.png";
       deafultZindex = 415;
    } else if (iconType == "bridge") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/bridge.png";
       deafultZindex = 409;
    } else if (iconType == "barrage-n-lr") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/barrage-n-lr.png";
       deafultZindex = 450;
    } else if (iconType == "barrage-nr") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/barrage-nr.png";
       deafultZindex = 450;
    }else if (iconType == "barrage-yl-nlr") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/barrage-yl-nlr.png";
       deafultZindex = 450;
    } else if (iconType == "bridge-cascade-ii") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/bridge-cascade-ii.png";
       deafultZindex = 450;
       photoIcon.imageMap = [0,0, 57,0, 57,37, 0,37];
       photoIcon.iconSize = new GSize(57, 37);
       photoIcon.iconAnchor = new GPoint(28, 37);
    } else if (iconType == "bridge-barrage-yl-nlr") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/bridge-barrage-yl-nlr.png";
       deafultZindex = 450;
       photoIcon.imageMap = [0,0, 57,0, 57,37, 0,37];
       photoIcon.iconSize = new GSize(57, 37);
       photoIcon.iconAnchor = new GPoint(28, 37);
    } else if (iconType == "mine") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/mine.png";
       deafultZindex = 450;
    } else if (iconType == "mine-closed") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/mine_closed.png";
       deafultZindex = 450;
    } else if (iconType == "geocache") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/geocache.png";
       deafultZindex = 450;
    } else if (iconType == "steamtrain") {
       photoIcon.image = "http://www.geotrips.eu/images/icons/steamtrain.png";
       deafultZindex = 450;
    } else if (iconType == "videospot") {
       photoIcon.image = "http://google-maps-icons.googlecode.com/files/video.png";
       deafultZindex = 700;
    } else if (iconType.indexOf("barrage")!=-1) {
       photoIcon.image = "http://www.geotrips.eu/images/icons/" + iconType + ".png";
       deafultZindex = 500;
    } else {
       photoIcon.image = "http://www.geotrips.eu/images/icons/" + iconType + ".png";
       deafultZindex = 400;
    }
       
   
   
    var markerOptions = {icon:photoIcon, zIndexProcess : function () {return deafultZindex;}};
    return markerOptions;    
}


//UTILITY
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function ra_de(ra) {
	var pi = Math.PI;
	var ra_de = ra*(180/pi);
	return ra_de;	
}

function de_ra(de) {
	var pi = Math.PI;
	var de_ra = de * (pi/180);
	return de_ra;
}

function join_asoc(a, b) {
	var result = [];
	for (i in a) {
		result[i] = a[i];
	}
	for (i in b) {
		result[i] = b[i];
	}
	return result;
}

