var cc = new CookieHandler();
var loader;

$(document).keyup(function(e) {
  if (e.keyCode == 27) { closePop(); };
});

$(function(){
	// transition effect
	style = 'easeOutQuart';	 
	
	$("#logo img").hover(
       function() {
          $(this).stop(true, false).fadeTo('slow', 0);
       }, function() {
          $(this).stop(true, false).fadeTo('slow', 1.0);
       }
    );
	
	$(".box").hover(
       function() {
          $(this).children().stop(true, false).fadeTo('fast', 1.0);
		  $(this).children().children('div:last').stop(false,true).animate({bottom:0},{duration:1, ease: style});
       }, function() {
          $(this).children().stop(true, false).fadeTo('medium', 0.25);
		  $(this).children().children('div:last').stop(false,true).animate({bottom:-80},{duration:100, ease: style});
       }
    );	
	
	 $("#nav li").hover(
       function() {
          $(this).stop(true, false).fadeTo('fast', 0.7);
       }, function() {
          $(this).stop(true, false).fadeTo('slow', 1.0);
       }
    );


	$('.detail').click(function(e){
		$('.boxOut', this).before('<div class="loader" style="position:absolute; top:0; left:0;"><img src="images/loader.gif"></div>');	
		loader = $('.loader', this);
		$('#popup').load('include/detail.php?id='+this.id, function() {
			$('#popup').fadeIn();
			$('#popup').height($('body').height());
		});
	}); 	
	$('.showpage').click(function(){
		$('#popup').load('include/page.php?id='+this.id, function() {
			$('#popup').fadeIn();
			$('#popup').height($('body').height());
			if(navigator.appName == "Microsoft Internet Explorer"){
				$('#contentPop').append('<div class="TL"></div>');
				$('#contentPop').append('<div class="TR"></div>');
				$('#contentPop').append('<div class="BL"></div>');
				$('#contentPop').append('<div class="BR"></div>');
			}
		});
	}); 
	if(navigator.appName == "Microsoft Internet Explorer"){
		$('#container').append('<div class="TL"></div>');
		$('#container').append('<div class="TR"></div>');
		$('#container').append('<div class="BL"></div>');
		$('#container').append('<div class="BR"></div>');
		
		$('#container').append('<div class="TM"></div>');
		$('#container').append('<div class="BM"></div>');
		$('#container').append('<div class="LM"></div>');
		$('#container').append('<div class="RM"></div>');
	}
	
	//if(!cc.getCookie('welcome')) welcome();
	
	//$(".detail1").pixastic("desaturate");

});	
function welcome(){
	cc.setCookie('welcome', 1, 600000);
	$('#popup').load('include/page.php?id=', function() {
		$('#popup').fadeIn();
		$('#popup').height($('body').height());
		if(navigator.appName == "Microsoft Internet Explorer"){
			$('#contentPop').append('<div class="TL"></div>');
			$('#contentPop').append('<div class="TR"></div>');
			$('#contentPop').append('<div class="BL"></div>');
			$('#contentPop').append('<div class="BR"></div>');
		}
	});	
}

function closePop(){
	if(loader) loader.remove();
	$('#popup').fadeOut();
	$('#popup').html('');
}

function over(img) {
	id = $(img).children().children().attr('id');
	//Pixastic.revert(document.getElementById(id));
}
function out(id) {
	img = $(id).children().children().attr('id');
	//$("#"+img).pixastic("desaturate");
}

function CookieHandler() {

	this.setCookie = function (name, value, seconds) {
 
		if (typeof(seconds) != 'undefined') {
			var date = new Date();
			date.setTime(date.getTime() + (seconds*1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else {
			var expires = "";
		}
 
		document.cookie = name+"="+value+expires+"; path=/";
		
	}
 
	this.getCookie = function (name) {
 
		name = name + "=";
		var carray = document.cookie.split(';');
 
		for(var i=0;i < carray.length;i++) {
			var c = carray[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
		}
 
		return null;
	}
 
	this.deleteCookie = function (name) {
		this.setCookie(name, "", -1);
	}
 
}
var val;
$.address.init(function(event) {
	
	$('#filters a').address(function() {
		return $(this).attr('href').replace(location.pathname, '');
	});
}).change(function(event) {
	
	var text = (event.value == '') ? 'all' : 
		event.pathNames[0].substr(0, 1).toUpperCase() + 
		event.pathNames[0].substr(1);
	$('a').each(function() {
		//$(this).toggleClass('selected', $(this).text() == text);
		$('#options').find('#filters.option-set a').click(function(){
			var $this = $(this);
			if ( !$this.hasClass('selected') ) {
				$this.parents('.option-set').find('.selected').removeClass('selected');
				$this.addClass('selected');
			}
		});
	});
}).internalChange(function(event) {
	//alert(event.value);
}).bind('externalChange', {msg: '{value}'}, function(event) {
	val = event.data.msg.replace(/\{value\}/, event.value).toString();
	val = val.replace('/','');
	$('#'+val).trigger('click');
});












