$(function() {

	$('body').supersleight();
	messages = {
		en : {
			mandatory   : 'Please, choose a destination',
			newsletter  : 'Successfully subscribed',
			wrong_email : 'Invalid email',
			close		: 'close',
			question	: 'Your request was received successfully. As possible soon, we will contact you',
			msg_send : 'Please, choose a destination',
			q_send	 : 'Message sent',
			url		 : 'URL was sent'
		},
		bg : {
			mandatory   : 'Моля, изберете дестинация',
			newsletter  : 'Абонирахте се успешно',
			wrong_email : 'Невалиден email',
			close		: 'затвори',
			question	: 'Вашето запитване бе получено успешно. Във възможно най кратък срок, ще се свържем с вас',
			msg_send : 'Запитването бе изпратено',
			q_send	 : 'Съобщението бе изпратено',
			url		 : 'Адреса бе изпратен'
		}
	}

	var base_url = 'http://chartairbg.com/';
	
 	var val = $('#subscribe_email').val();

	$('ul#ch_flights li, ul#promo li').hover(function() {
		$(this).animate({'backgroundColor' : '#E9E9E9'});
		
	}, function() {
		$(this).animate({'backgroundColor' : '#fff'});
	});
	$('#subscribe_email').focus(function() {
		$(this).val('');
	});
	
	$('#subscribe_email').blur(function() {
		if($(this).val() == '') {
			$(this).val(val);
		}
	});

	$('#subscribe_email')
 	$('#newsletter_subscribe').submit(function(e){ 
		e.preventDefault();	
		if(!validateEmail($('#subscribe_email').val())) {
			$('<p>'+messages[$.cookie('language')]['wrong_email']+'</p>').dialog({
						modal: true,
						buttons: {
							Ok: function() {
								$( this ).dialog( "close" );
							}
						}
					});
			
			return false;
		}
		
		$.post('/ajax/processor/subscribe/', {email : $('#subscribe_email').val(), language : $('#lng').val()}, function() {
			$('#submit_newsletter_button').remove();
			$('#newsletter_subscribe').append('<center><p style="padding-top: 6px; font-size: 0.9em; color: #A6101B">'+			messages[$.cookie('language')]['newsletter']+'</center></p>');
		});
	});
	
	$('#make_request').click(function() {
		$('#request_form').modal(
			{
				overlayClose : true,
				closeClass : 'close_me'
				
			});
	});
	     


	$( "#tabs" ).tabs();
		$('#offer_header a').click(function() {
			$('#order_nav').slideToggle();
	});
	
	$('#search_form_submit').click(function() {
		if($('#where').val() == 0) {
			return false;
		}
		$('#search_form').submit();
	});
	$("#where").change(function(){
	  
		var section_id = $('#where').val();
		
		$.getJSON(base_url + 'ajax/processor/getCategories/'+section_id+'/json', function(data) {
		
	
		   
			$('#what option').each(function(i, option) {
				$(option).remove();
			})
			
       

			$.each(data, function(key, val) {
		    	$('#what').append('<option value="' + key + '">' + val);		
			 });
		
		//	$('#search_bar div.jqTransformSelectWrapper:eq(1) option').append(olist);
		});
	    return false; //prevent default browser action
	});
	
	$('#question').submit(function(e) {
		var error = [];
		$('#question').find('input:text, textarea').removeClass('error_red');
		e.preventDefault();
		$('#question input:text, textarea').each(function(i) {
			if($(this).val() == '') {
				$(this).addClass('error_red');
				error.push(1);
			} 
		});
		
		if(error.length > 0) {
			return false;
		} 
		$(this).hide();
	$.post(base_url + 'ajax/processor/askQuestion', $('#question').serialize(), function() {
		$('#request_form').append('<br /><br /><br /><br /><center><p>'+messages[$.cookie('language')]['question']+'</p><br /></center>');
	});
});

});

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



function validateEmail(elementValue){  
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
    return emailPattern.test(elementValue);  
}

