function changeStatus(element_id) {
    var element = document.getElementById(element_id);
    if (element.style.display == "none") {
        element.style.display = "block"; 
    }
    else {
        element.style.display = "none"; 
    }
}


function countPropertiesReloadAdmin() {
    countProperties(true);
}

function countPropertiesReload() {
    countProperties(false);
}

function countProperties(admin) {
    var mode = jQuery('#fpocet option:selected').val();
    newUrl = ''; 
    var url = document.URL; 
    var search_pos = url.indexOf("search");
    var pos = url.indexOf('count/'); 
    
    if (search_pos > -1) {
        if (url.indexOf("?")) {
            var pos = url.indexOf('count=');
            if (pos > -1) {
                var newUrl = url.substr(0, pos-1);
                newUrl += url.substr(pos+7);
                
                window.location.replace(newUrl + "&count=" + mode);
            } else {
                
                var newUrl = url; 
                window.location.replace(newUrl + "&count=" + mode);
            }
        } else {
            var newUrl = url; 
            window.location.replace(newUrl + "?count=" + mode);
        }
    } else { 
        if (pos > -1) {
            var newUrl = url.substr(0, pos-1);
            newUrl += url.substr(pos+7);
        
        } else {
            newUrl = url; 
        }
        if (admin == false) {
            if (newUrl.indexOf('index') == -1) {
                if (newUrl.charAt(newUrl.length-1) != '/') {
                    newUrl +='/';
                }
    
                newUrl+="index";
            }
        }
    
        if (newUrl.charAt(newUrl.length) != '/') {
            newUrl +='/';
        }
        window.location.replace(newUrl + "count/" + mode);
    }
}

function sortPropertiesReloadAdmin() {
    sortProperties(true)
}

function sortPropertiesReload() {
    sortProperties(false) 
}

function sortProperties(admin) {
    var mode = jQuery('#fradit option:selected').val();
    if (mode == 0) 
        return; 
    newUrl = ''; 
    var url = document.URL; 
    var search_pos = url.indexOf('search'); 
    var pos = url.indexOf('sorting/'); 
    if (search_pos > -1) {
        if (url.indexOf("?") > -1) {
            var pos = url.indexOf('sorting=');
            if (pos > -1) {
                var newUrl = url.substr(0, pos-1);
                newUrl += url.substr(pos+9);
                
                window.location.replace(newUrl + "&sorting=" + mode);
            } else {
                
                var newUrl = url; 
                window.location.replace(newUrl + "&sorting=" + mode);
            }
        } else {
            var newUrl = url; 
            window.location.replace(newUrl + "?sorting=" + mode);
        }   
    } else {
        if (pos > -1) {
            var newUrl = url.substr(0, pos-1);
        
            newUrl += url.substr(pos+9);
        
        } else {
            newUrl = url; 
        }
        if (admin == false) {
            if (newUrl.indexOf('index') == -1) {
                if (newUrl.charAt(newUrl.length-1) != '/') {
                    newUrl +='/';
                }
    
                newUrl+="index";
            } 
        }
        if (newUrl.charAt(newUrl.length-1) != '/') {
            newUrl +='/';
        }
        window.location.replace(newUrl + "sorting/" + mode);
    }
    
}


//    jQuery.noConflict();  
//    jQuery(document).ready(function() {	
//
//        //                jQuery(document).pngFix(); 
//
//
//        jQuery(".main_image .desc").show(); //Show Banner
//        jQuery(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity
//
//        jQuery(".image_thumb ul li:first").addClass('active'); //Add the active class (highlights the very first list item by default)
//        jQuery(".image_thumb ul li").click(function(){
//            //Set Variables
//            var imgAlt = jQuery(this).find('img').attr("alt"); //Get Alt Tag of Image
//            var imgTitle = jQuery(this).find('a').attr("href"); //Get Main Image URL
//            var imgDesc = jQuery(this).find('.block').html();  //Get HTML of the "block" container
//            var imgDescHeight = jQuery(".main_image").find('.block').height(); //Find the height of the "block"
//
//            if (jQuery(this).is(".active")) {  //If the list item is active/selected, then...
//                return false; // Don't click through - Prevents repetitive animations on active/selected list-item
//            } else { //If not active then...
//                //Animate the Description
//                jQuery(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() { //Pull the block down (negative bottom margin of its own height)
//                    jQuery(".main_image .block").html(imgDesc).animate({ opacity: 0.85,  marginBottom: "0" }, 250 ); //swap the html of the block, then pull the block container back up and set opacity
//                    jQuery(".main_image img").attr({ src: imgTitle , alt: imgAlt}); //Switch the main image (URL + alt tag)
//                });
//            }
//            //Show active list-item
//            jQuery(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
//            jQuery(this).addClass('active');  //Add class of 'active' on the selected list
//            return false;
//
//        }) .hover(function(){ //Hover effects on list-item
//            jQuery(this).addClass('hover'); //Add class "hover" on hover
//        }, function() {
//            jQuery(this).removeClass('hover'); //Remove class "hover" on hover out
//        });
//
//        jQuery("a.collapse").click(function(){
//            jQuery(".main_banner .block").slideToggle(); //Toggle the description (slide up and down)
//            jQuery("a.collapse").toggleClass("show"); //Toggle the class name of "show" (the hide/show tab)
//        });
//    });
    
function fetchCityParts(offer_id, checked) {
    if (offer_id == -1) return; 
    if (checked == '-1') return; 
    try {
        var city = jQuery('#location :selected').text();
        var type = jQuery('#type_id :selected').val();
    } catch (exception) {}
    
    jQuery.ajax({ 
        type: "GET",
        url: "http://www.psn.cz/public/fetch-city-parts", 
        data: "city="+city+"&offer_id="+offer_id+"&type="+type+"&checked="+checked,
        success: function(message){
            document.getElementById('chckboxes').innerHTML = message;
    
        }

    });
}
        
function addToFavorites(id) {
    jQuery.ajax({ 
        type: "GET",
        url: "http://www.psn.cz/public/favorites/add", 
        data: "id="+id,
        success: function(message){
            document.getElementById('p'+id).innerHTML = message;
    
        }

    });
}

function removeFromFavorites(id) {
    jQuery.ajax({ 
        type: "GET",
        url: "http://www.psn.cz/public/favorites/remove", 
        data: "id="+id,
        success: function(message){
            document.getElementById('p'+id).innerHTML = message;
    
        }

    });
}

function addToFavoritesDetail(id) {
    jQuery.ajax({ 
        type: "GET",
        url: "http://www.psn.cz/public/favorites/add-detail", 
        data: "id="+id,
        success: function(message){
            document.getElementById('p'+id).innerHTML = message;
    
        }

    });
}

function removeFromFavoritesDetail(id) {
    jQuery.ajax({ 
        type: "GET",
        url: "http://www.psn.cz/public/favorites/remove-detail", 
        data: "id="+id,
        success: function(message){
            document.getElementById('p'+id).innerHTML = message;
    
        }

    });
}

function removeFromFavoritesInFavorites(id) {
    jQuery.ajax({ 
        type: "GET",
        url: "http://www.psn.cz/public/favorites/remove-detail", 
        data: "id="+id,
        success: function(message){
            //                alert(id);
            var elid = document.getElementById('p'+id);
            //                alert(elid);
            elid.setAttribute("style", "display:none");
            
        }

    });
}
        
function fetchDispositions(offer_id, checked) {
    if (offer_id == -1) return; 
    if (checked == '-1') return; 
    try {
        var type = jQuery('#type_id :selected').val();
        var city = jQuery('#location :selected').text();
    } catch (exception) {}

    var lang = window.location.href.split('/')[3];

    //    alert(checked )
    jQuery.ajax({ 
        type: "GET",
        url: "http://www.psn.cz/" + lang + "/fetch-dispositions",
        data: "offer_id="+offer_id+"&city="+city+"&type="+type+"&checked="+checked,
        success: function(message){
            document.getElementById('disp-frame').innerHTML = message;
        }
    });
}

function enableAll() {
    var chckbox = document.getElementById('cpall');
    if (chckbox.checked == true) {
        selectAllLocations();
        enableAllDispositions();
    }
}

function selectAllLocations() {
    var children = document.getElementById('chckboxes').childNodes;
    
    var j = 0;
    var i = 0;
    var inputs = null;
    for (i in children) {
        
        inputs = children[i].childNodes; 
        for (j in inputs) {
            try {
                inputs[j].checked = true;
            } catch (exception) {}
        //                alert(inputs.object);
        }
        
    }
}

function enableAllDispositions() {
    var disps = document.getElementById('disp-frame').childNodes;
    var i = 0;
    var inputs = null; 
    for (i in disps) {
        inputs = disps[i].childNodes; 
        var j = 0;
        for (j in inputs) {
            try {
                var el = document.getElementById(inputs[j].id);
                el.removeAttribute("disabled");
            } catch(exception) {}
        } 
    }
}

function disableDispositions(offer_id) {
    var children = document.getElementById('chckboxes').childNodes;
    var disps = document.getElementById('disp-frame').childNodes;
    var chckbox = document.getElementById('cpall');
    var j = 0;
    var i = 0;
    var cp = '';
    var inputs = null;
    var el = null;
    for (i in children) {
        inputs = children[i].childNodes; 
        for (j in inputs) {
            try {
                if (inputs[j].checked == true && !isNaN(inputs[j].value)) {
                    cp += inputs[j].value+'XX';
                } else if (chckbox.checked == true && !isNaN(inputs[j].value)) {
                    chckbox.checked = false;
                }
            } catch(exception) {}
        }
    }
    
    if (cp.length == 0) {
        //enable all
        for (i in disps) {
            inputs = disps[i].childNodes; 
            for (j in inputs) {
                try {
                    el = document.getElementById(inputs[j].id);
                    el.removeAttribute("disabled");
                } catch (exception) {}
            }
        }
        return; 
    }
    //    alert(cp);

    var type = jQuery('#type_id :selected').val();
    var city = jQuery('#location :selected').text();
    jQuery.ajax({ 
        cache: false,
        type: "GET",
        url: "http://www.psn.cz/public/fetch-dispositions/disable-dispositions", 
        data: "offer_id="+offer_id+"&city="+city+"&type="+type+"&cp="+cp,
        success: function(message){
            
            if (message.length == 0) { //disable all
                for (i in disps) {
                    inputs = disps[i].childNodes; 
                    for (j in inputs) {
                        var el = document.getElementById(inputs[j].id);
                        el.setAttribute("disabled", "true");
                    } 
                }
                return;
            }
            var brokenString = message.split("XX"); 
            i = 0;
            j = 0;  
            var l = 0;
            var match = false; 
            for (i in disps) {
                inputs = disps[i].childNodes; 
                for (j in inputs) {
                    //                    alert(inputs[j].id);
                    //zistime, ci ten input je niekde v brokenString;
                    l = 0;
                    match = false; 
                    for (l in brokenString) {
                        if ('d'+brokenString[l] == inputs[j].id) {
                            match = true;   
                        }
                    }
                    try {
                        el = document.getElementById(inputs[j].id);
                    
                        if (match == true && inputs[j].id != 'dall') {
                            el.removeAttribute("disabled");
                        } else {
                            //                            el.disabled = true;
                            el.setAttribute("disabled", "true");
                        }
                    } catch (exception) { 
    
                    }

    
                }
            }
        }
    
       
    });
}


function changeValue(id)
{
    if (jQuery('#'.id).checked){
        jQuery('#'.id).setValue('1');
    } else {
        jQuery('#'.id).setValue('0');
        jQuery('#'.id).checked=false;
    }

}

function createInput(id, price, normal) {
    var parent = document.getElementById("price"+id); 
    var parentNodes = parent.childNodes; 
    var i = 0; 
    for (i in parentNodes) {
        if (parentNodes[i].id == 'spanPrice'+id) {
            parent.removeChild(parentNodes[i]);
            var input = document.createElement("input"); 
            input.setAttribute("value", price);
            input.setAttribute("id", "inputPrice"+id)
            input.setAttribute("ondblclick", "savePrice("+id+", " + price + ", " +normal+")");
            parent.appendChild(input);
            parent.removeAttribute("ondblclick");
            
        }
    }
}

var sparent = null; 
var sparentNodes = null; 
var sid = null; 
var sprice = null; 
var snormal = null; 
var scurrentNode = null;
var snewPrice = null; 
var sspan = null;

function savePrice(lid, lprice, lnormal) {
     
    sparent = document.getElementById("price"+lid); 
    sid = lid; 
    sparentNodes = sparent.childNodes; 
    sprice = lprice; 
    snormal = lnormal;
    var i = 0; 
    for (i in sparentNodes) {
        if (sparentNodes[i].id == 'inputPrice'+sid) {
            scurrentNode = sparentNodes[i];
            span = document.createElement("span"); 
            span.setAttribute("id", "spanPrice"+sid);
            snewPrice = sparentNodes[i].value;
            span.innerHTML = snewPrice; 
            
            var url = '';
            if (snormal == true) 
                url = "http://www.psn.cz/public/admin-save-price/price"
            else 
                url = "http://www.psn.cz/public/admin-save-price/price-per-meter"
            jQuery.ajax({ 
                type: "GET",
                url: url, 
                data: "id="+sid+"&price="+snewPrice,
                success: function(message){
                    if (message == true) {
                        sparent.removeChild(scurrentNode);
                        //                        parent.removeAttribute("ondblclick");
                        sparent.setAttribute("ondblclick", "createInput("+sid+","+sprice+", "+snormal+")");
                        sparent.appendChild(span);
                    } else {
                        alert(message + " error ");
                    }
    
                }

            });
            
           
        }
    }
}
