window.addEvent('domready', function() 
{
	setForm();
	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/data3.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] = xmlDoc.responseXML.getElementsByTagName("images")[index].childNodes[0].nodeValue.split(',');
		url = xmlDoc.responseXML.getElementsByTagName("url")[index].childNodes[0].nodeValue;
		//alert(xmlDoc.responseXML.getElementsByTagName("url")[index].childNodes[0].nodeValue);
		loadSlideShow(item.getProperty('id'),allImg[index],url);
	});
	addBtnAction($$('ul.contact_form input.btn'), 'images/dark/btn_send.gif', '0', '-26px',  '-52px');	
	addBtnAction($$('div.web_footer a.btn_comment'), 'images/dark/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, url) {
	myShow = new Slideshow(name, {
		transition: Fx.Transitions.backInOut, 				   
		duration: [200],
		width:470,
		height:270,
		navigation: 'arrows+', 
		hu: 'print/', 
		images: imgs,
		url:url,
		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 setTheme(){
	var clock_time = new Date();
	var clock_hours = clock_time.getHours();
	var clock_minutes = clock_time.getMinutes();
	var clock_seconds = clock_time.getSeconds();
	
	var styleCookieValue = Cookie.get("style");
	
	if (styleCookieValue != false) {
		setActiveStyleSheet(styleCookieValue);
	} else {
		if (clock_hours > 5 && clock_hours < 18) 
		{
			setActiveStyleSheet('day');
		} else {
			setActiveStyleSheet('night');
		}
	}
	setClock();
}

function setActiveStyleSheet(title) {
	var i, a, main;
	if	(title == 'night') {
		loadStyle('dark');
	} else if (title == 'day') {
		loadStyle('light');	
	} else {
		loadStyle('sunrise');
	}	
	
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function loadStyle(theme) {
	switch(theme) 
	{
		case 'light':
		{
			
			$$('a.style_light').setStyle('background-position','0 -38px');
			$$('a.style_dark').setStyle('background-position','0 0');
			$$('a.style_sunrise').setStyle('background-position','0 0');
			
			$$('a.style_dark').addEvent('mouseenter', themeBtnOver);
			$$('a.style_dark').addEvent('mouseleave', themeBtnOut);
			$$('a.style_dark').addEvent('mousedown', themeBtnDown);
			
			$$('a.style_sunrise').addEvent('mouseenter', themeBtnOver);
			$$('a.style_sunrise').addEvent('mouseleave', themeBtnOut);
			$$('a.style_sunrise').addEvent('mousedown', themeBtnDown);

			$$('div.themeselector p').setHTML('<strong>Current theme: </strong><span>Day</span><br />It will change to the night theme at 6PM.');
			$$('p.web_header img').setProperty('src','images/light/link_arrow_green.gif');
			
			break;
		}
		case 'dark':
		{
		
			$$('a.style_dark').setStyle('background-position','0 -38px');
			$$('a.style_light').setStyle('background-position','0 0');
			$$('a.style_sunrise').setStyle('background-position','0 0');
			
			$$('a.style_light').addEvent('mouseenter', themeBtnOver);
			$$('a.style_light').addEvent('mouseleave', themeBtnOut);
			$$('a.style_light').addEvent('mousedown', themeBtnDown);
			
			$$('a.style_sunrise').addEvent('mouseenter', themeBtnOver);
			$$('a.style_sunrise').addEvent('mouseleave', themeBtnOut);
			$$('a.style_sunrise').addEvent('mousedown', themeBtnDown);
			
			$$('div.themeselector p').setHTML('<strong>Current theme: </strong><span>Night</span><br />It will change to the day theme at 6AM.');
			$$('p.web_header img').setProperty('src','images/dark/link_arrow_brown.gif');
			break;
		}
		case 'sunrise':
		{
		
			$$('a.style_dark').setStyle('background-position','0 0');
			$$('a.style_light').setStyle('background-position','0 0');
			$$('a.style_sunrise').setStyle('background-position','0 -38px');
			
			$$('a.style_dark').addEvent('mouseenter', themeBtnOver);
			$$('a.style_dark').addEvent('mouseleave', themeBtnOut);
			$$('a.style_dark').addEvent('mousedown', themeBtnDown);
			
			$$('a.style_light').addEvent('mouseenter', themeBtnOver);
			$$('a.style_light').addEvent('mouseleave', themeBtnOut);
			$$('a.style_light').addEvent('mousedown', themeBtnDown);
		
			$$('div.themeselector p').setHTML('<strong>Current theme: </strong><span>Sunrise</span><br />It will change to the day theme at 6AM.');
			$$('p.web_header img').setProperty('src','images/sunrise/link_arrow_brown.gif');
			break;
		}
	}
	
	$$('a.style_'+theme).removeEvent('mouseenter', themeBtnOver);
	$$('a.style_'+theme).removeEvent('mouseleave', themeBtnOut);
	$$('a.style_'+theme).removeEvent('mousedown', themeBtnDown);
	
		
	$('feat_select').setStyle('background-image','url(images/'+theme+'/dd_select.gif)');
	$$('ul.contact_form input.btn').setStyle('background-image','url(images/'+theme+'/btn_send.gif)');
	$$('div.web_footer a.btn_comment').setStyle('background-image','url(images/'+theme+'/btn_comment.gif)');
	$$('div.web_footer a.btn_comment').setStyle('background-image','url(images/'+theme+'/btn_comment.gif)');
	$$('a.prev').setStyle('background-image','url(images/'+theme+'/btn_prev.gif)');
	$$('a.next').setStyle('background-image','url(images/'+theme+'/btn_next.gif)');
}

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 setClock() {
	var clock_time = new Date();
	var clock_hours = clock_time.getHours();
	var clock_minutes = clock_time.getMinutes();
	var clock_seconds = clock_time.getSeconds();
	var clock_suffix = "AM";
	if (clock_hours > 11)
	{
		clock_suffix = "PM";
		clock_hours = clock_hours - 12;
	}
	
	if (clock_hours == 0)
		{clock_hours = 12;}
	if (clock_hours < 10)
		{clock_hours = "0" + clock_hours;}
	if (clock_minutes < 10)
		{clock_minutes = "0" + clock_minutes;}
	if (clock_seconds < 10)
		{clock_seconds = "0" + clock_seconds;}
	
	$$('li.time strong').setHTML(clock_hours + ":" + clock_minutes + " " + clock_suffix);
	setTimeout("setClock()", 60000);
}


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);
	}
}


