// Copyright (c) WorldServe Internet Services
// All Rights Reserved
// For more information, see http://WorldServe.com/copyright
//

WorldServe(function(Y, W)
{
    function changeStyle(event)
    {
	var style = this.get('id');
	setStyle(style);

	Y.Cookie.set('style', style);

	this.enhancedStatus();
    }

    function setStyle(style)
    {
	var styleID = style + 'Style';

	Y.all('link.menuStyle').each(function(node)
	{
	    var stylesheet = Y.StyleSheet(node);
	    if (node.get('id') == styleID)
	    {
		stylesheet.enable();
	    }
	    else
	    {
		stylesheet.disable();
	    }
	});
    }

    function nextMap(event)
    {
	imageMap(this.getAttribute('nextID'));
	this.enhancedStatus();

	event.preventDefault();
	//event.stopPropagation();
    }

    function imageMap(map)
    {
	var mapsDiv = Y.one('#maps').set('className', map);
    }

    function previewMouseOver(event)
    {
	if (this.hasAttribute('previewID'))
	{
	    var previewID = this.getAttribute('previewID');
	}
	else
	{
	    var photograph = this.get('id').replace(/^.*photoArea(\d+).*$/, "photograph$1");
	    this.setAttribute('photograph', photograph);

	    var previewID = '#' + photograph;
	    this.setAttribute('previewID', '#' + photograph);
	}

	Y.all('.preview').removeClass('preview');	// clear any existing preview
	Y.one(previewID).addClass('preview');

	return true;
    }

    function previewMouseOut(event)
    {
	if (this.hasAttribute('previewID'))
	{
	    var previewID = this.getAttribute('previewID');
	    Y.one(previewID).removeClass('preview');
	
	    return true;
	}

	return false;
    }

    var articleWindow = null;

    //todo: generalized method in WorldServe.js
    function article(event)
    {
	var windowURI = this.get('href');

	if (is_null(articleWindow) || articleWindow.closed)
	{
	    var width = screen.width;
	    var height = screen.height;
	    var dimensions = 'fullscreen=yes, maximized=yes, width=' + width + ',height=' + height +  ',left=0,top=0,';
	    var windowString = 'toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,status=no';
	    articleWindow = window.open(windowURI, 'article', dimensions + windowString);

	    if (is_null(articleWindow))
	    {
		// popup blocked
		return false;
	    }
	}
	else
	{
	    articleWindow.location.replace(windowURI);
	}

	articleWindow.focus();

	event.preventDefault();
	//event.stopPropagation();

	return true;
    }

    function selectAlbum(event)
    {
	if (this.hasAttribute('album'))
	{
	    var album = this.getAttribute('album');
	}
	else
	{
	    return true;
	}

	var albums = Y.one('#albums');
	albums.setAttribute('className', album);
	
	event.halt();
	return false;
    }

    var photographWindow = null;

    //todo: generalized method in WorldServe.js
    function photograph(event)
    {
	var windowURI = this.get('href');

	if (is_null(photographWindow) || photographWindow.closed)
	{
	    var windowString = 'toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,status=no,chrome=yes,centerscreen';
	    photographWindow = window.open(windowURI, 'photograph', windowString);

	    if (is_null(photographWindow))
	    {
		// popup blocked
		return false;
	    }
	}
	else
	{
	    photographWindow.location.replace(windowURI);
	}

	photographWindow.focus();

	event.preventDefault();
	//event.stopPropagation();

	return true;
    }

    function resizeToFit()
    {
	// push it back to work behind the scenes
	//window.blur();

	//resize to width
	var photograph = document.getElementById('photograph');
	var width = photograph.width + 35;
	var height = document.body.scrollHeight + 80;
	window.resizeTo(width, height);

	// center window
	var left = Math.round((screen.width - width) / 2);
	var top = Math.round((screen.height - height) / 2);
	window.moveTo(left, top);

	// bring it forward to be seen
	window.focus();
    }

    Y.on('domready', function()
    {
	Y.all('a.style').each(function(node)
	{
	    node.set('href',	'#');
	    node.on('click',	changeStyle);
	});

	Y.all('a.article,area.article').each(function(node)
	{
	    node.on('click', article);			// create the article window
	    node.setAttribute('target', 'article');	// and open the link into it
	});

	Y.all('a.album').each(function(node)
	{
	    var album = node.get('href').replace(/.*=/g, '');
	    node.setAttribute('album', album);

	    node.on('click',	selectAlbum);
	    node.setAttribute('href', '#' + album);
	});

	Y.all('area.next').each(function(node)
	{
	    //todo: move calculation to handler function
	    node.setAttribute('nextID', node.get('href').replace(/^.*=/g, ''));

	    node.on('click',	nextMap);
	});

	Y.all('a.photograph').each(function(node)
	{
	    node.on('click', photograph);	// create the photograph window
	    node.setAttribute('target', 'photograph');			// and open the link into it
	});

	Y.all('area.photograph').each(function(node)
	{
	    node.on('mouseover',previewMouseOver);
	    node.on('mouseout',	previewMouseOut);

	    //also for focus/blur (tabbing through imagemaps)
	    node.on('focus',	previewMouseOver);
	    node.on('blur',	previewMouseOut);

	    node.on('click', photograph);	// create the photograph window
	    node.setAttribute('target', 'photograph');			// and open the link into it
	});

	Y.all('body.photograph').each(function(node)
	{
	    node.one('#photograph').on('load', resizeToFit);
	});
    });
});
