
var z_store = new function()
    {
	this.i = 10;
	var self = this;

	this.getNewIndex = function()
	{
	    self.i += 1;
	    return self.i;
	};

	this.fade = function(d)
	{
	    var id = d.attr('id').split('_')[1];
	    $('#txt_'+id).addClass('current');
	    d.addClass('current');

	    	    $('#txt_container p:not(.current)').fadeTo('fast', 0);
	    $('#grid img:not(.current)').fadeTo('fast', 0.25);

	    $('#txt_container .current').fadeTo('fast', 1);
	};
    };




var toggle_archive = function(link)
{
    var item = link.parent();
    var list = item.find('> ul');
    if(list.css('display') == 'none')
	{
	    list.show(200);
	    //item.css('list-style', 'circle');
	}
    else
	{	  
	    list.hide(200);      	  
	    //item.css('list-style', 'disc');
	}
};



var archive = function()
{
    $('#archive a[href^="#"]').bind('click', function(e){
	    e.preventDefault();
	    var $this = $(this);
	    toggle_archive($this);
	});
};



var enlarger = new function()
    {     
	var current_image;
	var interval;
	var enlarged;
	var fade;
	var active;
	var self = this;
	
	
	this.resize = function()
	{
	    if(self.active == true)
		{
		    var width = self.current_image.width;
		    var height = self.current_image.height;
		    self.enlarged.css({
			    'left': ($('body').width() - width) / 2,
				'top': ($('body').height() - height) / 2});				
		    //  $('.enlarger_fade').css({'height': '100%', 'width': '100%'});
		}
	};
	
	this.load_image_complete = function()
	{
	    self.active = true;    
	    $('body').css({'overflow':'hidden'});
	    self.resize();
	    self.fade.css({'display':'block'});
	    self.fade.fadeTo("fast", 0.6);
	    self.enlarged.attr('src', self.current_image.src).fadeIn("fast");				
	};
	
	this.load_image = function()
	{
	    self.interval = setInterval("enlarger.watch_load()", 100);
	};
	
	this.watch_load = function()
	{
	    if(self.current_image.complete)
		{		
		    clearInterval(self.interval);
		    self.load_image_complete();
		}	    
	};

	
	this.init = function()
	{
	    self.active = false;
	    self.enlarged = $('<img/>')
	    .addClass('enlarged').hide().appendTo('body');

	    self.fade = $('<div/>')
	    .addClass('enlarger_fade').appendTo('body')
	    .css({'left': 0, 'top': 0, 'display':'none'}).fadeTo("fast", 0);



	    $('#ads img, .entry img').bind('click', function(e){
		    e.preventDefault();
		    $this = $(this);
		    var src = $this.attr('src');
		    //		    src = src.replace(/tn_/, '');
		    src = src.replace(/mid_/, '');

		    self.current_image = new Image();
		    self.current_image.src = src;
		    self.load_image();		    		  
		});

	    $('.enlarged').bind('click', function(e){
		    $this = $(this);
		    self.fade.fadeOut("fast");
		    $this.fadeOut("fast");
		    self.active = false;
		    $('body').css({'overflow':'auto'});
		});
	};
    };





$(document).ready(function(){

	var snap = 20;

	$('a.confirm').bind("click", function(e){
		return confirm('Are you sure you want to do this?');
	    });
	
	$('form.confirm').submit(function(){
		return confirm('Are you sure you want to do this?');
	    });
	
	
	$('a.back').bind("click", function(e){
		e.preventDefault();
		history.go(-1);
	    });


	if($('#blog_wrapper').length < 1 && ($('.entry').length > 0 || $('#ads').length > 0))
	    {
		enlarger.init();
		$(window).resize(function(){ enlarger.resize(); });
	    }


	if($('#archive').length > 0)
            {
		archive();
	    }


	/*
	$('.handle').css('cursor', 'move');
	$('.outer').bind('dragstart',function( event ){
                return $(event.target).is('.handle');
	    })
	.bind('drag',function( event ){
		    $( this ).css({
	    top: Math.round( event.offsetY/snap ) * snap,
				left: Math.round( event.offsetX/snap ) * snap,
				zIndex: z_store.getNewIndex()	
				})});
	*/


	$('#news_item img[id^=blog_image]').css('cursor', 'pointer');

    });


$(window).load(function(){


	/*
	$('#front').each(function(){		
		var $this = $(this);
		var w = $this.width();
		var h = $this.height();
		$this.css('marginTop', (h/2)*-1)
		    .css('marginLeft', (w/2)*-1)
		    .fadeIn(5000);
	    });
	*/	
    });