function OpenWindow(url,width,height, name, scrlls) {
	x = (640 - width)/2, y = (480 - height)/2;
	
	if (screen) {
		y = (screen.availHeight - height)/2;
		x = (screen.availWidth - width)/2;
	}
	var window_name = (name) ? name : 'mb_' + get_random();
	var scrlls = scrlls || "yes";

	window.open (url, window_name, 'width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x+ ',location=no,status=yes,resizable=yes,scrollbars=' + scrlls + ',titlebar=yes');
}

function InStr(strSearch, charSearchFor)
{
            for (i=0; i < strSearch.length-charSearchFor.length; i++)
            {
                  if (charSearchFor == strSearch.substring(i,charSearchFor.length))
                  {
                        return i;
                  }
            }
            return -1;
}

function ShowBanner(_ele_ids, _uri, _location) {
	var objs = new Array();
	for (var i in _ele_ids) {
		var obj = document.getElementById(_ele_ids[i]);
		if (obj) {
			objs.push(obj);
		}
	}

	$.ajax({
		type: "POST",
		url: '/ajax/banner.php',
		data: 'uri=' + _uri + '&location=' + _location,
		success: function(xml) {
			
			if (xml) {
				var i = 0;
				$(xml).find('banner').each(function(){
					objs[i].innerHTML = $(this).find('content').text();
					i++;
				});
			}
		}
	});		 
}
function init_scroll(){
	for (var i=0, j=''; i<4; i++,j=i) {
		$('#slideshow'+j).serialScroll({
			items:'li',
			prev:'#div_scroll'+j+' a.prev',
			next:'#div_scroll'+j+' a.next',
			axis:'x',
			offset: 0,
			duration:900,
			stop:true,
			lock:true,
			cycle:true,
			force:false,
			autoscroll: true,
			start:0, //порядковый номер(начиная с 0) картинки, с которой начинаем показывать галлерею
			//exclude:2,
			lazy: true
		});
	}
}


function wysiwygInit(_mode, _editor_selector, _lang) {	
	tinyMCE.init({
		mode: _mode,
		theme: 'simple',
		editor_selector: _editor_selector,
		cleanup_on_startup: true,
		remove_trailing_nbsp: true,
		remove_linebreaks: false,
		fix_list_elements: true,
		fix_table_elements: true,
		verify_html: true,
		language: _lang,
		relative_urls: false,
		document_base_url: '/',
		strict_loading_mode: false,
		apply_source_formatting: true,
		convert_fonts_to_spans: true
	});
}

function applyWysiwyg(_lang) {
	var elems = document.getElementsByTagName('TEXTAREA');
	for (var i = 0; i < elems.length; i++) {
		if (elems[i].className.indexOf('wysiwyg') > -1) {
			var id = elems[i].getAttribute('id');
			wysiwygInit('textareas', 'wysiwyg_' + id, _lang)
		}
	}
}

$(document).ready(function(){
    $('.tooltip1').bind("mouseover", function(e){

		
		$('.card_type_tooltip', this).show();
    	$('.tooltip1').css ({
	    'z-index':'5'
    	});
    	$('.tooltip2').css ({
	    'z-index':'5'
    	});
    	$(this).css ({
	    'z-index':'40'
    	});

    });    
    $('.tooltip1').bind("mouseout", function(e){
		$('.card_type_tooltip', this).hide();
    });

    $('.tooltip2').bind("mouseover", function(e){
		$('.card_type_tooltip', this).show();

    });    
    $('.tooltip2').bind("mouseout", function(e){
		$('.card_type_tooltip', this).hide();

    });

	init_scroll();

    $(".tags_list a").tooltip({
		showURL: false,
		loadURL: false
	});

	if ($('#order_count').length)	recalc();

    $('#order_count')
		.keypress( function(e) {
		    if($.browser.msie) {
	    	    return isNum(e.keyCode)
	   	    } else if($.browser.safari) {
	    	    return (e.keyCode) ? isNum(e.keyCode) : isNum(e.charCode) ;
	    	} else {
	    	    return (e.keyCode) ? true : isNum(e.charCode) ;}
		})
    	.keyup(function(e){
    		recalc();
    	});

});

function recalc(){
    var count = $('#order_count').val();
    var price = $('#price').val();
    price = str_replace(' ', '', price);

	if (count == '' || count==0) {
	    $('#send').attr('disabled','disabled');
	    $('#order_count').parent().addClass("error-td");
	} else {
	 	$("#send").removeAttr('disabled');
	    $('#order_count').parent().removeClass("error-td");
	    var total_price = number_format(count * price, 2, ',', ' ');

	    $('#total_price_txt').html(total_price);

	}
	
}

         
function isNum(cCode){
    return /[0-9]/.test(String.fromCharCode(cCode))
}

function select_fix(){
    var font_index = 5;
    var scroll_w = 25; /* Scroll width */
    var spacer_pixel_url = '/f/spacer.gif';
 
    if ($.browser.msie) { 
      var target_tag = '.select';
      var zindex = 10000;
      $(target_tag).each(function(){ 
        if ($(this).css('position') != 'absolute') {
          var css = 'position:relative;';
          if ($(this).css('display')) {
            css += 'display:'+$(this).css('display')+';';
          }
          if ($(this).css('margin-top')) {
            css += 'margin-top:'+$(this).css('margin-top')+';';
          }
          if ($(this).css('margin-right')) {
            css += 'margin-right:'+$(this).css('margin-right')+';';
          }
          if ($(this).css('margin-bottom')) {
            css += 'margin-bottom:'+$(this).css('margin-bottom')+';';
          }
          if ($(this).css('margin-left')) {
            css += 'margin-left:'+$(this).css('margin-left')+';';
          }
          if ($(this).css('height')) {
            var h = $(this).css('height')
          }
          if ($(this).css('width')) {
            var w = $(this).css('width')
          }
          if ($(this).css('vertical-align')) {
            css += 'vertical-align:'+$(this).css('vertical-align')+';';
          }
          var left  = $(this).position().left;
          var top   = $(this).position().top;
          $(this).css({
            'position':'absolute',
            'left':left,
            'top':top,
            'z-index':'0'
          });
        }
        $(this).after('<img src="'+spacer_pixel_url+'" width='+w+' height='+h+' />');
        $(this).parent().css({
          'position':'relative'
        })
        var width = 0;
        var opts = $(this).children('option');
        opts.each(function(){
          if ($(this).text().length > width){
            width = $(this).text().length;
          }
        })
        true_width = width*font_index+scroll_w;
        true_width = (true_width.toFixed(0));
        $(this).attr('min_width',$(this).width()+2);
        if (true_width<$(this).width()+2) {
          true_width = $(this).width()+2;
        }
        $(this).attr('max_width',true_width);
      })
      .mouseover(function(){//alert($(this).attr('max_width') ); 
        if ($(this).attr('max_width') != $(this).attr('min_width')) {
          $(this).css({
            'z-index':zindex++
          }); 
          $(this).animate({  
            'width':$(this).attr('max_width')
          })
        }
      });
      $('body').mouseover(function(){
        $(target_tag).each(function(){
          $(this).css({
            'width':$(this).attr('min_width')
          })
        })
      })
    }
}

function number_format( number, decimals, dec_point, thousands_sep ) {  // Format a number with grouped thousands
		var i, j, kw, kd, km, minus = "";

		if(number < 0){
			minus = "-";
			number = number*-1;
		}	 
	    // input sanitation & defaults
	    if( isNaN(decimals = Math.abs(decimals)) ){
	        decimals = 2;
	    }
	    if( dec_point == undefined ){
	        dec_point = ",";
	    }
	    if( thousands_sep == undefined ){
	        thousands_sep = ".";
	    }
	 
	    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
	 
	    if( (j = i.length) > 3 ){
	        j = j % 3;
	    } else{
	        j = 0;
	    }
	 
	    km = (j ? i.substr(0, j) + thousands_sep : "");
	    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	    //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
	    kd = ((decimals && Math.abs(number - i))? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
	 
	 
	    return minus + km + kw + kd;
}
function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}
