/*global YAHOO */
var Y, $;
Y = YAHOO.util;
$ = Y.Dom.get;


YAHOO.util.Event.onContentReady('map', function() {
	var el, link, link2, show, hide;
		
	el = $('map_overlay');
	link = $('show-map');
	link2 = $('hide-map');
	
	show = new Y.Anim(el, { opacity: { to: 0 } }, 0.5);
	hide = new Y.Anim(el, { opacity: { to: 1 } }, 0.5);
	
	Y.Event.on(['show-map', 'hide-map'], 'click', function() {
		if (show.isAnimated() || hide.isAnimated()) {
			return;
		}
		
		if (Y.Dom.getStyle(el, 'opacity') == '1')
		{
			link.style.display = 'none';
			link2.style.display = 'block';
			show.onComplete.subscribe(function () { el.style.display = 'none'; });
			show.animate();
		}
		else
		{
			el.style.display = 'block';
			link.style.display = 'block';
			link2.style.display = 'none';
			hide.animate();
		}
	});
	
});

YAHOO.util.Event.onContentReady('property_details_container', function() {
	var el, link, link2, attributes, show, hide, child, child_padding, duration_show;
		
	el = $('property_details_container');
	link = $('show-details');
	link2 = $('hide-details');
	child = $('property_details');
	child_padding = parseInt(Y.Dom.getStyle(child, 'padding-top'), 10)  + parseInt(Y.Dom.getStyle(child, 'padding-bottom'), 10);
	duration_show = .6;
	if (YAHOO.env.ua.ie > 0) { duration_show = 0; }     // Microsoft Internet Explorer show anim fix
	
	attributes = {
		height: { from: 0, to: parseInt(Y.Dom.getStyle(el, 'height'),10) }
	};
	
	show = new Y.Anim(el, attributes, duration_show, Y.Easing.easeOut);
	hide = new Y.Anim(el, { height: { to: 0 } }, .6, Y.Easing.easeIn);
	hide.onComplete.subscribe( function() { el.style.visibility = 'hidden' } );
	show.onComplete.subscribe(function() { el.style.height = 'auto' });
	
	show.attributes.height.to = parseInt(Y.Dom.getStyle(child, 'height'), 10) +child_padding;
	
	if (Y.Dom.getStyle(el, 'visibility') == 'hidden')
		el.style.height = 0;
	
	Y.Event.on(['show-details', 'hide-details'], 'click', function() {
		if (show.isAnimated() || hide.isAnimated()) {
			return;
		}
		
		if (Y.Dom.getStyle(el, 'visibility') == 'hidden')
		{
			el.style.height = 0;
			el.style.visibility = 'visible';
			link.style.display = 'none';
			link2.style.display = 'block';
			setcookie('property_details','1');
			show.animate();
		}
		else
		{
			link.style.display = 'block';
			link2.style.display = 'none';
			setcookie('property_details','0');
			hide.animate();
		}
	});
	
});


YAHOO.util.Event.onContentReady('works', function() {
	var table, animations;
	table = $('works');
	animations = {};

	function cancel_animation(id)
	{
		if(animations[id])
		{
			animations[id][0].stop();
			animations[id][1].stop();
		}

		animations[id] = null;
	}

	function mouse_over(e)
	{
		var el = Y.Event.getTarget(e);
		el = Y.Dom.getAncestorByTagName(el, 'TR');
		if (el && el.id != 'sort_row')
		{
			var links = el.getElementsByTagName('A');
			var fade_link = new Y.ColorAnim(links[1], { color: { to: '#FFFFFF' }}, 0.75, Y.Easing.easeOut);
			var fade = new Y.ColorAnim(el,
				{
					backgroundColor: {  to: '#333333' },
					color: { to: '#FFFFFF' }
				}, 0.75, Y.Easing.easeOut);

			var id = Y.Event.generateId(el);
			cancel_animation(id);
			animations[id] = [fade, fade_link];
			fade_link.onComplete.subscribe(function () { animations[id] = null; } );
			fade.animate();
			fade_link.animate();
		}
	}

	function mouse_out(e)
	{
		var el = Y.Event.getTarget(e);
		el = Y.Dom.getAncestorByTagName(el, 'TR');
		if (el)
		{
			var links = el.getElementsByTagName('A');
			var fadeout_link = new Y.ColorAnim(links[1], { color: { to: '#b5b5b5' }}, 0.75, Y.Easing.easeOut);
			var fadeout = new Y.ColorAnim(el,
				{
					backgroundColor: {  to: '#000000' },
					color: { to: '#b5b5b5' }
				}, 0.75, Y.Easing.easeOut);

			var id = Y.Event.generateId(el);
			cancel_animation(id);
			animations[id] = [fadeout, fadeout_link];
			fadeout_link.onComplete.subscribe(function () { animations[id] = null; });
			fadeout.animate();
			fadeout_link.animate();
		}
	}

	Y.Event.on(table, 'mouseover', mouse_over);
	Y.Event.on(table, 'mouseout', mouse_out);
});


YAHOO.util.Event.onDOMReady(function() {
	function SimpleCrossFade(el,x){
		var bg, div, fade;

		bg = el.getAttribute('oversrc');
		div = Y.Dom.getAncestorByClassName(el, 'crossfade');
		// prevent backround image from being seen on load
		if (div) {
			div.style.backgroundImage = 'url('+bg+')';
		}
		if (x == 'out') {
			fade = new Y.Anim(el, { opacity: { to: 0 } }, 0.5);
		}
		else {
			fade = new Y.Anim(el, { opacity: { to: 1 } }, 0.5);
		}

		fade.animate();
	}


	// This is the implementation of SimpleSwap
	// by Jehiah Czebotar
	// Version 1.1 - June 10, 2005
	// Distributed under Creative Commons
	//
	// Include this script on your page
	// then make image rollovers simple like:
	// <img src="/images/ss_img.gif" oversrc="/images/ss_img_over.gif">
	//
	// http://jehiah.com/archive/simple-swap
	//


	//function SimpleSwap(el,which){
	//  el.src=el.getAttribute(which || "origsrc");
	//}

	function SimpleSwapSetup(){
		var x, i, oversrc;

	  x = document.getElementsByTagName("img");
	  for (i=0;i<x.length;i++){
		oversrc = x[i].getAttribute("oversrc");
		if (!oversrc) {
			continue;
		}

		// preload image
		// comment the next two lines to disable image pre-loading
		x[i].oversrc_img = new Image();
		x[i].oversrc_img.src=oversrc;
		// set event handlers
		x[i].onmouseover = function() { SimpleCrossFade(this,'out'); };
		x[i].onmouseout = function() { SimpleCrossFade(this,'in'); };
		// save original src
		x[i].setAttribute("origsrc",x[i].src);
	  }
	}

	SimpleSwapSetup();
});

function work_swap(src, width, height)
{
	var img = $('main_img');
	img.src = src;
	img.width = width;
	img.height = height;
}

function setcookie(name, value, expires, path, domain, secure) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: setcookie('author_name', 'Kevin van Zonneveld');
    // *     returns 1: true
 
    expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
    var r = [name + "=" + escape(value)], s, i;
    for(i in s = {expires: expires, path: path, domain: domain}){
        s[i] && r.push(i + "=" + s[i]);
    }
    return secure && r.push("secure"), document.cookie = r.join(";"), true;
}
