

/*shaddow script*/

cssChange = {'html,body' : {'width' : '100%', 'height' : '100%', 'overflow' : 'hidden'}, 
                    '#layout' : {'position' : 'static', 'width' : '100%', 'height' : '100%', 'overflow' : 'auto'}};
cssOldValues = {};
JsHttpRequest = {query:function(){}};
function getPopupContent(link){
		JsHttpRequest.query(
        link,
        "'':''",
        function(result, errors) {
            jQuery('#pop_up').html(result['data']);
        },
        false  // do not disable caching
    );
}

function showPopup(){
    defaultPopup = true;
    url = window.location.href;
    pattern = '#(.*)?';
    rx = new RegExp(pattern);
    var parts = rx.exec(url);
    
    hash = "";
    if(parts !== null){
        if(parts[1] !== undefined){
            jQuery('#header_'+parts[1]+' a').trigger('click');
        }
        hash = parts[1] || "";
    }
    if(hash !== ""){
        if(hash.substring(0, 6) === 'popup_'){
            jQuery('a.socCommentLink[rel='+hash+']').trigger('click'); 
            defaultPopup = false;
        }
    }
    if(defaultPopup){
        getPopupContent('/menu/main/call_me/?ajax&noMainIndex');
    }
}

function autoIframe(frameId){
    try{
        frame = document.getElementById(frameId);
        innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
        objToResize = (frame.style) ? frame.style : frame;
        objToResize.height = innerDoc.documentElement.scrollHeight;
        
        if(jQuery(window).height() < jQuery('#pop_up_all').height()){
            jQuery('#pop_up_all').css('top','0px');    
        }else{
            top = parseInt((jQuery(window).height() - jQuery('#pop_up_all').height())/2);
            jQuery('#pop_up_all').css('top',top+'px');    
        }
        
    }
    catch(err){
        window.status = err.message;
    }    
}



jQuery(document).ready(function(){
    
    jQuery('#facebook-like').attr('src', 'http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2FOTDEL-KACESTVA%2F157943230905935&layout=button_count&show_faces=false&width=240&action=like&colorscheme=light&height=21');
    
    var descr_block_text_show = false;
    jQuery('.dir_description>a').click(function(){
    	if($.browser.msie) {
    		if(descr_block_text_show){
    			jQuery(this).parent().find('.dir_text').hide();
    		}else{
    			jQuery(this).parent().find('.dir_text').show();
    		}
    		
    	}else{
        	jQuery(this).parent().find('.dir_text').slideToggle('slow');
    	}
    	descr_block_text_show = !descr_block_text_show;
        return false;
    });
    
    jQuery('.descr_block').css('display','none');

    var descr_block_show = false;
    jQuery('.informer_btn a').click(function(){
    	if($.browser.msie) {
    		if(descr_block_show){
    			jQuery('.descr_block').hide();
    		}else{
    			jQuery('.descr_block').show();
    		}
    		
    	}else{
        	jQuery('.descr_block').slideToggle('slow');
    	}
    	descr_block_show = !descr_block_show;
        return false;
    });
    
    
    jQuery('.mainNews .link a.location').click(function(){
        window.location = jQuery(this).attr('rel');
        return false;
    });
    
    jQuery('#sPopUp').css('top', ($(window).height() - 375) / 2).show();
    jQuery('a[href=/menu/ok.design/obratniy_zvon/]').addClass('ajax-link');
    jQuery('a[href=/menu/ok.repair/call_back/]').addClass('ajax-link');
    jQuery('a[href=/menu/ok.building/_obrat_zvonok_stroy/]').addClass('ajax-link');
    jQuery('a[href=/menu/ok.polymers/obratniy_zvon/]').addClass('ajax-link');
        jQuery('.main_part_td img').each(function(){
            if(jQuery(this).is('.noshadow') == false){
                var src = jQuery(this).attr('src');
                var found = src.match('design');
                if(found == null){

                    var photo = jQuery(this).parents('.photo');
                    if(photo.length == 0){
                        var img = $(this);
                        jQuery(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>');
                        jQuery(img).parents('.img-border').width(jQuery(img).width() + 25);
                        jQuery(img).load(function(){
                            jQuery(this).parents('.img-border').width(jQuery(this).width() + 25);
                        });

                    }
                }
            }

        });
	
	
	jQuery('a.ajax-link').click(function(){
		if($.browser.msie) {
			jQuery('body').scrollTop(0);
			for (i in cssChange) {
				cssOldValues[i] = {};
				for (y in cssChange[i]) {
					cssOldValues[i][y] = y == 'height' ? '100%' : jQuery(i).css(y);
					jQuery(i).css(y, cssChange[i][y]);
				}
			}
		}
        widthX = $('body').width();
        x = (widthX - 750)/2 + 45;
        jQuery('#pop_up_all').css('left', x+'px');
        jQuery('#pop_up_all').show();
        //h = $('#tab').height();
        h = $(document).height();
        jQuery('#overlay_pop_up').show();
        jQuery('#overlay_pop_up').css('height', h+'px');
        body_height       = jQuery('#overlay_pop_up').height();
        pop_up_all_height = jQuery('#pop_up').height();
        //margin          = body_height-parseInt($(window).height()/2)-parseInt(pop_up_all_height/2)-30;
        //$('#pop_up_all').css('top', margin+'px');
        setTimeout(function(){
            $('.contact_form_content .right').css('height', jQuery('.contact_form_content_in').height()+'px');
        },200);
        return false;
	});
	
	jQuery('#pop_up_close').click(function(){
	jQuery('#pop_up_all').hide();
	jQuery('#overlay_pop_up').hide();
	
	if(jQuery.browser.msie) {
		for (i in cssOldValues) {
			for (y in cssOldValues[i]) {
				jQuery(i).css(y, cssOldValues[i][y]);
			}
		}
	}
	getPopupContent('/menu/main/call_me/?ajax&noMainIndex');
	return false;
	});
   jQuery('#overlay_pop_up, #overlay_scroll, #p_scroller').live('click', function(){
        jQuery('#popup_rubber').unwrap('#p_scroller');
        jQuery('#popup_rubber').hide();
        jQuery('#pop_up_all').hide();
        jQuery('#overlay_pop_up').hide();
        jQuery('#overlay_scroll').hide();
        jQuery('body').css('overflow','auto');
        
        getPopupContent('/menu/main/call_me/?ajax&noMainIndex'); 
        return false;
  }); 
  
  jQuery('.socCommentLink').click(function(){
        var load_first = true;
        link = jQuery(this);
        $('#layout').width($('#layout').width());
        iframe = jQuery('<iframe id="socCommentsFrame" frameborder="no" scrolling="auto" allowtransparency="true" src="'+link.attr('href')+'"></iframe>');
        var ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("opera") != -1){
            jQuery('html, body').css({'position':'fixed'});
        }
        offsetY = jQuery('body').scrollTop();
        jQuery('body').prepend(iframe.css('top', offsetY)).css({'width' : $('body').width(), 'overflow' : 'hidden'});
        return false;
    });

    showPopup();
  
  
});

function setPopupRubberLeft(){
    trueWidth = jQuery('#popup_rubber').outerWidth() - 66;
    winWidth =  jQuery(window).width();
    left = parseInt((winWidth - trueWidth)/2);
    
    //jQuery('#popup_rubber').css('left', left+'px');   
}

function closePopup(){
    jQuery('#pop_up_all').hide();
    jQuery('#overlay_pop_up').hide();
    getPopupContent('/menu/main/call_me/?ajax&noMainIndex'); 
    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 preloader(){
    //document.getElementById("preload").style.display = "none";
    document.getElementById("layout").style.visibility = "visible";
    document.getElementsByTagName('body')[0].style.overflow = "visible";
}

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() {            
            $(document.body).html();            
            $('#wall-wrapper').css('display', 'none');
            /*if($('#socCommentsFrame').length == 0){
                $(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' ? '������ ����' : '�������� ����');
            if($.browser.msie || $.browser.opera) {
            	$('#wall-wrapper .tree-wrapper').toggle();
            }
            $(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 ($.browser.msie){
            $('#wall-wrapper').css('position','absolute');
            $(window).scroll(function(){
                $('#wall-wrapper').css('top', $('body').get(0).scrollTop+'px');
            })
                $('#wall-wrapper').css('top', $('body').get(0).scrollTop+'px');

 
        }
        
        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 + '"]').trigger('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);
            $('#wall2').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);
            $('#gal' + dir).trigger('click');

            try{
                $('#wall-wrapper > .tree-wrapper > .tree').customScroller({ speed: 4});
            } catch(e) {}
        });
    }

    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);

function wallShow(link){
    i = link.id.split('_');
    jQuery.showWall(link.rel, i[0], i[1]); 
       
}
jQuery(document).ready(function() {
	jQuery('a.wall').click(function() {
        wallShow(this);
        return false;
    });
    init_3dbuttons();
});

function init_3dbuttons() {
    $('.button3d').wrap('<div class="button3d-wrapper" style="overflow:hidden;"></div>').css('position', 'relative');
    $('.button3d-wrapper').each(function() {
        
		mi = $(this).children('.button3d');
		if(mi.hasClass('consult')) mi.height(109);
        $(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'); consult = mi.hasClass('consult');
        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('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;
            }
        })
        $(this).find('.consult').click();
    });
}

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;
}
/*
nsform.Microsoft.AlphaImageLoader(src="' + src + '");');
{
    return img;
}
 */

