$( function () {

   $intro_img = $("#intro_image");
   if ($intro_img.length > 0) {

       var images = String($intro_img.attr('class')).match(/([a-zA-Z0-9\-]+\.png)/g);
       var current_image = 0;

       if ((images != 'undefined') && (images.length > 1)) {
           var animation = function () {
               current_image = (current_image + 1 == images.length) ? 0 : current_image + 1;

               $.imgLoad('/wp-content/themes/ecm/images/' + images[current_image], function (img) {

                   $intro_img.fadeOut(400, function () {
                       $(this).attr('src', img.src).fadeIn( function () {
                           setTimeout(animation, 2000);
                       });
                   });
                   
               })

           };

           setTimeout(animation, 2000);
       }

   }

   $("a[href$=.jpg],a[href$=.jpeg],a[href$=.png],a[href$=.gif]", "#cms-content").each( function () {

      $(this).click( function () {

        popup($(this).attr('href'));
        return false;

      });

   });

   $("#searchform").submit( function () {
        if ($("#s").val() == '') {
            $("#s").css('border-color', '#E6040A');
            return false;
        }
   });

});

jQuery.extend({
    imgLoad: function(src, loaded_callback) {
        var img = new Image();
        $(img).load( function () {
            loaded_callback(img);
        });
        img.src = src;
        return this;
    },
    dimIn: function (user_css, callback) {
        $dim = $("#dim");
        if ($dim.length == 0) {
            $dim = $("<div />").attr('id', 'dim').css(
                {position: 'absolute',
                  top: 0,
                  left: 0,
                  width: '100%',
                  height: $(document).height(),
                  zIndex: 100,
                  background: '#333',
                  opacity: .4});
            $('body').prepend($dim);
        }
        if ($dim.data('act') == true) return false;
        if (user_css != undefined) {
            $dim.css(user_css);
        }
        return $dim.data('act', true).fadeIn(400, callback);
    },
    dimOut: function (callback) {
        $dim = $("#dim");
        if ($dim.data('act') == true) {
            $dim.fadeOut(400, callback).data('act', false);
        }
    }
});

jQuery.fn.extend({
	fadeOver: function (attr, speed, callback) {
		c = this.clone();
		c.attr(attr).css( { position: 'absolute',
                            width: this.width(),
                            height: this.height(),
                            display: 'none',
                            zIndex: 2 } );
        $tp = this.parent(); // container
		$tp.css( { width: $tp.width(),
				   height: $tp.height()} );
		c.insertBefore(this).fadeIn(speed, function () {
			c.next().remove();
			c.css( { position: 'relative', zIndex: 1 } );
			if (typeof(callback) == 'function') callback();
		});
		return this;
	}
});

function popup(url) {

    $.dimIn();

    if (typeof(window.popuped) == 'undefined') {
        window.popuped = $('<div style="display: none; cursor: pointer; position: absolute; padding: 5px; background: #2E8390; z-index: 1001" />').appendTo('body').click( function () {$(this).find('img').fadeOut(400).end().fadeOut(400); $.dimOut(); } );
        $('<img style="display: none" />').appendTo(window.popuped);
    }

    $popuped = window.popuped;

    var popup_top = $(document).scrollTop() + Math.ceil($(window).height() / 2 - 50);
    if (popup_top < 10) {
        popup_top = 10;
    }

    var props = { width: 100,
                  height: 100,
                  left: Math.ceil($(window).width() / 2) - 50,
                  top: popup_top};

    if ($popuped.css('display') == 'none') {
        $popuped.css(props);
    } else {
        $popuped.find('img').fadeOut(400);
        $.dimOut();
    }

    var img = new Image();

    $popuped.fadeIn(400);

    $(img).load( function () {

        var x = $(document).scrollTop() + Math.ceil(($(window).height() - img.height) / 2);
        x = x < 15 ? 15 : x;

        $popuped.find('img').attr('src', img.src).end().animate(
            {
                top: x,
                left: Math.ceil(($(window).width() - img.width) / 2),
                height: img.height,
                width: img.width
            }, 400, function () {$(this).find('img').fadeIn('fast');}
        );


    });

    img.src = url;

}
