$(document).ready(function(){ 
    /*buggy browsers detecting*/
    window.ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    window.ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1 && navigator.appVersion.indexOf("MSIE 8.0") == -1);
    window.ie8 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 8.0") != -1);
    
    /*star rates*/
    $(".star_holder").filter(".active").mousemove(function(e){
        active_holder = $(this).children('span');
        offset = $(this).offset();
        var starWidth = 12;
        var stars = Math.floor((e.pageX - offset.left)/starWidth) + ((e.pageX - offset.left)%starWidth != 0 ? 1 : 0);
        if (stars < 1) stars = 1;
        $('#mark').attr ('value', stars);
        var hints = ['Poor', 'Fair', 'Average', 'Good', 'Excellent'];
        $('#mark_hint').html( lang(hints[stars-1]) );
        $(active_holder).css("width",(stars*starWidth) + "px");
    });
    
    
    /*ie6 png fix img*/
    if (jQuery.browser.msie && (ie55 || ie6)) {
        // png fix break block height so fix them in pixels 
        $(".propertyTitleBlock").css("height",$(".propertyTitleBlock").height() + "px");
        $(".mapResultContainer").css("height",$(".mapResultContainer").height() + "px");
        //fix images with png-source
        jQuery(this).find("img[src$=.png]").each(function() {

            jQuery(this).attr('width',jQuery(this).width());
            jQuery(this).attr('height',jQuery(this).height());

            var prevStyle = '';
            var strNewHTML = '';
            var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
            var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
            var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
            var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
            var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
            var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
            if (this.style.border) {
                prevStyle += 'border:'+this.style.border+';';
                this.style.border = '';
            }
            if (this.style.padding) {
                prevStyle += 'padding:'+this.style.padding+';';
                this.style.padding = '';
            }
            if (this.style.margin) {
                prevStyle += 'margin:'+this.style.margin+';';
                this.style.margin = '';
            }
            var imgStyle = (this.style.cssText);

            strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
            strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
            strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
            strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
            strNewHTML += imgStyle+'"></span>';
            if (prevStyle != ''){
                strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;'+'">' + strNewHTML + '</span>';
            }
            jQuery(this).hide();
            jQuery(this).after(strNewHTML);

        });
        
        // fix css background pngs
        jQuery(this).find("*").each(function(){
            var bgIMG = jQuery(this).css('background-image');
            if(bgIMG.indexOf("-trans.png")!=-1){
                var iebg = bgIMG.split('url("')[1].split('")')[0];
                jQuery(this).css('background-image', 'none');
                jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
            }
        });
        
        
    }
    var fix_back = function(object){
        if (jQuery.browser.msie && (ie6)) {
            var bgIMG = jQuery(object).css('background-image');
            var iebg = bgIMG.split('url("')[1].split('")')[0];
            jQuery(object).css('background-image', 'none');
            jQuery(object).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
        }
    }
    
    /*making bullets for ie6 and ie7 since they do not understand before declaration*/
    if (jQuery.browser.msie && (!ie8)) {
        $(".sidebar .sidebarInfoBlock UL > LI, ul.search-tips > li,.contactUs UL > LI, ul.arrow-list > li, li.arrow, ul.list-type > li, .href_arrow").prepend($("<span>&raquo; </span>")); 
    }
    
});



function dialog_complex( array_fields, parent, save_function , cancel_function, width, position_holder, left_position, top_position, default_values, default_values_repeat ) {
    var width = width || "260px";
    var position_holder = position_holder || parent;
    var left_position = left_position || 0;
    var top_position = top_position || 7;
    var default_values =  default_values || new Array();
    var default_values_repeat = default_values_repeat || new Array();
        
    if (!$("#simple_dialog").length){
        $("#wrapper").append("<div id=\"simple_dialog\"></div>");
    };
    $("#simple_dialog").hide().css("width",width);
    var childrens = $(parent).children().filter(".select_edit");
    
    if (!$(default_values).length) {
        $(childrens).each(function(e,i){
            default_values[e] = $(this).html();
        });
    };
    var form_text = "";
    var buffer_array = new Array("");
    var prev_common = "";
    var length = $(array_fields).length - 1;
    $(array_fields).each(function(e,i){
        var next_label = (e == length)?0:array_fields[e+1].common;
        if (typeof(buffer_array[i.common])=='undefined' || buffer_array[i.common]===null) buffer_array[i.common] = "";
        /* block holder start - label + element  */
        if (!((i.common == prev_common) 
                || (length == 0) 
                || (length == 1 && array_fields[0].common == array_fields[1].common))) {buffer_array[i.common]  = "<div class=\"simple_block\">";} ;
        /* if one element, or elements have the same common number, or its not exchange or text fields we dont need labels*/
        buffer_array[i.common] += (!((i.common == prev_common) 
                                        || (length == 0) 
                                        || (length == 1 && array_fields[0].common == array_fields[1].common)) 
                                        && i.type != "text" && i.type != "exchange_areas" && i.label != "")?"<label for=\"simple_block_field_" + e + "\">" + i.label  + ":</label>":"";
        i.id = i.id || 'simple_block_field_' + e;
        switch(i.type)
        {
            case "select":
                buffer_array[i.common] += "<select " + i.additional + " name=\"simple_block_field_" + e + "\" class=\"simple_block_field " + i.add_class + '" id="' + i.id + '">';
                $(i.values).each(function(num, val){
                    var selected = (default_values[e] == val)?'selected=\"selected\"':"";
                    buffer_array[i.common] += "<option value=\"" + val + "\" " + selected + ">" + val + "</option>";
                });
                buffer_array[i.common] += "</select> ";
                break;
            case "input":
                var text_value = (typeof(default_values[e])=='undefined' || default_values[e]===null)?"":default_values[e];
                buffer_array[i.common] += "<input " + i.additional + "  type=\"text\" name=\"simple_block_field_" + e + "\" class=\"simple_block_field " + i.add_class + '" id="' + i.id + '" value="' + text_value.replace("<br>","\n") + "\" /> ";
                break;
            case "password":
                var text_value = (typeof(default_values[e])=='undefined' || default_values[e]===null)?"":default_values[e];
                buffer_array[i.common] += "<input " + i.additional + "  type=\"password\" name=\"simple_block_field_" + e + "\" class=\"simple_block_field " + i.add_class + '" id="' + i.id + '" value="' + text_value.replace("<br>","\n") + "\" /> ";                    
                break;
            case "checkbox":
                buffer_array[i.common] += "<div class=\"simple_radio_holder\">";
                $(i.values).each(function(num, val){
                    var selected = (default_values[e] == val)?'checked=\"checked\"':"";
                    buffer_array[i.common] += "<p class=\"p_" + num + " " + i.add_class + "\">";
                        buffer_array[i.common] += "<input " + i.additional + selected + "  type=\"checkbox\" name=\"simple_block_field_" + e + '" id="' + i.id + "_" + num + "\" class=\"radio simple_block_field\" value=\"" + num + "\" />";
                        buffer_array[i.common] += "<label for=\"simple_block_field_" + e + "_" + num + "\">" + val + "</label>";
                    buffer_array[i.common] += "</p>";
                });
                buffer_array[i.common] += "</div> ";    
                break;
            case "radio":
                buffer_array[i.common] += "<div class=\"simple_radio_holder\">";
                $(i.values).each(function(num, val){
                    var selected = (default_values[e] == val)?'checked=\"checked\"':"";
                    buffer_array[i.common] += "<p class=\"p_" + num + " " + i.add_class + "\">";
                        buffer_array[i.common] += "<input " + i.additional + selected + "  type=\"radio\" name=\"simple_block_field_" + e + '" id="' + i.id + "_" + num + "\" class=\"radio simple_block_field\" value=\"" + num + "\" />";
                        buffer_array[i.common] += "<label for=\"simple_block_field_" + e + "_" + num + "\">" + val + "</label>";
                    buffer_array[i.common] += "</p>";
                });
                buffer_array[i.common] += "</div> ";
                break;    
            case "textarea":
                var text_value = (typeof(default_values[e])=='undefined' || default_values[e]===null)?"":default_values[e];
                var expr = /<br>/gi ;
                buffer_array[i.common] += "<textarea " + i.additional + "   name=\"simple_block_field_" + e + "\" class=\"simple_block_field " + i.add_class + '" id="' + i.id + '" >' + text_value.replace(expr,"\n") + "</textarea>";
                break;
            case "text":
                buffer_array[i.common] += "<p class=\"simple_block_field " + i.add_class + '" id="' + i.id + "\" " + i.additional +" >" + i.label + "</p>";
                break;
            case "html":
                buffer_array[i.common] += $(i.values[0]).html();
                break;
            case "exchange_areas":
                var give_id = (i.add_class == "exchange_areas_src")?"":'id="' + i.id + '"';
                buffer_array[i.common] += "<div class=\"simple_block_field " + i.add_class + "\" " + i.additional + give_id + " >";
                if (i.add_class == "exchange_areas_src")
                {
                    $(i.values).each(function(num, val){
                        buffer_array[i.common] += "<a class=\"ex_item\" href=\"#\">" + val + "</a>";
                    });
                } else {
                    $(default_values).each(function(num, val){
                        if (num >= e && !(typeof(default_values_repeat[num])=='undefined' || default_values_repeat[num]===null)) {            
                            for (var j = 0; j < default_values_repeat[num]; j++){
                                if (val != "") buffer_array[i.common] += "<a class=\"exa_item\"  href=\"#\">" + val + "</a>";
                            };
                        } else {
                            if (val != "" && num >= e) buffer_array[i.common] += "<a class=\"exa_item\"  href=\"#\">" + val + "</a>";
                        };
                    });
                };
                buffer_array[i.common] += "</div> ";
                break;    
        }
        /* add text after field */
        if (i.add_text_after) buffer_array[i.common] += "<span class=\"simple_additional_text\">" + i.add_text_after + "</span>";
        
        /* close element holder*/
        if(i.common != next_label) {buffer_array[i.common] += "</div>";}
        if ((e == length) || (i.common != next_label)) {form_text += buffer_array[i.common] };
        prev_common = i.common;
    })
    form_text += "<span class=\"ui-dialog-buttonpane right\"><input class=\"ui-state-default ui-corner-all simple_dialog_edit\" type=\"button\" style=\"background:#4E77AB\" value=\""+lang('Save')+"\" /> &nbsp;or ";
    form_text += "<a href=\"#\"  class=\"simple_dialog_cancel\">"+lang('Cancel')+"</a></span>";
    
    /* create form */
    $("#simple_dialog").html(form_text);
    /* form aligning */
    $("#simple_dialog").css("left",$(position_holder).offset().left - $("#wrapper").offset().left - left_position)
                        .css("top",$(position_holder).offset().top - $("#wrapper").offset().top - top_position).fadeIn('fast');
    
    /* add button events */
    $(".simple_dialog_edit").bind("click","",save_function);
    $(".simple_dialog_cancel").bind("click","",cancel_function);
};
