var pagini = new Array("about","map","oferte","contact");
var initial_coords_case = new Array();
var initial_coords_ap = new Array();
var initial_coords_acasa = new Array();
var top_acasa = new Array();
var left_acasa = new Array();
var img_width = new Array();
var img_height = new Array();

var scara_acasa;
var h = 0;
var w = 0;

var dif = 200;
    
function CautaZona(tip,zona)
{
    $("#cauta_frm").attr("src","cauta.php");
    if ( tip == "case" )
        $('#cauta_frm').contents().find("#frm_caut").attr("action","http://www.casebraila.ro/");
    if ( tip == "ap" )
        $('#cauta_frm').contents().find("#frm_caut").attr("action","http://www.apartamentebraila.ro/");
    $('#cauta_frm').contents().find("#zona1").attr("value",zona);
    $('#cauta_frm').contents().find("#frm_caut").submit();
}
     
function hidePages(id)
{
    for(i=0; i<pagini.length; i++)
    {
        if (i != id && $("#"+pagini[i]).css("display") != "none" )
            $("#"+pagini[i]).css("display","none");    
    }
    top_margin = $(document).height() - $("#meniu_container").height(); 
    
    if ( top_margin == 4 || $("#meniu_container").height() == 48 )
    {
        $("#div_acasa").fadeOut("fast");
        $("#close").html('<img src="images/close_red.png" height="13" alt="" style="float: left; margin-left: 20px;">Inchide');
    }
    else
    {
        $("#div_acasa").fadeIn("fast");
        $("#close").html("");
    }
}

function valideaza()
{
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var telReg = /^[0-9]+$/;
    var erori = "";
    if ( jQuery.trim($("#nume").val()) == "" )
        erori += "Va rog sa introduceti numele dumneavoastra !\n";
    if ( jQuery.trim($("#tel").val() ) == "" )
    {
        if ( jQuery.trim($("#mail").val()) == "" )
            erori += "Va rog sa introduceti emailul sau telefonul dumneavoastra !\n";
        else
        {
            if ( !emailReg.test(jQuery.trim($("#mail").val())) )
                erori += "Va rog sa introduceti o adresa de email valida !\n";
        }
    }
    else
    {
        if ( !telReg.test(jQuery.trim($("#tel").val())) )
            erori += "Va rog sa introduceti doar cifre la numarul de telefon !\n";
    }
    if ( jQuery.trim($("#mesaj").val()) == "" )
        erori += "Va rog sa introduceti mesajul dumneavoastra !\n";
    if ( erori == "")
        $("#frm_mesaj").submit();
    else
    {
        alert(erori);
    }
}

function getInitialCoords()
{
    $("#coord_case area").each(function(index){
        initial_coords_case[index] = $(this).attr("coords"); 
    });
    $("#coord_ap area").each(function(index){
        initial_coords_ap[index] = $(this).attr("coords"); 
    });
    $("#coord_acasa area").each(function(index){
        initial_coords_acasa[index] = $(this).attr("coords"); 
    });
    var temp = new Array();
    $("#acasa img").each(function(index){
        temp = $(this).css("top").split("px");
        top_acasa[index] = parseInt(temp[0]);
        temp = $(this).css("left").split("px");
        left_acasa[index] =  parseInt(temp[0]);
        
        temp = $(this).css("width").split("px");
        img_width[index] = parseInt(temp[0]);
        temp = $(this).css("height").split("px");
        img_height[index] = parseInt(temp[0]);    
        
    });  
}

function coordXY( map )
{
    if ( map == "case" || map == "ap" )
    { 
        // dimensiuni initiale harta case si apartamente
        case_w = 1331; 
        case_h = 822; 
        h1 = $(window).height()-150;
        scara = h1/case_h;
    }
    if ( map == "acasa" )
    {
        // dimensiuni initiale acasa
        acasa_h = 517; 
        h1 = $("#acasa").height();
        scara = h1/acasa_h;
    }
    
    
    $("#coord_"+map+" area").each(function(index){
        coords = "";
        if ( map == "case" )
            coords = initial_coords_case[index];
        if ( map == "ap" )
            coords = initial_coords_ap[index];
        if ( map == "acasa" )
            coords = initial_coords_acasa[index];
                
        arr_coords = coords.split(", ");
        new_coords = new Array();
        for (i=0; i<arr_coords.length; i++ )
        {
            curent_coord = arr_coords[i];
            pos_comma = curent_coord.indexOf(",");
            x = parseInt( curent_coord.substr(0,pos_comma) );
            y = parseInt( curent_coord.substr(pos_comma+1) );
            
            new_x = Math.round(x * scara);
            new_y = Math.round(y * scara);
            
            new_coord = new_x+","+new_y;
            new_coords[i] = new_coord;
        }
        new_str_coords = new_coords.join(", ");
        $(this).attr("coords",new_str_coords);
    });
}

function meniuClick(id)
{
    hidePages(id);
    $("#"+pagini[id]).slideToggle('fast',function(){
        if ( $("#meniu_container").height() == 48 )
        {
             $("#div_acasa").fadeIn("fast");
            $("#close").html('');
        }
        else
        {
            $("#div_acasa").fadeOut("fast");
            $("#close").html('<img src="images/close_red.png" height="13" alt="" style="float: left; margin-left: 20px;">Inchide'); 
        } 
           
    });
}

function acasaLink(actiune, site)
{
    $("#cauta_sites_frm").attr("src","cauta_site.php?site="+site+"&actiune="+actiune); 
}

function getWindowDim()
{
    h = $(window).height()-70;
    w = $(window).width();    
}

function blinking( durata )
{
    $('.blink').fadeOut(durata,function()
    {
        $('.blink').fadeIn(durata,function()
        {
            blinking(durata);                        
        })   
    })
}

$(window).load(function(){
    getInitialCoords();
    getWindowDim();
    blinking(500);
    side_height = $("#sidebar").height();
    $("#sidebar").css("top",Math.round((h+130-side_height)/2) + "px");
    $("#acasa").height(h-dif);
    scara_acasa = (h-dif)/555;
    //alert("scara_acasa="+scara_acasa +"\n acasa_w="+$("#acasa").css("width"));
    $("#acasa").width( Math.round(677*scara_acasa) );
    $("#acasa").height( Math.round(555*scara_acasa) );
    $("#div_acasa").width( $("#acasa").width() + 340 );
    
    
    $("#acasa img").each(function(index){
        if ( index > 0 )
        {
            $(this).css("top", Math.round( scara_acasa * parseInt(top_acasa[index]) )+"px"); 
            $(this).css("left", Math.round( scara_acasa * parseInt(left_acasa[index]) )+"px");
            
            //alert (scara_acasa + "\n" + img_width[index] )
            $(this).css("width", Math.round(img_width[index]*scara_acasa) + "px" ); 
            $(this).css("height", Math.round(img_height[index]*scara_acasa) + "px" );
        }
        else
        {
            $(this).width($("#acasa").width());
            $(this).height($("#acasa").height());
        }
    });
    
    coordXY("case");
    coordXY("ap");
    coordXY("acasa");
    
    $("#about").css("height",h);
    $("#about div").css("height",h-70);
    $("#map").css("height",h);
    $("#oferte").css("height",h);
    $("#contact").css("height",h);
    
    $("#h_ap").css("height",h-80); 
    $("#h_ca").css("height",h-80);
    
    $("#map_ap").width(w/2-30);
    $("#map_case").width(w/2-30);
    
    $(window).resize(function(e) {
        
        getWindowDim();
        side_height = $("#sidebar").height();
        $("#sidebar").css("top",Math.round((h+130-side_height)/2) + "px");        
        $("#acasa").height(h-dif);
        scara_acasa = (h-dif)/555;
        //$("#acasa").width( Math.round(677*scara_acasa) );
        $("#acasa").width( Math.round(677*scara_acasa) );
        $("#acasa").height( Math.round(555*scara_acasa) );
        $("#div_acasa").width($("#acasa").width() + 340 );
        
        $("#acasa img").each(function(index){
            if ( index > 0 )
            {
                $(this).css("top", Math.round( scara_acasa * parseInt(top_acasa[index]) )+"px"); 
                $(this).css("left", Math.round( scara_acasa * parseInt(left_acasa[index]) )+"px");
                
                $(this).css("width", Math.round(img_width[index]*scara_acasa) + "px" ); 
                $(this).css("height", Math.round(img_height[index]*scara_acasa) + "px" );
            }
            else
            {
                $(this).width($("#acasa").width());
                $(this).height($("#acasa").height());
            }
        });
        
        coordXY("case");
        coordXY("ap");
        coordXY("acasa");
        
        $("#about").css("height",h);
        $("#map").css("height",h);
        $("#oferte").css("height",h);
        $("#contact").css("height",h);
        
        $("#h_ap").css("height",h-80); 
        $("#h_ca").css("height",h-80);    
        
        $("#map_ap").width(w/2-30);
        $("#map_case").width(w/2-30);
        
        
    });
    $(".meniu_link")
        .mouseover(function(){
            $(this).css("color","#FF0000");
            if ( $.browser.msie )
               if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
        .mouseout(function(){
            $(this).css("color","#FFFFFF");    
        })
    $(".meniu_link").eq(0).click(function(){
        meniuClick(0);
    });
    $(".meniu_link").eq(1).click(function(){
        meniuClick(1);
    });
    $(".meniu_link").eq(2).click(function(){
        meniuClick(2);
    });
    $(".meniu_link").eq(3).click(function(){
        meniuClick(3);
    });
    $(".tabs li")
        .mouseover(function(){
            if ( $.browser.msie )
               if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
        .click(function(){
            var id = $(this).attr("id");
            
            $(".adresa").each(function(){
                $(this).css("display","none");
            });
            $(".tabs li").each(function(){
                $(this).attr("class","normal");
            });
            $(".tabs li:eq("+id+")").attr("class","selected");
            $(".adresa:eq("+id+")").fadeIn();
        });
    $("#meniu_oferte div")
        .mouseover(function(){
            if ( $.browser.msie )
               if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");
            var id = $("#meniu_oferte div").index(this);
            if ( $(".zona_oferte:eq("+id+")").css('display') === "none" )
            {
                $(this).addClass("meniu_oferta_hover");
            }
            else
            {
                $(this).removeClass("meniu_oferta_active");
            }
        })
        .mouseout( function() {
            var id = $("#meniu_oferte div").index(this);
            if ( $(".zona_oferte:eq("+id+")").css('display') === "none" )
            {
                $(this).removeClass("meniu_oferta_hover");
            }
            else
            {
                $(this).removeClass("meniu_oferta_hover");
            }
        })
        .click(function(){
            var id = $("#meniu_oferte div").index(this);
            $("#meniu_oferte div").each(function(index){
                $(this).removeClass($(this).attr('class'));
                if( index != id )
                    $(this).addClass("meniu_oferta_inactiv");
                else
                    $(this).addClass("meniu_oferta_activ");
            });
            $(".zona_oferte").each(function(){
                $(this).css("display","none");
            });
            $(".zona_oferte:eq("+id+")").fadeIn(150);
            
        });
     $(".site img").mouseover(function(){
        if ( $.browser.msie )
            if ($.browser.version == "9.0")
                $(this).css("cursor","pointer");    
            else
                $(this).css("cursor","hand");
        else
            $(this).css("cursor","pointer");
        $(this).css({ opacity: 0.6 });
    });
    $(".site img").mouseout(function(){
        $(this).css({ opacity: 1 });
    });
    $('.site img').click(function() {
        var lungime = $(this).attr("src").length-4-8;
        var link = "http://www." + $(this).attr("src").substr(8,lungime) + ".ro";
        window.open(link);
    });
    $('#developed')
        .mouseover(function(){
            if ( $.browser.msie )
                if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
        .click(function() {
            window.open("http://www.bajanica-bogdan.tk");
        });
    $('#close')
        .mouseover(function(){
            if ( $.browser.msie )
                if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
        .click(function() {
            hidePages(100);
            $("#div_acasa").fadeIn("fast");
            $("#close").html("");
        });
    $('#oferte_speciale')
        .mouseover(function(){
            if ( $.browser.msie )
                if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
    $('#map_ap')
        .mouseover(function(){
            if ( $.browser.msie )
                if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
        .click(function() {
            $("#map .titlu_pag").text("Harta interactive apartamente");
            $("#harti").css("display","none");
            $("#harta_apartamente").css("display","block");
        });
    $('#map_case')
        .mouseover(function(){
            if ( $.browser.msie )
                if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
        .click(function() {
            $("#map .titlu_pag").text("Harta interactive case");
            $("#harti").css("display","none");
            $("#harta_case").css("display","block");
        });
    $('.back:eq(0)')
        .mouseover(function(){
            if ( $.browser.msie )
                if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
        .click(function() {
            $("#map .titlu_pag").text("Harti interactive");
            $('#harta_apartamente').css('display','none');
            $('#harti').css('display','block');
        });
    $('.back:eq(1)')
        .mouseover(function(){
            if ( $.browser.msie )
                if ($.browser.version == "9.0")
                    $(this).css("cursor","pointer");    
                else
                    $(this).css("cursor","hand");
            else
                $(this).css("cursor","pointer");    
        })
        .click(function() {
            $("#map .titlu_pag").text("Harti interactive");
            $('#harta_case').css('display','none');
            $('#harti').css('display','block');
        });
    $('#target').fold();
    
    function puls ( durata )
    {
        $("#sageata").animate({ top: '55px', right : '60px'}, durata, function()
        {
            $("#sageata").animate({ top: '65px', right : '70px' }, durata, function()
            {
                puls (durata);
            });    
        });
    }
    /*
    function puls_colt( durata )
    {
        $("#turn_object").animate({ width: '58px', height : '58px'}, durata, function()
        {
            $("#turn_object").animate({ width: '50px', height : '50px' }, durata, function()
            {
                puls_colt (durata);
            });    
        });
    } */
    puls(500);
    //puls_colt(500);
    $("#loading").css('display','none');
});

