/*shaddow script*/

$(document).ready(function(){
    
    $('a[href=/menu/ok.design/obratniy_zvon/]').attr('class','ajax-link');
    $('a[href=/menu/ok.repair/call_back/]').attr('class','ajax-link');
    $('a[href=/menu/ok.building/_obrat_zvonok_stroy/]').attr('class','ajax-link');
    $('a[href=/menu/ok.polymers/obratniy_zvon/]').attr('class','ajax-link');
        $('.main_part_td img').each(function(){
            if($(this).is('.noshadow') == false){
                var src = $(this).attr('src');
                var found = src.match('design');
                if(found == null){

                    var photo = $(this).parents('.photo');
                    if(photo.length == 0){
                        var img = $(this);
                        $(img).wrap('<div class="img-border"><div class="left-b"><div class="right-b"><div class="top-b"><div class="bottom-b"><div class="corner-br"><div class="corner-bl"><div class="corner-tr"><div class="corner-tl"></div></div></div></div></div></div></div></div></div>');
                        $(img).parents('.img-border').width($(img).width() + 25);
                        $(img).load(function(){
                            $(this).parents('.img-border').width($(this).width() + 25);
                        });

                    }
                }
            }

        });
        
  $('a.ajax-link').click(function(){
    JsHttpRequest.query(
              '/menu/main/call_me/?ajax&noMainIndex',
              "'':''",
              function(result, errors) {
                widthX = $('body').width();
                x = (widthX - 750)/2 + 45;
                $('#pop_up_all').css('left', x+'px');
                $('#pop_up_all').show();

                //h = $('#tab').height();
                h = $('#body').height();
                $('#overlay_pop_up').show();
                $('#overlay_pop_up').css('height', h+'px');
                $('#pop_up').html(result['data']);
                body_height         = $('#overlay_pop_up').height();
                pop_up_all_height   = $('#pop_up').height();
                margin              = body_height-parseInt($(window).height()/2)-parseInt(pop_up_all_height/2)-30;
                //$('#pop_up_all').css('top', margin+'px');
                $('#pop_up_all').css('top', $('body').scrollTop()+150+'px');
                setTimeout(function(){$('.contact_form_content .right').css('height', $('.contact_form_content_in').height()+'px');},200);
              },
              false  // do not disable caching
          );
    return false;
  });
  
  $('#pop_up_close').click(function(){
        $('#pop_up_all').hide();
        $('#overlay_pop_up').hide();
        return false;    
  });
   $('#overlay_pop_up').click(function(){
        $('#pop_up_all').hide();
        $('#overlay_pop_up').hide();
        return false;    
  });
  
});


/*end shaddow script*/

var agt=navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

if(typeof noPreventSelection == 'undefined') {
	
	document.oncontextmenu = function() {
  		return false;
	}
}

function preventSelection(element){
  if(typeof noPreventSelection != 'undefined') {
  	return;
  }

  var preventSelection = false;

  function addHandler(element, event, handler){
    if (element.attachEvent) 
      element.attachEvent('on' + event, handler);
    else 
      if (element.addEventListener) 
        element.addEventListener(event, handler, false);
  }
  function removeSelection(){
    if (window.getSelection) { window.getSelection().removeAllRanges(); }
    else if (document.selection && document.selection.clear)
      document.selection.clear();
  }
  function killCtrlA(event){
    var event = event || window.event;
    var sender = event.target || event.srcElement;

    if (sender.tagName.match(/INPUT|TEXTAREA/i))
      return;

    var key = event.keyCode || event.which;
    if (event.ctrlKey && key == 'A'.charCodeAt(0))  // 'A'.charCodeAt(0) можно заменить на 65
    {
      removeSelection();

      if (event.preventDefault) 
        event.preventDefault();
      else
        event.returnValue = false;
    }
  }

  addHandler(element, 'mousemove', function(){
    if(preventSelection)
      removeSelection();
  });
  addHandler(element, 'mousedown', function(event){
    var event = event || window.event;
    var sender = event.target || event.srcElement;
    preventSelection = !sender.tagName.match(/INPUT|TEXTAREA/i);
  });

  addHandler(element, 'mouseup', function(){
    if (preventSelection)
      removeSelection();
    preventSelection = false;
  });

  addHandler(element, 'keydown', killCtrlA);
  addHandler(element, 'keyup', killCtrlA);
}



if(is_ie){

  window.fixPNG = function (imgName, attr) {

    var img = new Image();
    img.src = imgName;
    return "<img " + attr + " src='/images/design/null.gif' width='"+img.width+"' height='"+img.height+"' style='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="+'"'+img.src+'"'+");') />";
  }
}else {
  window.fixPNG = function (imgName, attr) {
    return "<img style='cursor:hand;' src='"+imgName+"' alt='' " + attr + " />";
  }
}

  function showbig(picname){
    if(window.screen.availWidth <= 1024 || window.screen.availHeight <= 800){
      window.open('/includes/fotos.php5?pic='+picname,'new','location=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,channelmode=yes');
    }else
      window.open('/includes/foto.php5?pic='+picname,'_blank','scrollbars=yes,taskbar=no,resizeable=no,status=no,menubar=no,top=30,left=30,width=30,height=30');
  }

  function PreLoad(source){
    var NewImage = new Image();
    NewImage.src = source;
  }

  function prepareSubmit(frm){
    var len = frm.all("editbox").length; if (!len) len = 1;
    for (var i=0; i<len; i++)
      frm.all("codebox", i).innerText = frm.all("codebox", i).style.display ? borderOn(frm.all("codebox", i).innerText) : borderOn(frm.all("editbox", i).innerHTML);
  }

 function menuProcess(menuId){
    if(menuId.style.display == 'none'){
     menuId.style.display = 'block';
   }else{
      menuId.style.display = 'none';
   }
 }

 function ge(el){
     return document.getElementById(el);
 }

 function mf(id){
   m = ge(id);
   mImg = ge(id + '_img')
   if(m.style.display == 'block'){
     m.style.display = 'none';
     if(mImg)
     	mImg.src = '/images/design/t_plus.gif';
   }else{
     m.style.display = 'block';
     if(mImg)
     	mImg.src = '/images/design/t_minus.gif';
   }
   return false;
 }
 
 function mf_small(id){
   m = ge(id);
   mImg = ge(id + '_img_small')
   if(m.style.display == 'block'){
     m.style.display = 'none';
     if(mImg)
     	mImg.src = '/images/design/left_menu_plus.gif';
   }else{
     m.style.display = 'block';
     if(mImg)
     	mImg.src = '/images/design/left_menu_minus.gif';
   }
   return false;
 }

 function btShowFunctionArguments(name){
   btChangeFunctionArgumentsBlock(name,'none','block','block');
 }

 function btHideFunctionArguments(name){
   btChangeFunctionArgumentsBlock(name,'block','none','none');
 }

 function btChangeFunctionArgumentsBlock(name,plus,minus,arguments){
    document.getElementById('backtrace_img_plus_'+name).style.display = plus;
    document.getElementById('backtrace_img_minus_'+name).style.display = minus;
    document.getElementById('backtrace_arguments_'+name).style.display = arguments;
 }

  function processTree(elementId,isTop){
    tbl = document.getElementById('dir_'+elementId);
    img = document.getElementById('img_'+elementId);

    if(tbl.style.display != 'none'){
       tbl.style.display = 'none';
       img.alt = 'Раскрыть';
       if(isTop)
         img.src = '/images/design/tree_plus.gif';
       else
         img.src = '/images/design/tree_sub_plus.gif';
    }else{
       tbl.style.display = 'block';
       img.alt = 'Закрыть';
       if(isTop)
         img.src = '/images/design/tree_minus.gif';
       else
         img.src = '/images/design/tree_sub_minus.gif';
    }
 }

 /**
 * Преобразует многомерный обьект данных в строковое представление
 */
 function Dump(d,l) {
    if (l == null) l = 1;
    var s = '';
    if (typeof(d) == "object") {
        s += typeof(d) + " {\n";
        for (var k in d) {
            for (var i=0; i<l; i++) s += "    ";
            s += k+": " + Dump(d[k],l+1);
        }
        for (var i=0; i<l-1; i++) s += "  ";
        s += "}\n"
    } else {
        s += "" + d + "\n";
    }
    return s;
 }
 function  print_r(arr){
  alert(Dump(arr));
 }

 /**
 * Создаёт копию обьекта (для передачи не по ссылки)
 */
 function clone(d) {
  var s = new Array();
  if(typeof(d) == "object"){
    for (var k in d)
      s[k] = clone(d[k]);
  }else
    s = d;
  return s;
 }

 /** v 1.1
 * Выдаёт окончание
 *
 * @param int $count - Количество элементов
 * @param str $word - название элемента в ед. числе
 * @return str - название в указанном количестве
 */
function rus_case(count, word){
  var sp_count = (1 * count % 100 - 1 * count % 10) == 10;
  var count = 1 * count % 10;
  var mod = word.substr(word.length-1, 1);
  if(count == 1 && !sp_count)
    return word;
  switch (mod){
    case 'а' :
      if((count==2 || count==3 || count==4) && !sp_count)
        return word.substr(0, word.length-1) + 'ы';
      else
        return word.substr(0, word.length-1);
    case 'ь' :
      if((count==2 || count==3 || count==4) && !sp_count)
        return word.substr(0, word.length-1) + 'я';
      else
        return word.substr(0, word.length-1) + 'ей';
    case 'е' :
      if((count==2 || count==3 || count==4) && !sp_count)
        return word.substr(0, word.length-1) + 'я';
      else
        return word.substr(0, word.length-1) + 'й';
    default:
      if((count==2 || count==3 || count==4) && !sp_count)
        return word + 'а';
      else
        return word + 'ов';
  }
 }

var cooliris = {};
(function($) {


    $(document).ready(function() { $.docLoaded = true; wallInit(); });

    function wallInit() {
        $('#wall-wrapper .close').click(function() {
            $('#wall-wrapper').css('display', 'none');
            $(document.body).css('overflow-y', 'auto').css('overflow-x', 'auto');
        });
        $('#wall-wrapper .toggle_menu').click(function() {
            t = $('#wall-wrapper .tree');
            t.toggle();
            $(this).html(t.css('display') == 'block' ? 'Скрыть меню' : 'Показать меню');
            $(window).resize();
        });
        /*$('#wall-wrapper .tree-wrapper .up').click(function() {

            $('#wall-wrapper .tree').each(function() {
                if($('#wall-wrapper .tree').height() > $('#wall-wrapper .tree-wrapper').height()) {
                    $(this).css('top', parseInt($(this).css('top')) - 10);
                }
            });
        });*/





    }




    $.showWall = function(feed, dir, guid) {
        $('#wall-wrapper').css('display', 'block');
        if(typeof $.docLoaded == 'undefined') { return; }
        $(document.body).css('overflow-y', 'hidden').css('overflow-x', 'hidden');
        isDataLoaded = false;
        cooliris.onEmbedInitialized = function() {
            if(!isDataLoaded) {
                isDataLoaded = true;
                window.setTimeout(function() {
                    if(cooliris.embed && cooliris.embed.setFeedURL) {
                        cooliris.embed.setFeedURL(feed);
                    }
                }, 200);
                window.setTimeout(function() {
                    cooliris.embed.selectItemByGUID(guid);
                    //cooliris.embed.setFeedURL($(this).attr('href'));
                }, 1500);

            } else {

            }
        }
        if($('#wall-wrapper .tree').html() == '') {
            loadTree(dir);
        } else {
            $('#wall-wrapper .tree a[href^="/galleries/data/' + dir + '"]').click();
        }

        $(window).resize(function() {                                                                                                        
            w = $(window).width();
            h = $(window).height();
            $('#wall-wrapper').width(w).height(h + 27);
            t = $('#wall-wrapper .tree').height(h);
            $('#wall').width(w - (t.css('display') == 'block' ? 300 : 0)).height(h + 27);
            
        }).resize();



    };

    function loadTree(dir) {
        $.get('/galleries/' + dir, '', function (data) {
            
            $('#wall-wrapper .tree').html(data);
            $('#wall-wrapper .tree a').click(treeClick);
            $('#wall-wrapper .tree a[href="/galleries/data/' + dir + '"]').click();
            $('#wall-wrapper > .tree-wrapper > .tree').customScroller({ speed: 4});


        });
    }

    function treeClick() {
        if(!$(this).hasClass('sel')) {
            $('#wall-wrapper .tree a').removeClass('sel');
            $(this).addClass('sel');
            if(cooliris.embed && cooliris.embed.setFeedURL) {
                cooliris.embed.setFeedURL($(this).attr('href'));
            }
        }
        return false;
    }



})(jQuery);


jQuery(document).ready(function() {
    jQuery('a.wall').click(function() {
        i = this.id.split('_');
        jQuery.showWall(this.rel, i[0], i[1]);
        return false;
    });
    init_3dbuttons();
});

function init_3dbuttons() {
    $('.button3d').wrap('<div class="button3d-wrapper"></div>').css('position', 'relative');
    $('.button3d-wrapper').each(function() {
        mi = $(this).children('.button3d');
        $(this).width(mi.width());
        $(this).height(mi.height());
    }).mouseenter(function() {
        mi = $(this).children('.button3d');
        mi_w = mi.width(); mi_h = mi.height(); light = mi.hasClass('light');
        onover_img = $(this).children('.button3d-onover')
        if(!onover_img.length) {
            onover_img = get_bt_img('over', light, mi_w, mi_h);
            onover_img.addClass('button3d-onover').css('margin-top', -mi_h);
            that = this;
            if($.browser.msie  || $.browser.opera) {
                $(that).append(onover_img);
            } else {
                onover_img.load(function() {
                    $(that).append(onover_img);
                });
            }
        }
        onover_img.toggle(true);
    }).mouseleave(function() {
        $(this).children('.button3d-onover').toggle(false);
        $(this).children('.button3d-ondown').toggle(false);
    }).mousedown(function() {
        mi = $(this).children('.button3d');
        mi_w = mi.width(); mi_h = mi.height(); light = mi.hasClass('light');
        $(this).children('.button3d-onover').toggle(false);
        ondown_img = $(this).children('.button3d-ondown')
        if(!ondown_img.length) {
            ondown_img = get_bt_img('down', light, mi_w, mi_h);
            ondown_img.addClass('button3d-ondown').css('margin-top', -mi_h);
            that = this;
            if($.browser.msie || $.browser.opera) {
                $(that).append(ondown_img);
            } else {
                ondown_img.load(function() {
                    $(that).append(ondown_img);
                });
            }
        }
        ondown_img.toggle(true);
    }).mouseup(function() {
        $(this).children('.button3d-ondown').toggle(false);
        $(this).children('.button3d-onover').toggle(true);
        $(this).parent('a').each(function() {
            if(this.target == '_blank') {
                window.open(this.href);
            } else {
                location.href = this.href;
            }
        })
    });
}

is_ie6 = $.browser.msie && $.browser.version == '6.0';

function get_bt_img(prefix, light, w, h) {
    src = '/images/design/buttons/on' + prefix + '_' + (light ? 'light_' : '') + mi_w + 'x' + mi_h + '.png';
    if(w == 89 && h == 66) {
        //w = 88; h = 64;
    }
    img = $(document.createElement('img')).hide();
    img.attr('src', is_ie6 ? '/images/design/null.gif' : src);
    img.width(w).height(h);
    if(is_ie6) {
        img.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '");');
    }
    return img;
}
 