jQuery(document).ready(
function(){
    $('textarea[name=comm_contact]').focus(
        function()
        {
            if(($(this).val()=='Примечания к контактным данным') || ($(this).val()=='Примечания не указаны'))
                $(this).val('');
        }
    );
    $('textarea[name=comm_contact]').blur(
        function()
        {
            if($(this).val()=='')
                $(this).val('Примечания к контактным данным');
        }
    );
    $('textarea[name=comm_time]').focus(
        function()
        {
            if(($(this).val()=='Обозначьте удобное для вас время, когда мы можем вам позвонить') || ($(this).val()=='Время удобное для звонка не указано'))
                $(this).val('');
        }
    );
    $('textarea[name=comm_time]').blur(
        function()
        {
            if($(this).val()=='')
                $(this).val('Обозначьте удобное для вас время, когда мы можем вам позвонить');
        }
    );
    $('a.inputSubmit').click(
        function(){
            $("#getStatus").submit();
        }
    );
    $("div.rightSide p.f-c a").click(
        function(){
            changeFont($(this).attr('class'));
            return false;
        }
    );
    $("p.select_epit").click(
        function()
        {
            $("#epitaphs").dialog("open");
            return false;
        }
    );
    $('.font-changer a').click(
        function()
        {
            var id=$(this).parent().attr('id');
            $('.'+id+'Font').val($(this).html());
            $('.'+id+'CurFont').text($(this).html());
            $('.'+id+'Preview').css('font-family',$('.'+id+'Font').val());
            $('.font-changer').dialog('close');
            return false;
        }
    );
    $('#epitaphs a.se').click(
        function()
        {
            $('textarea[name=epit]').val($(this).text());
            $('.epitPreview').html(nl2br($(this).html()));
            $('#epitaphs').dialog('close');
            return false;
        }
    );
    $("#epitaphs").dialog({"modal":1,"position":"center","autoOpen": "false","height":"auto","width":400,"title":"Список эпитафий"});
    $(".font-changer").dialog({"modal":1,"position":"center","autoOpen": "false","height":"auto","width":400});
    $(".font-changer").dialog('close');
    $("#epitaphs").dialog('close');
    
    $(".inscriptionTxt input").blur(
        function()
        {
            var s=$('input[name=surname]').val();
            var n=$('input[name=name]').val();
            var f=$('input[name=f_name]').val();
            var b=$('input[name=bDay]').val();
            var d=$('input[name=dDay]').val();
            $('.inscrPreview').html(s+" "+n+" "+f+"<br />"+b+" - "+d);
        }
    );
    $("textarea[name=epit]").blur(
        function()
        {
            $('.epitPreview').html(nl2br($(this).val()));
        }
    );
    $("input[name=inscriptionType]").change(
        function()
        {
            var status=$(this).val();
            if (status>0)
            {
                // ON
                $('.inscrBlock').show();
            } else {
                // OFF
                $('.inscrBlock').hide();
            }
        }
    );
    $("input[name=epitaph]").change(
        function()
        {
            var status=$(this).val();
            if (status==1)
            {
                // ON
                $('.epitBlock').show();
            } else {
                // OFF
                $('.epitBlock').hide();
            }
        }
    );
    $("input[name=portrait]").change(
        function()
        {
            var status=$(this).val();
            if (status==1)
            {
                // ON
                $('.portBlock').show();
            } else {
                // OFF
                $('.portBlock').hide();
            }
        }
    );
    $("input[name=additionalPictures]").change(
        function()
        {
            var status=$(this).val();
            if (status==1)
            {
                // ON
                $('.imagesBlock').show();
            } else {
                // OFF
                $('.imagesBlock').hide();
            }
        }
    );
    $(".orderContent input[type=text]").blur(function(){calc();});
    $(".orderContent textarea").blur(function(){calc();});
    $(".orderContent input[type=radio]").change(function(){calc();});
    
    
    
    $(".specialPrice li a img").click(
        function()
        {
            ShowBigPic($(this).attr('src').replace('thumb_','o_'));
            return false;
        }
    );
    $("td.photo a img").click(
        function()
        {
            ShowBigPic($(this).parent().attr('href'));
            return false;
        }
    );
    $(".minipic a img").click(
        function()
        {
            ShowBigPic($(this).parent().attr('href'));
            return false;
        }
    );
});

function changeFont(link){
    $('#'+link+'.font-changer').dialog();
}
function randomNumber (m,n)
{
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}
var store = {
    getCounter : function(){
       return this.counter; 
    }
    ,setCounter: function(newcounter){
        this.counter = newcounter;     
    }
    ,storeAction: function(action){
        this.action = action;
    }
    ,getAction: function(){
        return this.action;
    }
}

function deleteImage(f){
   imagenum = store.getCounter()-1; 
   
   if(imagenum > 1){
       $('#'+f+'_'+imagenum).remove();
       
       store.setCounter(imagenum);                             
   }
   else{
       $('#'+f).remove();
       store.setCounter(imagenum);
   }
   
   calc('yes');
}

function createUploader(id,dir,p,f){
    counter = 1;
    store.setCounter(counter);
    //alert(counter);    
    store.storeAction('/application/ajax.php?f=upload&dir='+dir+'&fName='+f);
    
    var uploader = new qq.FileUploader({
        "element": document.getElementById(id),
        "action": '/application/ajax.php?f=upload&dir='+dir+'&fName='+f,
        "allowedExtensions": ['jpg', 'jpeg', 'png', 'gif'], 
        "onSubmit" : function(id, fileName){
          if(store.getCounter() > 3) {
              alert('Вы загрузили максимально возможное количество фотографий');
          }
        },       
        "onProgress":function()
        {
            //$(p).append('<img src="/theme/stones/i/loading.gif" alt="" />');
        },
        "onComplete":function(id, fileName, response){
            $.get(
                '/application/ajax.php',
                {
                    'f':'getRect',
                    'd':f,
                    'dir':dir,
                    'counter':store.getCounter()
                },
                function(data)
                {
                    if(dir=='images'){
                        if(counter == 1){
                           $(p).empty(); 
                        }
                    }
                    //alert(fileName);
                    
                    //var repid=f;
                    //repid.replace(/\-/g," ");
                    //alert(p);
                    //alert(data);
                    //$(p).html('<img src="/data/'+dir+'/thumb'+repid+'.jpg" alt="" />');
                    //$(p).html("loaded...");
                    //$(p).html("loaded..");
                    //$(p).html("loaded.");
                   if(dir=='images'){
                        var replacedf = f.replace(/\-/g,'');
                        $(p).append(data);
                        counter = store.getCounter();
                        counter = counter + 1;
                        store.setCounter(counter);
                        $('.imageuploadlabel').html('Загрузить еще');
                        $('#imageuploadlabel').html('Загрузить изображение');
                        $('#deleteImage').show();
                        calc(); 
                    }
                    else { 
                        $(p).html(data);
                    }
                    
                    
                }
            );
            
           
        } 
    });           
}

function createUploader2(id,dir,p,f){
    
    var uploader = new qq.FileUploader({
        "element": document.getElementById(id),
        "action": '/application/ajax.php?f=upload&dir='+dir+'&fName='+f,
        "onProgress":function()
        {
            $(p).html('<img src="/theme/stones/i/loading.gif" alt="" />');
        },
        "onComplete":function(id, fileName, response){
            $.get(
                '/application/ajax.php',
                {
                    'f':'getRect',
                    'd':f,
                    'dir':dir
                },
                function(data)
                {
                    
                    $(p).html(data);
                    
                }
            );
            
        }
    });           
}

function Vblock(inscr, epit, port, images)
{
    if (inscr>0)
    {
        // ON
        $('.inscrBlock').show();
    } else {
        // OFF
        $('.inscrBlock').hide();
    }
    if (epit==1)
    {
        // ON
        $('.epitBlock').show();
    } else {
        // OFF
        $('.epitBlock').hide();
    }
    if (port==1)
    {
        // ON
        $('.portBlock').show();
    } else {
        // OFF
        $('.portBlock').hide();
    }
    if (images==1)
    {
        // ON
        $('.imagesBlock').show();
    } else {
        // OFF
        $('.imagesBlock').hide();
    }
    calc();
}
function ShowBigPic(img)
    {
            $("#footer").parent().append('<div id="popup_dialog"></div>');
            var data='<img src="'+img+'" alt="" />';
            $("#popup_dialog").html(data);
            var opt={
                "modal":1,"position":"top","autoOpen": "false","height":"auto","title":"Просмотр изображения","width":640
            };
            $("#popup_dialog").dialog(opt);
    }
function nl2br(str){return str.replace(/([^>])\n/g, '$1<br/>')}
