//****************************************** METODY *****************************************************************

//*************************************** J Q U E R Y ****************************************************************
$(document).ready(function() {
    
    if(document.getElementById("mapCanvas")){ 
    	initializeMap();
    }

    if(document.getElementById("sortList")){ 
     	$("#sortList").sortable({
     	
     	    axis: "y",
            revert:false,
            cursor: "move",
            dropOnEmpty: false,
            scroll:false,
            tolerance: 'pointer',
            helper: 'clone'
     	
     	});
    }
 	
 	$("h1.customerInformation").click(function(){
 	
        display = $("div.customerInformation").css('display');
        if(display == "block"){
        	$("div.customerInformation").slideUp(300);
        }else{
            $("div.customerInformation").slideDown(300);
        }
    });
    
    $("h2.dirtyDate").click(function(){
 	
        display = $("div.dirtyDate").css('display');
        if(display == "block"){
        	$("div.dirtyDate").slideUp(300);
        }else{
            $("div.dirtyDate").slideDown(300);
        }
    });
    
    $("h2.reservation").click(function(){
 	
        display = $("div.reservation").css('display');
        if(display == "block"){
        	$("div.reservation").slideUp(300);
        }else{
            $("div.reservation").slideDown(300);
        }
    });
    
    $("h1.VacuShape").click(function(){
      
        display = $("div.Vacu").css('display');
        if(display == "block"){
             $("div.Vacu").slideUp(300);
        }else{
            $("div.Vacu").slideDown(300);
        }
    });
    
    $("h1.PowerPlate").click(function(){
      
        display = $("div.Power").css('display');
        if(display == "block"){
             $("div.Power").slideUp(300);
        }else{
            $("div.Power").slideDown(300);
        }
    });
    
    $("#Facebook").click(function(){
    
        var newWindow = window.open('https://www.facebook.com/pages/fit-hany-bany/222241304476879', '_blank');
        newWindow.focus();
        return false;
        
    });
    $("#google-mapa").click(function(){
    
        var newWindow = window.open('http://maps.google.com/maps?f=q&source=s_q&hl=cs&geocode=&q=%C4%8Dp.+172+%C5%A0trossova+530+03+Pardubice&sll=37.0625,-95.677068&sspn=47.704107,79.013672&ie=UTF8&hq=&hnear=%C5%A0trossova,+530+03+Pardubice,+%C4%8Cesk%C3%A1+republika&ll=50.035732,15.787557&spn=0.004755,0.009645&t=h&z=17', '_blank');
        newWindow.focus();
        return false;
        
    });
         	
});

function saveSortedList(control){
    
    var rowNumber = $('#sortList li').size();
    var sort = new Array();
    
    for(i = 0;i < rowNumber;i++){
    
        sort[i] = $('#sortList li').eq(i).attr("id");
         
    }
    
    $.ajax({
		type: "POST",
		url: "/admin/helper/savesortedlist",
		data: "sort="+sort+"&control="+control,
		success: function(html){

		    $('div.sortMessage').css("display","inline-block");
		    $('div.sortMessage').html("Pořadí úspěšně uloženo");
            
			}

	});    

}



function addPhoto(){
    
    id = $('.currentContent p.photos input:last').attr('id');
    id++;
    photo = "<label for=\"text\"> &nbsp; </label><input id=\""+id+"\" type=\"file\" name=\"photo"+id+"\" size=\"47\">";
    $('.currentContent p.photos').append(photo);
    $('.currentContent p input[name=hidden]').val(id);
    
    if(id == 5){
    
        $('#pridat_foto').css("display","none");   
    
    }

}

function generatePassword(len){

    var charactors = 'abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var password = '';

    while(password.length < len){
  
        password += charactors.charAt(Math.floor(Math.random()*charactors.length));
    
    }
    
    $('.currentContent p input[id=password]').val(password);
    
}

function addPLayTime(){
    
    count = $('.currentContent p input[name=hiddenPLayTimeCount]').val();
    count++;
    
    date = $('.currentContent div.playTime fieldset:first select[name = date[]] ').html();
    stage = $('.currentContent div.playTime fieldset:first select[name = stage[]] ').html();
    from = $('.currentContent div.playTime fieldset:first select[name = from[]] ').html();
    to = $('.currentContent div.playTime fieldset:first select[name = to[]] ').html();

    playTime = "<fieldset><label>Ve dne:</label><select name=\"date[]\">"+date+"</select><br /><label>Na stagei:</label><select name=\"stage[]\">"+stage+"</select><br /><label>Od - do:</label><select name=\"from[]\">"+from+"</select> -- <select name=\"to[]\">"+to+"</select><br /></fieldset>";
    $('.currentContent div.playTime').append(playTime);
    $('.currentContent p input[name=hiddenPLayTimeCount]').val(count);
    
}

function addNewDay(){
    
    id = $('.currentContent div.date input:last').attr('id');
    id++;
    //photo = "<label for=\"text\"> &nbsp; </label><input id=\""+id+"\" type=\"file\" name=\"photo"+id+"\" size=\"47\">";  
    date = "<p><label for=\"date\">Datum "+id+".dne: </label> <input id=\""+id+"\" type=\"text\" name=\"date[]\" value=\"\" />";
    $('.currentContent div.date').append(date);
    $('.currentContent p input[type=hidden]').val(id);
    
}

function content(){

    itemek = $('div.currentContent select').val();

    $.ajax({
		type: "POST",
		url: "/admin/helper/obsah",
		data: "item="+itemek,
		success: function(html){

             $("div.contentItem").html(html);   

			}

	});

}

function setRezervation(day,month,year){
   
    $("div.animation")
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            $(this).hide();
        });
   
    customer = $('div.currentContent input[name=customer]').val();
   
    $.ajax({
		type: "POST",
		url: "/admin/helper/setreservation",
		data: "day="+day+"& month="+month+"& year="+year+"&customer="+customer,
		success: function(html){
		   
		    $('div.reservations').html(html);
            
		}

	});    

}

function showRezervation(day,month,year){

    $("div.animation")
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            $(this).hide();
        });
        
    $.ajax({
        type: "POST",
        url: "/admin/helper/showreservation",
        data: "day="+day+"& month="+month+"& year="+year,
        success: function(html){
           
            $('div.reservations').html(html);
            
        }
        

    });    
}

function rezerve(day,month,year,time,equipmentID,equipmentTitle){
    $.ajax({
        type: "POST",
        url: "/admin/helper/setsession",
        data: "day="+day+"& month="+month+"& year="+year+"& time="+time+"& equipmentID="+equipmentID+"& equipmentTitle="+equipmentTitle,
        success: function(html){
                          
            window.location = "/rezervace/potvrzeni"
        }    

    });    
    $("div.animation").hide();
}

function clock()
{
    var currentTime = new Date();    
    var currentHours = currentTime.getHours ( );
      var currentMinutes = currentTime.getMinutes ( );
      var currentSeconds = currentTime.getSeconds ( );

       currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
      currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
    
    var time=currentHours+':'+currentMinutes+':'+currentSeconds;
    $('#H_time').text(time);
    
}

function date()
{
    var currentTime = new Date();    
    var day = currentTime.getDate();
      var month = currentTime.getMonth()+1;
      var year = currentTime.getFullYear();

    
    var time=day+'/'+month+'/'+year;
    $('#H_datum ').append(time);
}

function confitmReservation(day,month,year,hour,equipmentID,title)
{
    if(equipmentID == '3' || equipmentID == '2'){
    	
    	jPrompt('Opravdu si přejete zarezervovat termín '+title+' '+day+'.'+month+'.'+year+' '+hour+' ?','', 'Zarezervovat', function(result) {
    	    data = year+'-'+month+'-'+day+' '+hour+'_'+equipmentID+'_'+result;
            reserveEquip(day,month,year,title,data,result);
        });
          
     }else{
     
     	jConfirm('Opravdu si přejete zarezervovat termín '+title+' '+day+'.'+month+'.'+year+' '+hour+' ?','Zarezervovat', function(result) {
     	    data = year+'-'+month+'-'+day+' '+hour+'_'+equipmentID+'_0';
            reserveEquip(day,month,year,title,data,result);
        });
     
     }

}

function reserveEquip(day,month,year,title,data,result){

	                                
            if(result)
            {
                 $.ajax({
                            type: "POST",
                            url: "/helper/reserve",
                            data: "data="+ data,
                            success: function (html) {
                                  $('div.reservations').html(html);
                                  showRezervation(day,month,year);                                
                              },   
                            complete: function (XMLHttpRequest, textStatus) {
                                  
                                  if($('input#hid').val()){
                                  
                                      alert($('input#hid').val());
                                  
                                  }
  
                              } 
                  }); 
            }

}

function getCustValue(element)
{
    $('input.custSearch').val( $(element).text() );
    $('input.custSearchCustomer').val( $(element).text() );
    $('div.cus_list ul li').hide();
     $('div.cus_list').hide();
}

function hideCust()
{
    $('div.cus_list ul li').hide();
     $('div.cus_list').hide();
}



function addBanner(bannerName,width,height,element){
	    html = '<embed wmode="transparent" width="'+width+'" height="'+height+'" quality="high" name="mymovie" id="mymovie" style="" src="'+bannerName+'" type="application/x-shockwave-flash"/>';
	    $(element).append(html);
                    
}

function openInNewWindow(thisClick) {
 // Change "_blank" to something like "newWindow" to load all links in the same new window
 var newWindow = window.open($(thisClick).attr("href"), "_blank");
 newWindow.focus();
 return false;
}

