(function ($) {
var self = $.sola = $.extend({
//=============================//
	
	newsXML:           'news/news.xml',
	worksXML:          'works/works.xml',
	
//=============================//
	
	init: function () {
		self.rollover();
		self.load();
		self.events();
	},
	
	load: function () {
		$.ajax({ url: self.worksXML, type: 'GET', dataType: 'xml', cache: false, success: function (r) {
			self._works = $(r).find('item').map(function (i, item) {
				item = $(item);
				return {
					id:        item.attr('id'),
					date:      item.find('date').text(),
					title:     item.find('title').text(),
					type:      item.find('type').text(),
					thumbnail: item.find('thumbnail').attr('src'),
					images:    item.find('images img').map(function (i, img) { return $(img).attr('src'); })
				};
			});
			
			self.works();
		}});
		
		$.ajax({ url: self.newsXML, type: 'GET', dataType: 'xml', cache: false, success: function (r) {
			self._news = $(r).find('item').map(function (i, item) {
				item = $(item);
				return {
					date:      item.find('date').text(),
					title:     item.find('title').text(),
					text:      item.find('text').text(),
					thumbnail: item.find('thumbnail').attr('src'),
					links:     item.find('links link').map(function (i, link) {
						return {
							id:   $(link).attr('id'),
							url:  $(link).attr('url'),
							text: $(link).text()
						};
					})
				};
			});
			self.news();
		}});
	},
	
	events: function () {
		$('#nav li:eq(0) a').click(function () {
			$('div.content:visible:not(li div.content)').fadeOut(1000, function () {
				$('#news').fadeIn(1000);
			});
			
			return false;
		});
		
		$('#nav li:eq(1) a').click(function () {
			if ($('div#works:visible').length) {
				self.flip();
				return false;
			}
			
			$('div.content:visible:not(li div.content)').fadeOut(1000, function () {
				$('#works').fadeIn(1000, function () {
					self.flip();
				});
			});
			
			return false;
		});
		
		$('#nav li:eq(2) a').click(function () {
			$('div.content:visible:not(li div.content)').fadeOut(1000, function () {
				$('#about').fadeIn(1000);
			});
			
			return false;
		});
	},
	
	works: function () {
		var li = [];
		$.each(self._works, function (i, _work) {
			li.push([
				'<li>',
				'<img src="shared/img/thumbnail.gif" width="114" height="114" alt="" />',
				'<img id="jacket_' + _work.id + '" class="jacket" src="works/img/' + _work.thumbnail + '" width="114" height="114" alt="' + _work.title + '" style="display:none" />',
				'</li>'
			].join(''));
		});
		
		$('#works ul').append(li.join('')).vgrid({
			easeing: "easeOutCirc",
			delay:             50
		});
		
		$('#works ul li img.jacket').click(function () {
			self.popup($(this).attr('id').replace(/jacket_/, ''), true);
			return false;
		});
	},
	
	news: function () {
		var li = [];
		$.each(self._news, function (i, _news) {
			li.push([
				'<li' + (i >= 30 ? ' style="display: none;"' : '') + '>',
				'<img class="thumbnail" src="news/img/' + _news.thumbnail + '" width="114" height="114" alt="" />',
				'<strong>' + _news.date + '</strong>',
				'<div class="content">',
				'<h2>' + _news.title + '</h2>',
				'<p>' + self.character(_news.text) + '</p>',
				'<div class="link">',
				(function () {
					return $.map(_news.links, function (link, j) {
						return '<a id="' + (link.id ? ('works_' + link.id) : '') + '" class="' + (link.id ? 'popup' : '') + '" href="' + link.url + '" target="_blank">' + link.text + '</a>';
					}).join('');
				})(),
				'</div>',
				'</div>',
				'</li>'
			].join(''));
		});
		
		$('#news ul').append(li.join(''));
		
		if (self._news.length)
			$('#news div.more').show();
		
		$('#news ul .popup').click(function () {
			self.popup($(this).attr('id').replace(/works_/, ''), false);
			return false;
		});
		
		$('#news div.more').click(function () {
			var count = $('#news ul li:visible').length + 30;
			var max = $('#news ul li').length;
			
			$('#news ul li:lt(' + count + ')').fadeIn(1000);
			
			if (count > max)
				$('#news div.more').hide();
			
			return false;
		});
	},
	
	flip: function () {
		var i = 0;
		
		if (!self.didFlip)
			(function () {
				$('#works ul li:eq(' + (i++) + ')').find('img:last:hidden').fadeIn(500);
				if (i < self._works.length)
					setTimeout(arguments.callee, 75);
			})();
		
		self.didFlip = true;
	},
	
	popup: function (id, from) {
		var work = self._works[self._works.length - id];
		var worksWindow = $('#worksWindow');
		var table = worksWindow.find('table').empty().hide();
		var preLoad = new Object();
		
		var img = new Image();
		img.src = 'works/img/' + work.images[0];
		
		var html = [
			'<tr><th colspan="3">&nbsp;</th></tr>',
			'<tr>',
			'<td class="prev"><a href="#"><img src="shared/img/works_btn_prev.gif" width="56" height="12" alt="previous" class="ro" /></a></td>',
			'<td class="img"><img src="' + img.src + '" /></td>',
			'<td class="next"><a href="#"><img src="shared/img/works_btn_next.gif" width="33" height="12" alt="next" class="ro" /></a></td>',
			'</tr>',
			'<tr>',
			'<td colspan="3" class="caption">',
			'<div class="wrap">',
			'<div class="title">',
			'<h2><img src="shared/img/works_hl_title.gif" width="24" height="11" alt="title" /></h2>',
			'<p>' + self.character(work.title) + '</p>',
			'</div>',
			'<div class="type">',
			'<h2><img src="shared/img/works_hl_type.gif" width="68" height="11" alt="project type" /></h2>',
			'<p>' + self.character(work.type) + '</p>',
			'</div>',
			'<div class="date">',
			'<h2><img src="shared/img/works_hl_date.gif" width="26" height="11" alt="date" /></h2>',
			'<p>' + work.date + '</p>',
			'</div>',
			'<ul class="thumbnails">',
			(function () {
				var li = [];
				for (var i = 0; i < 5; i++) {
					var flug = (i < work.images.length);
					li.push([
						'<li id="thumb_' + i + '" class="thumb' + (flug ? '' : ' dummy') + '">',
						flug ? '<a href="#">' : '',
						flug ? ('<img class="' + (i == 0 ? 'current' : '') + '" src="shared/img/works_thumbnail_current.gif" width="30" height="30" alt="" />') : '',
						'<img src="' + (flug ? ('works/img/' + work.images[i].replace(/(\..*$)/, '_s$1')) : 'shared/img/thumbnail_s.jpg') + '" />',
						flug ? '</a>' : '',
						'</li>'
					].join(''));
					
					if (flug) {
						var imgSrc = work.images[i];
						preLoad[i] = new Image();
						preLoad[i].src = 'works/img/' + imgSrc;
					}
				}
				return li.join('');
			})(),
			'</ul>',
			'</div>',
			'</td>',
			'</tr>',
			'<tr><th colspan="3">&nbsp;</th></tr>'
		].join('');
		
		table.append(html).fadeIn(1000);
		$('td.prev a, td.next a')[from ? 'show' : 'hide']();
		
		worksWindow.fadeIn(1000);
		$('#worksWindowBase, #worksWindow > table th').unbind().click(function () {
			worksWindow.fadeOut(1000);
		});
		
		worksWindow.find('td.img img').unbind().load(function () {
			var img = new Image();
			img.src = $(this).attr('src');
			$(this).css({height: img.height});
		});
		
		worksWindow.find('li.thumb a').unbind().click(function () {
			var img = preLoad[$(this).parent().attr('id').replace(/thumb_/, '')];
			worksWindow.find('td.img img').attr('src', img.src);
			return false;
		});
		
		self.change(id);
	},
	
	change: function (id) {
		var thumbs      = $('#worksWindow ul.thumbnails li a');
		var max         = self._works.length;
		var min         = 1;
		var now         = 0;
		
		$('#worksWindow td.prev a, #worksWindow td.next a').unbind().click(function () {
			switch ($(this).parent().attr('class')) {
				case 'prev':  if (++id > max) id = min; break;
				case 'next':  if (--id < min) id = max; break;
			}
			
			self.popup(id, true);
			
			return false;
		});
	},
	
	rollover: function () {
		var preLoad = new Object();
		$('img.ro,input.ro,#side ul li a img').not("[src*='_ro.']").each(function(){
			var imgSrc = this.src;
			var fType = imgSrc.substring(imgSrc.lastIndexOf('.'));
			var imgName = imgSrc.substr(0, imgSrc.lastIndexOf('.'));
			var imgOver = imgName + '_ro' + fType;
			preLoad[this.src] = new Image();
			preLoad[this.src].src = imgOver;
			$(this).hover(
				function (){ this.src = imgOver; },
				function (){ this.src = imgSrc;  }
			);
		});
	},
	
  location: function (url) {
    url = url || location.href;
    return (function (url, scheme, host, port, path, query, hash) {
      return {
        scheme: scheme,
        host:   host,
        port:   port,
        path:   path,
        query:  query,
        hash:   hash
      };
    }).apply(null, url.match(
      /^(https?):\/\/([^\/:]+):?([^\/]*)([^\?#]+)\??([^#]*)#?(.*)$/
    ));
  },
  
  character: function (str) {
  	return str
  		.replace(/{b}/g,  '<br />')
	  	.replace(/{r}/g,  '&reg;')
			.replace(/{c}/g,  '&copy;')
			.replace(/{tm}/g, '&trade;');
  },
  
	checkCookie: function () {
		if ($.cookie('opening') == null) {
			$.cookie('opening','opc',{expires:1,path:'/'});
			self.opening();
		}
		else {
			$('#opening').hide();
			$('body').css('overflow-y', 'auto');
			self.init();
		}
		
	},
	
	opening: function () {
		$('#opening img').fadeIn(7000,function () {
			self.init();
			$('#opening').fadeOut(1000, function() {
				$('body').css('overflow-y', 'auto');
			});
		});
	}
	
});

$(function () {
	$.sola.checkCookie();
});
})(jQuery);

