//IE detect
if ($.browser.msie && $.browser.version.substr(0,1)<8) {  
  var IE7 = 1;
} else var IE7 = 0;
if ($.browser.msie && $.browser.version.substr(0,1)<9 && $.browser.version.substr(0,1)>7) {
  var IE8 = 1;
} else var IE8 = 0;
if ($.browser.msie ) {
  var IE = 1;
} else var IE = 0;


// display error dialog
function show_error(element){
	var n_ame = "#"+element.id;
	$.validationEngine.buildPrompt(n_ame,$(element).text(),"error");
}
//webroot path
var d_root = $("#im_placer").text();
//current aver
var aver = $("#im_placeraver").text();
//current afid
var afid = $("#im_placerafid").text();

//datepicker
if($(".datepicker").length){
	$.datepicker.setDefaults($.extend({
									numberOfMonths: 3,
									showButtonPanel: true,
									minDate: 0, 
									maxDate: '+12M +10D',
									dateFormat: "yy-mm-dd",
									showOn: 'button', 
									buttonImage: d_root +'img/icon/cal.png', 
									buttonImageOnly: true										
									}));
	
	$(".datepicker").datepicker({
			dateFormat: "yy-mm-dd",	
			onSelect: function(dateText, inst) {
				var fieldset = $(this).parents('fieldset').attr('id');
				var f_prefix = fieldset.split("_");
				var dateTable = dateText.split("-");
				if(f_prefix[0] == 'main'){
                                    if($(this).is('.from')){
                                        $( ".to" ).datepicker( "option", "minDate", new Date(dateTable[0],dateTable[1] -1 , dateTable[2]) );
                                        if($( ".to" ).val() < dateText)	$( ".to" ).val(dateText);
                                    }
				}
				if(f_prefix[0] == 'multicity'){
					//TODO - 4 multicity feature
				}
				return false;	
			}
	});
}

//auto read-write date value
$(".datepicker").keyup(function(){
	if($(this).is('.from')){
		var itemVal = $(this).val();	
		if (itemVal.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}$/)) {
			var dateTable = itemVal.split("-");
                        $( ".to" ).datepicker( "option", "minDate", new Date(dateTable[0],dateTable[1] -1 , dateTable[2]) );
			if($( ".to" ).val() < itemVal){				
				$( ".to" ).val(itemVal);
			}			
		}
	}

        if($(this).is('.to')){
        var itemVal2 = $(this).val();
        if (itemVal2.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}$/)) {            
                if($( ".from" ).val()>$( this ).val()){                 
                        $( ".to" ).val($(".from").val());
                }
               
        }
	}
});

//auto write 
$(".datepicker").each(function(){
	var id = $(this).attr('id');
	var classes = $(this).is('.auto_off');
	if(classes == false)
		$('#' + id).datepicker('option', {dateFormat: "yy-mm-dd"});

});


// Simple modal
if($("a.simple-modal").length){
	$("a.simple-modal").click(function(e){
		e.preventDefault();
		$('#hiddenModalContent').modal();
	});
}

// Send Form via link
if($(".submit-form").length){
	$(document).ready(function() {
		$(".submit-form").click(function(e){
			var validatorForm = $(this).parents("form");
			var validatorOpts = {};
			validatorOpts['returnIsValid']=true;
			if($(this).parents('form').is('.form-check-login'))
				validatorOpts['promptPosition']="topLeft";
			if (validatorForm.validationEngine(validatorOpts))
			{
			 	if($(this).is(".splashPromotions"))
               		var dialogContent = $('#splashPromotions').html();
               	else if($(this).is(".splashNews"))
               		var dialogContent = $('#splashNews').html();
               	else if($(this).is(".splashTarrif"))
               		var dialogContent = $('#splashTarrif').html();                   	
               	else
               		var dialogContent = $('#splashBoxSearcher').html(); 
				var dialog = $('<div style="display:hidden" class="none" id="ajax-splash-load"><div class="splash-box">'+dialogContent+'</div></div>').appendTo('#page-body');
				dialog.dialog({
								bgiframe: true,
								height: 350,
								width: 450,
								modal: true,
								dialogClass: 'splash',
								closeOnEscape: false,
								draggable: false,
								resizable: false										
							});
				$(this).parents("form").submit();
				e.preventDefault();
				return false;
			}
			e.preventDefault();
	      	return false;
		});
	});	
}








function closeCCErrorMsg(){
	 $('#walidacja_cc').children().each(function(){ //div o id walidacja_cc_invoice zawiera szczeguly walidacji dla poszczegulnych pol
		 var split_walidacji=this.id.split("__");
		 var id_walidacji='#'+split_walidacji[1]; //id pola walidacji powstaje z drugiej czesci id spana po splicie 
		 var id_spana='#'+this.id; 							//$(id_spana).text() zawiera tekst walidacji dla danego pola w postaci calej klasy np. validate[require, length[1,2]]
		
		 var validation_prompt_id="."+split_walidacji[1]+"formError";
		 $.validationEngine.closePrompt(validation_prompt_id,true); //zamyka wszystkie alerty credit card
	 });
}






//	Set border on active passangers type
if($(".passangers").length){
	function setActive(){
							if($(this).val() > 0) $(this).parents("dl").addClass("act");
							else $(this).parents("dl").removeClass("act");
						}
	$(".passangers select").each(setActive);
	$(".passangers select").change(setActive);
}




//	Form validator | Docs: http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
$(document).ready(function() {
	
	$(".form-check").validationEngine();
	$(".form-check-login").validationEngine({promptPosition: "topLeft"});
	$('#errors_all').children().each(function(){
		show_error(this);	  	 
	 });
	

});





// -------------------------------------------
//		SANDBOX
// -------------------------------------------







//Ajax test - modal splash
$('a.ajax-splash').click(function(e) {
						 //prevent the browser to follow the link
                       e.preventDefault();
     		
                       var url = this.href;
						var title = $(this).attr("title");
                       var dialog = $('<div style="display:hidden" class="none" title="' + title + '" id="ajax-splash-load"></div>').appendTo('body');
                       // load remote content
                       dialog.load(
                               url, 
                               {},
                               function (responseText, textStatus, XMLHttpRequest) {
                                       dialog.dialog({
														bgiframe: true,
														//autoOpen: false,
														height: 350,
														width: 450,
														modal: true,
														dialogClass: 'splash',
														closeOnEscape: false,
														draggable: false,
														resizable: false/*,
														close: function() { $("#ajax-splash-load").remove();$(this).dialog('destroy') },
														buttons: {
															Zamknij: function() {
																dialog.dialog('close');
															}
														}*/														
													});
										//dialog.dialog('open');
                               }
                       );
                      
						//return false;
});

$('a.sorting_form').click(function(e){
	
	var ids = $(this).attr('id');
	var sval = ids.split('_');
	var sort = sval[1];
	
	$('#sort_type').val(sort);
		
	
		var dialogContent = $('#splashBoxSearcher').html(); 
		var dialog = $('<div style="display:hidden" class="none" id="ajax-splash-load"><div class="splash-box">'+dialogContent+'</div></div>').appendTo('#page-body');
		dialog.dialog({
						bgiframe: true,
						height: 350,
						width: 450,
						modal: true,
						dialogClass: 'splash',
						closeOnEscape: false,
						draggable: false,
						resizable: false										
					});
		$(this).parents("form").submit();
		e.preventDefault();
		return false;	
	
});


//submit splash screen
$('a.ajax').click(function(e) {
						 //prevent the browser to follow the link
                    e.preventDefault();
                   	var dialogContent = $('#splashBoxSearcher').html(); 
                	var predialog = $('<div style="display:hidden" class="none" id="ajax-splash-load"><div class="splash-box">'+dialogContent+'</div></div>').appendTo('#page-body');
                	predialog.dialog({
                					bgiframe: true,
                					height: 350,
                					width: 450,
                					modal: true,
                					dialogClass: 'splash',
                					closeOnEscape: false,
                					draggable: false,
                					resizable: false										
                				});
     		
                       var url = this.href;
					   var title = $(this).attr("title");
                       var dialog = $('<div style="display:hidden" class="none" title="' + title + '" id="ajax-load"></div>').appendTo('body');
                       var txt = $('#im_placerclose').text();
                   	   var buttonsOpts = {};
                	   buttonsOpts[txt] = function () {
                   		   					dialog.dialog('close');
                						};                       
                       // load remote content
                       dialog.load(
                               url, 
                               {},
                               function (responseText, textStatus, XMLHttpRequest) {
                            	   predialog.dialog('close');     
                            	   	dialog.dialog({
														bgiframe: true,
														height: 550,
														width: 750,
														modal: true,
														draggable: false,
														resizable: false,
														close: function() {$("#ajax-load").remove();$(this).dialog('destroy')},
														buttons: buttonsOpts,
														stack: true
													});
                               }
                       );
});

 
$(".sidebar-box").wrapInner('<div class="sidebar-box-inner-wrap"></div>');
$(".content-box").wrapInner('<div class="content-box-inner-wrap"></div>');
if(IE8) $("#footer").wrapInner('<div class="footer-inner-wrap"></div>');
if(IE8) $("#atlas_box").wrapInner('<div class="atlas_box-wrap"></div>');
	

function splash_screen(id_splash){
	var dialogContent =$('#'+id_splash).html();
	var dialog = $('<div style="display:hidden" class="none" id="ajax-splash-load"><div class="splash-box">'+dialogContent+'</div></div>').appendTo('#page-body');
	dialog.dialog({
					bgiframe: true,
					height: 350,
					width: 450,
					modal: true,
					dialogClass: 'splash',
					closeOnEscape: false,
					draggable: false,
					resizable: false										
				});
}


// Match datepicker text size
if($("input.datepicker").length){
    $(document).ready(function() {
        $("input.datepicker").each(function(){
            var width = $(this).css('width').substr(0, ($(this).css('width').length)-2);
            if(width < 90) {
                $(this).css('font-size','12px');
                $(this).css('text-indent','3px');
            }
            // Set calendatr icon position
            $(this).next('img').css('left',(width-17)+'px');
        });
    });
}


