function ShowMessage(msg, autoclose) {

	if(autoclose == undefined) autoclose = false;
	Message = msg;
	Message += ' <a href="javascript:;" onclick="$.fancybox.close();" style="font-weight: bold;">[X]</a>';
	if(autoclose) Message += ' <script language="javascript" type="text/javascript">setTimeout ("$.fancybox.close()", 2000); </script>';

	$(document).ready(function() { 
		$.fancybox.close();
		
		$.fancybox(Message,
		{ 
			'autoScale' : true, 
			'transitionIn' : 'none', 
			'transitionOut' : 'none',
			'speedIn' : 0, 
			'speedOut' : 0,
			'hideOnContentClick' : true,
			'hideOnOverlayClick' : true,
			'showCloseButton' : false,
			'overlayShow' : false
		}); 
		
		
	});
	
}

function ShowConfirmDelete(CallBack, ID) {

	Message = 'Are you sure you want to delete this?';
	Message += '<br /><br /><a href="javascript:;" onclick="' + CallBack + '(' + ID + ');">Yes, please delete.</a>';
	Message += '<br /><a href="javascript:;" onclick="$.fancybox.close();">No, I do not want to delete.</a>';
	Message += "<br />&nbsp;"

	$(document).ready(function() { 
		$.fancybox(Message,
		{ 
			'width' : 300,
			'height' : 300,
			'autoScale' : false, 
			'transitionIn' : 'fade', 
			'transitionOut' : 'fade',
			'speedIn' : 200, 
			'speedOut' : 100,
			'hideOnContentClick' : false,
			'hideOnOverlayClick' : false,
			'showCloseButton' : false,
			'overlayShow' : true
		}); 
		
		
	});	
}
	
function ShowPreview(url) {
	$(document).ready(function() { 
		$.fancybox(
		{ 
			'href' : url,
			'width' : 900, 
			'height' : 400, 
			'autoScale' : false, 
			'transitionIn' : 'none', 
			'transitionOut' : 'none', 
			'type' : 'iframe'
		});
	});
}



function ToggleSet(ParentID) {
	if(IsOpen(ParentID)) {
		CloseSet(ParentID);
	} else {
		OpenSet(ParentID);
	}
	if($('#AtlasOne-Footer')[0] != null)
		$('#AtlasOne-Footer')[0].style.top = ($('#AtlasOne-Main')[0].offsetHeight + 60) + 'px';
}
function OpenSet(ParentID) {
	$('#'+ParentID)[0].className = 'SetOpen';
}
function CloseSet(ParentID) {
	$('#'+ParentID)[0].className = 'SetClosed';
}
function IsOpen(ParentID) {
	return ($('#'+ParentID)[0].className == 'SetOpen');
}



/** Media Manager **/

var MediaManager_ButtonID;
var MediaManager_OwnerType;
var MediaManager_OwnerID;
var MediaManager_ListType;
var MediaManager_MediaList;
var MediaManager_Order;
var MediaManager_Feature;

function MediaManager(Button, Feature, OwnerType, OwnerID, ListType, MediaList, Order) {

	MediaManager_ButtonID = Button.id;
	MediaManager_OwnerType = OwnerType;
	MediaManager_OwnerID = OwnerID;
	MediaManager_ListType = ListType;
	MediaManager_MediaList = MediaList;
	MediaManager_Order = Order;
	MediaManager_Feature = Feature;
	

	$(document).ready(function() { 
		$.fancybox(
		{ 
			'href' : '/index.php?page=Media&type=' + ListType + '&disassociate=' + (($('#' + Button.id.replace('Button','Submitted'))[0].value.length > 0) ? 'true' : 'false'),
			'width' : 640, 
			'height' : 400, 
			'autoScale' : false, 
			'transitionIn' : 'none', 
			'transitionOut' : 'none', 
			'type' : 'iframe'
		});
		
		$('.pft-directory').live('click',function(){
			$(this).find('a').preventDefault();
			alert($(this).find('a').text());
			$('#AtlasOne-MediaUpload-Directory').val($(this).find('a').text());
		});
		
		$('.pft-file').live('click',function(){
			$(this).find('a').preventDefault();
			alert($(this).find('a').text());
			$('#imgPreview').attr('src',$(this).find('a').text());
		});
	});
}

function AssociateMedia(MediaPath) {

	ShowMessage('Associating media file...');
	
	var methodology = "";
	switch(MediaManager_ListType) {
		case 'images':
			methodology = 'AssignImageTo' + MediaManager_Feature;
			break;
		case 'documents':
			methodology = 'AssignDocumentTo' + MediaManager_Feature;
			break;
	} 
	
	$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: {
			method: methodology,
			propertyid: MediaManager_OwnerID,
			imagelist: MediaManager_MediaList,
			imagepath: MediaPath,
			order: MediaManager_Order
		},
		dataType: "xml",
		success: function(xmlResp){
			$('#'+MediaManager_ButtonID)[0].innerHTML = MediaPath.substring(MediaPath.lastIndexOf('/')+1);
			$('#'+MediaManager_ButtonID.replace('Button','Submitted'))[0].value = MediaPath;
			ShowMessage('Media file associated!', true);
		},
		error: function(xmlResp){
			ShowMessage('Media file associated failed. Please try again later.');
		}
	});
}

function DisassociateMedia() {
	ShowMessage('Disassociating media file...');
	
	var methodology = "";
	switch(MediaManager_ListType) {
		case 'images':
			methodology = 'UnassignImageFrom' + MediaManager_Feature;
			break;
		case 'documents':
			methodology = 'UnassignDocumentFrom' + MediaManager_Feature;
			break;
	} 
	
	$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: {
			method: methodology,
			propertyid: MediaManager_OwnerID,
			imagelist: MediaManager_MediaList,
			order: MediaManager_Order
		},
		dataType: "xml",
		success: function(xmlResp){
			$('#'+MediaManager_ButtonID)[0].innerHTML = 'add ' + MediaManager_ListType.substring(0,MediaManager_ListType.length-1) + "...";
			$('#'+MediaManager_ButtonID.replace('Button','Submitted'))[0].value = '';
			ShowMessage('Media file unassociated!', true);
		},
		error: function(xmlResp){
			ShowMessage('Media file unassociated failed. Please try again later.');
		}
	});
}



var ListManager_OwnerType;
var ListManager_OwnerID;
var ListManager_ListType;
var ListManager_MediaList;
var ListManager_Feature;
var ListManager_Order = 0;
var ListManager_ImageWidth;

function ListManager(Button, Feature, OwnerType, OwnerID, ListType, MediaList, ImageWidth) {

	ListManager_OwnerType = OwnerType;
	ListManager_OwnerID = OwnerID;
	ListManager_ListType = ListType;
	ListManager_MediaList = MediaList;
	ListManager_Feature = Feature;
	ListManager_ImageWidth = ImageWidth;
	

	$(document).ready(function() { 
		$.fancybox(
		{ 
			'href' : '/index.php?page=List&type=' + ListType,
			'width' : 640, 
			'height' : 400, 
			'autoScale' : false, 
			'transitionIn' : 'none', 
			'transitionOut' : 'none', 
			'type' : 'iframe'
		});
		
	});
}



var ImageManager_Table;
var ImageManager_ID;
var ImageManager_Field;
var ImageManager_ImageWidth;

function ImageManager(Button, Table, ID, Field, ImageWidth) {

	ImageManager_Table = Table;
	ImageManager_ID = ID;
	ImageManager_Field = Field;
	ImageManager_ImageWidth = ImageWidth;
	

	$(document).ready(function() { 
		$.fancybox(
		{ 
			'href' : '/index.php?page=Image&type=' + Field,
			'width' : 640, 
			'height' : 400, 
			'autoScale' : false, 
			'transitionIn' : 'none', 
			'transitionOut' : 'none', 
			'type' : 'iframe'
		});
		
	});
}



/**
 * Cookie Functions
 **/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}




/* Map */
var map, layer, markers;
var MapPopupTemplate = '<div style="width:200px;position:relative;overflow:hidden;"><div style="float:left"><b>%CompanyName%</b><br />%StreetAddress%<br />%City%, %State% %Zip% %Phone% %Status%</div><div style="float:right;margin:1em 0 0 6px;padding:2px;border-top: 1px solid #C9C8C5;border-bottom: 1px solid #C9C8C5;">%Details% %Website% %Directions%</div></div>';

function MapInit() {
    map = new OpenLayers.Map('map');
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    
    var gphy = new OpenLayers.Layer.Google(
        "Google Physical",
        {type: google.maps.MapTypeId.TERRAIN}
    );
    var gmap = new OpenLayers.Layer.Google(
        "Google Streets", // the default
        {numZoomLevels: 20}
    );
    var ghyb = new OpenLayers.Layer.Google(
        "Google Hybrid",
        {type: google.maps.MapTypeId.HYBRID, numZoomLevels: 20}
    );
    var gsat = new OpenLayers.Layer.Google(
        "Google Satellite",
        {type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22}
    );

    map.addLayers([gphy, gmap, ghyb, gsat]);

    // Google.v3 uses EPSG:900913 as projection, so we have to
    // transform our coordinates
    map.setCenter(new OpenLayers.LonLat(-97, 38).transform(
        new OpenLayers.Projection("EPSG:4326"),
        map.getProjectionObject()
    ), 4);
	
	markers = new OpenLayers.Layer.Markers( "Markers" );
	map.addLayer(markers);
    
    // add behavior to html
    for (var i=map.layers.length-1; i>=0; --i) {
        map.layers[i].animationEnabled = true;
    }
}

function AddMarker(index, lat, lon, popup) {
	var size = new OpenLayers.Size(20,31);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon('http://api-dev.atlas-advertising.com/Marker.aspx?index='+index,size,offset);
	var lonlat = new OpenLayers.LonLat(lon,lat);
	var marker = new OpenLayers.Marker(lonlat.transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()),icon);
	var feature = new OpenLayers.Feature(markers, lonlat);
	feature.closeBox = true;
	feature.popupClass = OpenLayers.Class(OpenLayers.Popup.FramedCloud, { } );
	feature.data.popupContentHTML = popup;
	marker.feature = feature;
	marker.events.register('mousedown', feature, function(evt) { 
	    if (this.popup == null) {
	        this.popup = this.createPopup(this.closeBox);
	        map.addPopup(this.popup);
	        this.popup.show();
	    } else {
	        this.popup.toggle();
	    }
		OpenLayers.Event.stop(evt); 
	});
    markers.addMarker(marker);
}
function ClearMarkers() {
	if(markers != null) {
		for (var i=markers.markers.length-1; i>=0; --i) {
			markers.removeMarker(markers.markers[i]);
		}
	}
}
function ResetBounds() {
	if(markers != null && markers.markers.length > 0) {
	
		minLeft = markers.markers[0].lonlat.lon;
		minBottom = markers.markers[0].lonlat.lat;
		maxRight = markers.markers[0].lonlat.lon;
		maxTop = markers.markers[0].lonlat.lat;
	
		for (var i=markers.markers.length-1; i>=0; --i) {
			if(markers.markers[i].lonlat.lon < minLeft) minLeft = markers.markers[i].lonlat.lon;
			if(markers.markers[i].lonlat.lon > maxRight) maxRight = markers.markers[i].lonlat.lon;
			if(markers.markers[i].lonlat.lat < minBottom) minBottom = markers.markers[i].lonlat.lat;
			if(markers.markers[i].lonlat.lat > maxTop) maxTop = markers.markers[i].lonlat.lat;
		}	
		
		var bounds = new OpenLayers.Bounds(minLeft,minBottom,maxRight,maxTop);
		var zoomOffset = (minLeft == maxRight) ? 0 : 1;
		map.setCenter(bounds.getCenterLonLat(),map.getZoomForExtent(bounds)-zoomOffset);
	}
}


