$(function () {
 $('.selectMenu').click( function(){
        if($(this).hasClass('hover')){}
        else{
            $(this).addClass('hover');
            $('.selectMenu ul').slideDown(400);
        }
    });
    
    $('.selectMenu').hover(
        function () {}, 
        function () {
            if($(this).hasClass('hover')){
                $('.selectMenu ul').slideUp(400,function(){
                            $('.selectMenu').removeClass('hover');
                        });
            }
        }
    );
    $(".btn-search").click( function () {

        $(this).parents('form:first').submit();

    });

    $("#btn-close").click( function () {
        $.dimOut();
        $("#form").fadeOut();
        return false;
    });

    $("#btn-send-form").click( function () {

        var $inputs = $(this).parent().parent().find('.required').animate( {color: '#212121', borderColor: "#AAADB4"}, 400);

        if ($inputs.filter('[value=]').stop().animate( {color: "#f00000", borderColor: '#f00000'}, 500).length == 0) {

            var post_data = {ajax: true, action: 'send-form'};
            var form_data = $.getForm($("#form"));

            for (attrname in form_data) {
                post_data[attrname] = form_data[attrname];
            }

            $.post('/', post_data, function (data) {

                if (data == 'ok') {

                    $("#form .f").hide();
                    $("#form .successful").show();

                } else {

                    $("#form input[name=elpastas]").animate( {color: "#f00000", borderColor: '#f00000'}, 500);

                }
                
            });

        }

    });

    $("#btn-clear").click( function () {

        $(":input[type=text], textarea", "#form").val('');
        return false;

    });

    var js_actions = $("#body").attr('class').match(/js\-[a-z_]+/g);

    if (js_actions != null) {
        $.each(js_actions, function () {
            if (primer[this.substr(3)] != undefined) {
                primer[this.substr(3)]();
            }
        });
    }

    primer.popup();

});

var primer = {

    popup: function () {

      $(".popup").each( function () {

          var classes = String($(this).attr('class'));
          var matches = classes.match(/popup\-(.+)/);
          if ($('#' + matches[1]).length > 0) {
              $(this).data('popup', matches[1]).click( function () {

                  $.dimIn();
                  $('#' + $(this).data('popup')).center().css('zIndex', 1001).fadeIn();
                  return false;
                  
              });
          }

         
      });

    },

    homepage: function () {

        
    }

}

//- required
function popup(url) {

    if (typeof(window.popuped) == 'undefined') {
        window.popuped = $('<div style="display: none; cursor: pointer; position: absolute; padding: 5px; background: #333" />').appendTo('body').click( function () {$(this).find('img').fadeOut(400).end().fadeOut(400);} );
        $('<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);
    }

    var img = new Image();

    $popuped.fadeIn(400);

    $(img).load( function () {

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


    });

    img.src = url;

}

// plugins
(function(jQuery){

	jQuery.each(['backgroundColor', 'color', 'borderColor'], function(i, attr){
		jQuery.fx.step[attr] = function(fx){
			if (fx.state == 0) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}
			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	function getRGB(color) {
		var result;
		if (color && color.constructor == Array && color.length == 3) return color;
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
            return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];
	}

	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);
			if (color != '' && color != 'transparent' || jQuery.nodeName(elem, "body"))	break;
			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	}

})(jQuery);

jQuery.extend( jQuery.easing, {
    def: 'easeOutQuad',
    easeOutQuad: function (x, t, b, c, d) {
        return -c *(t/=d)*(t-2) + b;
    }
});

jQuery.extend({
    slide: function($container, child_element_no, callback) {
        var $slider = $container.children(":first");
        var new_offset = $slider.children(':eq('+child_element_no+')').offset().left - $container.offset().left - ($slider.offset().left - $container.offset().left);
        $slider.animate( {left: -1 * new_offset}, 600, 'easeOutQuad', callback);
        return this;
    },
    slide_page: function($container, page_no, size) {
        if (typeof(size) == 'undefined') {
            size = parseInt($container.parent().css('height').match(/\d+/));
        }
        var new_offset = size * page_no;
        $container.animate( {top: -1 * new_offset}, 600, 'easeOutQuad');
        return this;
    },
    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);
        }
    },
    getForm: function ($form) {
        var ret = {};
        $form.find(':input[type=text], textarea').each( function () {

            ret[$(this).attr('name')] = $(this).val();

        });
        return ret;
    }
});

jQuery.fn.extend({
    center: function () {
        this.css( {position: 'absolute',
                    top: $(document).scrollTop() + (Math.ceil($(window).height()) - this.height()) / 2,
                    left: '50%',
                    marginLeft: -1 * Math.ceil(this.width() / 2)} );

        return this;
    },
    fadeOver: function (speed, callback) {
        c = this.clone();
        c.css( {position: 'absolute',
                         width: this.width(),
                         height: this.height(),
                         display: 'none',
                         zIndex: 2} ).addClass('no-box-shadow');
        $tp = this.parent(); // container
        $tp.css( {width: $tp.width(),
                   height: $tp.height()} );

        c.insertBefore(this).fadeIn(speed, function () {
                c.removeClass('no-box-shadow').next().remove();
                c.css( {zIndex: 1} );
                if (typeof(callback) == 'function') callback();
        });
        return this;
    }
});
