window.addEvent('domready', function() {
    loadXML();
    loadAccordion();
    setToolTips();

    $('feat_select').addEvent('mousedown', featDropdownToggle);


    $$('ul.webnav').addEvent('mouseenter', webnavEnter = function() {
        this.setStyle('background-position', '0 -35px');
    });

    $$('ul.webnav').addEvent('mouseleave', webnavLeave = function() {
        this.setStyle('background-position', '0px 0px');
    });

    $$('ul.webnav').addEvent('mousedown', webnavDown = function() {
        this.setStyle('background-position', '0px -70px');
    });

    $$('a.btn_comment').addEvent('click', showComment);

    contactItems = new Array($('name'), $('email'), $('msg'));
    contactItems.each(function(item) {
        item.addEvent('focus', function() {
            item.addClass('focus');
        });
        item.addEvent('blur', function() {
            item.removeClass('focus');
        });
    });
});

var xmlDoc;

function loadXML() {
  
    url="/xml/data.xml";
	if (window.XMLHttpRequest) {
        xmlDoc = new XMLHttpRequest();
        xmlDoc.onreadystatechange = processReqChange;
        xmlDoc.open("GET", url, true);
        xmlDoc.send(null);
    
    } else if (window.ActiveXObject) {
        isIE = true;
        xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlDoc) {
            xmlDoc.onreadystatechange = processReqChange;
            xmlDoc.open("GET", url, true);
            xmlDoc.send();
        }
    }
}

function processReqChange() {
   
    if (xmlDoc.readyState == 4) {
      
        if (xmlDoc.status == 200) {
            readXML();
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlDoc.statusText);
         }
    }
}


function readXML()
{
	allImg = new Array;
	$$('div.web_imgs').each(function(item, index)
	{
		allImg [index] = new Array;
		allImg [index] = xmlDoc.responseXML.getElementsByTagName("images")[index].childNodes[0].nodeValue.split(',');
		loadSlideShow(item.getProperty('id'),allImg[index]);
	});
	addBtnAction($$('ul.contact_form input.btn'), '/images/light/btn_send.gif', '0', '-26px',  '-52px');	
	addBtnAction($$('div.web_footer a.btn_comment'), '/images/light/btn_comment.gif', '0', '-16px',  '-32px');	
	addBtnAction($('feat_select'), '/images/light/dd_select.gif', '0', '-24px',  '-48px');	
	setTheme();
	$$('p.web_comment').setStyle('opacity','0.95');
}


function loadSlideShow(name, imgs) {
	myShow = new Slideshow(name, {
		transition: Fx.Transitions.backInOut, 				   
		duration: [200],
		width:470,
		height:270,
		navigation: 'arrows+', 
		hu: 'work/', 
		images: imgs,
		resize:false
	});
}

function loadAccordion() {
	var CookieAutoAccordion = Accordion.extend({
														   
	  initialize: function(togglers, elements, options) {
		this.options.cookieName = 'accordion-place';
		this.options.cookieOptions = {path: '/', duration: 30};	
		this.setOptions(options);
		this.options.allowMultipleOpen = false;
		this.options.opacity = false;
		this.options.openAll = false;
		this.options.wait = false;
		var cookieValue = Cookie.get(this.options.cookieName);
			
		if (cookieValue != false) {
		  this.options.show = cookieValue.toInt();
		  this.addEvent('onActive', function(toggler, element) {
				toggler.addClass('webnav_active');
			});							 
		} else {
		  this.addEvent('onActive', function(toggler, element) {
				toggler.addClass('webnav_active');
			});
		}

		this.parent.apply(this, arguments);
		this.addEvent('onActive', function(toggler, element) {
		  Cookie.set(this.options.cookieName, this.togglers.indexOf(toggler), this.options.cookieOptions);
		  toggler.addClass('webnav_active');
		});
		this.addEvent('onBackground', function(toggler, element) {
			$$('p.web_comment').addClass('hide');
			if (toggler.hasClass('webnav_active')) {
				toggler.removeClass('webnav_active');
			}
		});
	  }
	});
			
	var accordion = new CookieAutoAccordion($$('ul.webnav'), $$('div.web_content'));	
		
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  var styleCookieOptions = {path: '/', duration: 1};
  Cookie.set('style', title, styleCookieOptions);
}

function setForm() {
	$('contactform').addEvent('submit', function(e) {
		new Event(e).stop();
		var log = $('contact_log').empty().addClass('contact_loading');
		this.send({
			update: log,
			evalScripts:true,
			onComplete: function() {
				log.removeClass('contact_loading');
			}
		});
	});	
}
function resetForm() {
	$('name').setProperty('value','');
	$('email').setProperty('value','');
	$('msg').setProperty('value','');
}




function themeBtnOver() {
	this.setStyle('background-position','0 -19px');
}

function themeBtnOut() {
	this.setStyle('background-position','0 0');
}

function themeBtnDown() {
	this.setStyle('background-position','0 -38px');
}

function setToolTips() {
	var tip_dark = new Tips($$('a.style_dark img'), {
		showDelay: 200,
		hideDelay: 100,
		className: 'tt_night',
		offsets: {'x': 2, 'y': -17},
		fixed: true
	});	
	
	var tip_light = new Tips($$('a.style_light img'), {
		showDelay: 200,
		hideDelay: 100,
		className: 'tt_day',
		offsets: {'x': 2, 'y': -17},
		fixed: true
	});	
		var tip_light = new Tips($$('a.style_sunrise img'), {
		showDelay: 200,
		hideDelay: 100,
		className: 'tt_sunrise',
		offsets: {'x': 2, 'y': -17},
		fixed: true
	});	

	var tip_desc = new Tips($$('a.btn_comment'), {
		showDelay: 200,
		hideDelay: 100,
		className: 'tt_desc',
		offsets: {'x': 1, 'y': -17},
		fixed: true
	});	

}

function showComment() {
	objElement = $$('p.web_comment');
	objElement.toggleClass('hide')
};

function addBtnAction(id, url,  mouseleave, mouseenter, mousedown) {
	
	id.setStyle('background-image', 'url('+url+')');
	
	id.addEvent('mouseenter', function()
	{
		id.setStyle('background-position','0 '+mouseenter);
	});
	
	id.addEvent('mouseleave', function()
	{
		id.setStyle('background-position','0 '+mouseleave);
	});
	
	id.addEvent('mousedown', function()
	{
		id.setStyle('background-position','0 '+mousedown);
	});
	
	id.addEvent('mouseup', function()
	{
		id.setStyle('background-position','0 '+mouseenter);
	});
}


function featDropdownToggle() {
	
	objElement = $('feat_options');
	
	if(!objElement.Fx)
	{
		objElement.Fx = new Fx.Style(objElement, 'height', {duration:500, transition:Fx.Transitions.quintInOut});
	}
	
	if('0px' == objElement.getStyle('height'))
	{
		objElement.Fx.start(450);
	}
	else
	{
		objElement.Fx.start(0);
	}
}


