$(document).ready(function(){
    $('a[href^="http://"]').attr({target: "_blank", title: "Opens in a new window"});
    
    
    var thatchSlideShow = function(what) {
        var me = this;
        var timer = 7000;
        var fade = timer / 4;
        var images = new Array('DSC06492.jpg','DSC06499.jpg','DSC06508.jpg','DSC06512.jpg','DSC06515.jpg','DSC06523.jpg','DSC06549.jpg','DSC06560.jpg','DSC06562.jpg','DSC06577.jpg','DSC06601.jpg','DSC06606.jpg','DSC06610.jpg','DSC06611.jpg','DSC06612.jpg','DSC06619.jpg','DSC06627.jpg','DSC06629.jpg','DSC06631.jpg','DSC06650.jpg','DSC06654.jpg','DSC06657.jpg','DSC06673.jpg','DSC06677.jpg','DSC06679.jpg');
        var stuff = {
            doSwitch: function() {
                var active = $(what+' img.active');
                if ( active.length == 0 ) active = $(what+' img:last');
                var next =  active.next().length ? active.next() : $(what+' img:first');
                
                active.addClass('last-active');
                
                next.css({opacity: 0.0})
                .addClass('active')
                .animate({opacity: 1.0}, fade, function() {
                    active.removeClass('active last-active');
                });
                
            },
            init:function() {
                // set the opacity of the textbox to 0.8 (for ie);
                $('#slideshow h1').css('opacity','0.8');
                // first load our css
                if(typeof $.preloadCssImages == 'function' ) var loadedImgs = $.preloadCssImages();
                //force preload the slideshow
                jQuery(images).each(function(i){
                    Preload.load('images/slideshow/'+this, function(img){
                        var image = img.obj;
                        $(image).attr('width',img.width);
                        $(image).attr('height',img.height);
                        $(image).css('opacity',0);
                        if (i == 0) {$(image).addClass('active').animate({opacity: 1.0},fade,function(){$('#slideshow').css('background-image', 'none');});}
                        $('#slideshow').append(image);
                    });
				});

                setInterval(function() {
                    stuff.doSwitch.apply()},timer);
            }
        }
        stuff.init.apply(stuff);
        return stuff;
    };
    
    
    if ($('#slideshow').length) {
        new thatchSlideShow('#slideshow');
    }
	
	if ($('#metachanger')) {
        var metadata = '';
        var id = $('#metalink').attr('rel');
        $('#metadiv').hide();
        $('#metalink').click(function(){
            $('#metadiv').slideToggle(500);
            
            $.get("/update.php", { getcontent: "1", id: $(this).attr('rel'), target: 'title' },function(data) {
                //$("form.metaform input[name='title']").val(data);
                $("#mc-title").val(data);
            });
            $.get("/update.php", { getcontent: "1", id: $(this).attr('rel'), target: 'headline' },function(data) {
                //$("form.metaform input[name='desc']").val(data);
                $("#mc-desc").val(data);
            });
            
            $.get("/update.php", { getcontent: "1", id: $(this).attr('rel'), target: 'keywords' },function(data) {
                //$("form.metaform input[name='keywords']").val(data);
                $("#mc-keywords").val(data);
            });
            
            $.get("/update.php", { getcontent: "1", id: $(this).attr('rel'), target: 'weight' },function(data) {
                //$("form.metaform input[name='weight']").val(data);
                $("#mc-weight").val(data);
            });
            
            return false;
        });
        
        $('form#metaform').submit(function() {
            var inputs = [];
            $(':input', this).each(function() {
                inputs.push(this.name + '=' + escape(this.value));
            });
            $('#metadiv').slideUp('fast');
             $.ajax({
                type: "POST",
                url: this.action,
                data: inputs.join('&'),
                beforeSend: function(){$("#loading").slideDown("fast");}, //show loading just when link is clicked 
                complete: function(){ $("#loading").slideUp("fast");}, //stop showing loading when the process is complete 
                success: function(html){ //so, if data is retrieved, store it in html 
                    $("#metadiv").show("slow"); //animation
                    $('#metalog').slideDown('fast');
                    $("#metalog").html("<h2>Meta Log</h2><div style='clear:both'></div>"+html); //show the h
                    setTimeout(function(){
                        $('#metalog').slideUp('slow');
                    }, 5000);
                }
            });
            return false;
        });
    } // metachanger
});


/*******************************************************************************
 *  Preloading
 ******************************************************************************/
var Preload = {
    load: function(trigger, handlers) {
        if( (typeof trigger).toLowerCase() == 'string' ){
            src = trigger;
        }else{
            src = $(trigger).attr('href');
            try{
                opts = eval( '({' + $(trigger).attr('rel') + '})' );
            }catch(e){}
        }
        //test if it is an image, otherwise, use ajax
        if( src.match(/.jpg$|.jpeg$|.png$|.gif$|.bmp$|.tif$|.tiff$|.gi$/i) ){
            var img = new Image(); 
            if( handlers ){
                if(typeof handlers == 'function' ){
                    img.onload = function(){							
                        response = { 
                            width: img.width,
                            height: img.height,
                            obj: $('<img>').attr('src',img.src)
                        };
                        handlers( response );
                    };
                }
                if( handlers.loaded && typeof handlers.error == 'function' ){
                    img.onerror = function( err ){
                        handlers.error( err );
                    }
                }
            }
            img.src = src;
        }else if( src.match(/.swf$|.flv$|.SWF$|.FLV$/i) ){
            if( $.fn.flash ){
                if( handlers ){
                    if( handlers.loaded && typeof handlers.loaded == 'function' ){
                    // todo: code flash handling
                    }
                }				
            }
        }else{
            $.get(
                src,
                function( html ){
                    if( handlers ){
                        if( handlers.loaded && typeof handlers.loaded == 'function' ){							
                            var content = $('<div>').html( html );
                            response = { 
                                width: parseInt(content.children(0).css('width')),
                                height: parseInt(content.children(0).css('height')),
                                obj: content,
                                data: html
                            };
                            handlers.loaded( response );
                        }
                    }
                }
            );
        }
    }
};