jQuery.fn.myPopup = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '.link-popup',
		linkCloseName: 'a.close',
		divFader: 'fader'
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _IE = ((navigator.appName.indexOf('Microsoft Internet Explorer') != -1) && (parseInt(navigator.appVersion) < 9)) ? true : false;
		var links = _hold.find(_options.linkOpenName);
		var _fader = $('<div class="'+_options.divFader+'"></div>');
		var popup;
		$('body').append(_fader);
		_fader.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 999,
			background: 'black',
			opacity: 0.7
		});

		function init(_obj){
			popup = $(_obj);
			var btnClose = popup.find(_options.linkCloseName);
			
			if (_IE){
				$('select').css({visibility: 'hidden'});
				popup.find('select').css({visibility: 'visible'});
			}
			var w = $('body').width();
			var _w = $('#container').width();
			if (_w > w) w =_w;
			var h = $(window).height();
			var _offset = $('html').scrollTop();
			
			var ret = _offset+(h/2) - popup.outerHeight(true)/2;
			if (ret < 0) ret = 0;
			popup.css({
				top: ret,
				left: w/2 - popup.outerWidth(true)/2
			}).hide();
			_fader.css({
				width: w,
				height: $('#container').height()
			}).fadeIn(300, function(){
				popup.fadeIn(300);
			});
			$(window).resize(function(){
				w = $('body').width();
				_w = $('#container').width();
				if (_w > w) w =_w;
				popup.css({
					left: w/2 - popup.outerWidth(true)/2
				});
				_fader.css({
					width: w
				});
			});
			

			btnClose.click(function(){
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				$(window).unbind('resize');
				return false;
			});
			_fader.click(function(){
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				$(window).unbind('resize');
				return false;
			});
		}
		links.click(function(){
			if ($(this).attr('href')){
				init($(this).attr('href'));
			}
			else{
				init($(this).attr('title'));
			}
			return false;
		});
	});
}
function initOpenBox(){
	$('div.slide-block').each(function(){
		var hold = $(this);
		var link = hold.find('a.button');
		var box = hold.find('div.slide-block-hold');
		var h = box.outerHeight(true);
		
		hold.addClass('opened');
		link.click(function(){
			if (hold.hasClass('opened')){
				box.animate({height:0}, {queue:false, duration: 500});
				hold.removeClass('opened');
			}
			else{
				box.animate({height:h}, {queue:false, duration: 500});
				hold.addClass('opened');
			}
			
			return false;
		});
	});
}
function initSlideText(){
	$('ul.index-offers div.image').each(function(){
		var hold = $(this);
		var box = hold.find('> div.box');
		var span = hold.find('> div.box > span');
		var h_b = box.outerHeight(true);
		var h_s = span.outerHeight(true);
		
		box.css({height:h_s});
		
		box.hover(function(){
			box.animate({height:h_b}, {queue:false, duration: 300});
		}, function(){
			box.animate({height:h_s}, {queue:false, duration: 300});
		});
	});
}
function initFormValidation() {
	var _errorClass = 'error';
	var _regEmail = /^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/;
	var _regPhone = /^[69]{2}[0-9]{8}$/;
	var _regNum = /^[0-9]+$/;
	var _regDay = /^(([0]{1}[1-9]{1})|([1-9]{1})|([1-2]{1}[0-9]{1})|([3]{1}[0-1]{1}))$/;
	var _regMon = /^(([0]{1}[1-9]{1})|([1-9]{1})|([1]{1}[0-2]{1}))$/;
	var _regYear = /^[19]{2}[0-9]{2}$/;
	
	$('form.validate-form').each(function(){
		var _form = $(this);
		function checkFields() {
			
			var _flag = false;
			_form.find('.'+_errorClass).removeClass(_errorClass);

			// fields validation
			_form.find('input.required-email').each(function(){
				if(!_regEmail.test($(this).val())) addError($(this));
			});
			_form.find('input.required-day').each(function(){
				if(!_regDay.test($(this).val())) addError($(this));
			});
			_form.find('input.required-mon').each(function(){
				if(!_regMon.test($(this).val())) addError($(this));
			});
			_form.find('input.required-year').each(function(){
				if(!_regYear.test($(this).val())) addError($(this));
			});
			_form.find('input.required-phone').each(function(){
				if(!_regPhone.test($(this).val())) addError($(this));
			});
			_form.find('input.required-num').each(function(){
				if(!_regNum.test($(this).val())) addError($(this));
			});
			_form.find('input.required, textarea.required').each(function(){
				if(!$(this).val().length || $(this).val() == $(this).attr('alt')) addError($(this));
			});

			// error class adding
			function addError(_obj) {
				_obj.parent().addClass(_errorClass);
				_flag=true;
			}
			return _flag;
		}

		// catch form submit event
		_form.find('.submit').click(function(){
			if(checkFields()) {
				return false;
			}
		});
	});
}
$(document).ready(function(){
	initOpenBox();
	initSlideText();
	$('body').myPopup();
	initFormValidation();


    jQuery('input[hint],textarea[hint]').each(function(i,v){
     if($(v).attr('hint') != ''){
         $(v).val($(v).attr('hint'));
     }
    });

    jQuery('input[hint],textarea[hint]').focus(function(){
     if($(this).val()  == $(this).attr('hint')){
         $(this).val('');
     }
    });
    jQuery('input[hint],textarea[hint]').blur(function(){
        if($(this).val()  == '' ){
         $(this).val($(this).attr('hint'));
        }
    });

});


function close_modal(id){
_IE = ((navigator.appName.indexOf('Microsoft Internet Explorer') != -1) && (parseInt(navigator.appVersion) < 9)) ? true : false;
        $('#'+id).css({left: '-9999px'});
        $('.fader').hide();
        if (_IE) $('select').css({visibility: 'visible'});
        $(window).unbind('resize');
}

		function open_modal(_obj){
            	var _fader = $('<div class="fader"></div>');

            _IE = ((navigator.appName.indexOf('Microsoft Internet Explorer') != -1) && (parseInt(navigator.appVersion) < 9)) ? true : false;
			popup = $(_obj);
			var btnClose = popup.find('a.close');

			if (_IE){
				$('select').css({visibility: 'hidden'});
				popup.find('select').css({visibility: 'visible'});
			}
			var w = $('body').width();
			var _w = $('#container').width();
			if (_w > w) w =_w;
			var h = $(window).height();
			var _offset = $('html').scrollTop();

			var ret = _offset+(h/2) - popup.outerHeight(true)/2;
			if (ret < 0) ret = 0;
			popup.css({
				top: ret,
				left: w/2 - popup.outerWidth(true)/2
			}).hide();
			_fader.css({
				width: w,
				height: $('#container').height()
			}).fadeIn(300, function(){
				popup.fadeIn(300);
			});
			$(window).resize(function(){
				w = $('body').width();
				_w = $('#container').width();
				if (_w > w) w =_w;
				popup.css({
					left: w/2 - popup.outerWidth(true)/2
				});
				_fader.css({
					width: w
				});
			});


			btnClose.click(function(){
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				$(window).unbind('resize');
				return false;
			});
			_fader.click(function(){
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				$(window).unbind('resize');
				return false;
			});
		}
