
function openPopup(url,name,width,height) {
	var popupWin = top[name];
	if (popupWin != null && typeof(popupWin) == 'object' && !popupWin.closed) {
		if (popupWin.document.location.href != url) {
			popupWin.document.location.href = url;
		}
	} else {
		popupWin = window.open(url, name, 'width=' + width + ',height=' + height + 'status=0,menubar=0,resizable=0,scrollbars=0, left=' + String((screen.width-width)/2) + ',top=' + String((screen.height-height)/2));
	}
	popupWin.focus();
}

function closePopup(url) {
	if (opener) {
		if (opener.closed) window.open(url, "");
		self.close();
		return false;
	}
	return true;
}

function wopen(url,name,w,h){
	opened = window.open(url,name,"top=100,left=100,width="+w+",height="+h+",resizable=1,scrollbars=1,menubar=0");
	opened.document.open();
	opened.document.write('<html>\n');
	opened.document.write('<head></head>\n');
	opened.document.write('<body style="margin:0; padding:0;">\n');
	opened.document.write('<img src="'+url+'" alt="" />\n');
	opened.document.write('</body>\n');
	opened.document.write('</html>');
	opened.document.close();
}

function wopen2(url,name,w,h){
	window.open(url,name,"top=100,left=100,width="+w+",height="+h+",status=yes,menubar=yes,resizable=yes,scrollbars=yes");
}

function shhi(obj){
	var elem = document.getElementById(obj);
	if (elem.style.display == "block"){
		elem.style.display = "none";
	}else{
		elem.style.display = "block";
	}
}

$(document).ready(function(){
	$('td.b2').html("<!-- webim button --><a href=\"/webim/client.php?locale=ru\" target=\"_blank\" onclick=\"if(navigator.userAgent.toLowerCase().indexOf('opera') != -1 &amp;&amp; window.event.preventDefault) window.event.preventDefault();this.newWindow = window.open('/webim/client.php?locale=ru&amp;url='+escape(document.location.href.replace('http://','').replace('https://',''))+'&amp;referrer='+escape(document.referrer.replace('http://','').replace('https://','')), 'webim', 'toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=600,height=400,resizable=1');this.newWindow.focus();this.newWindow.opener=window;return false;\"><img style=\"margin: -25px 0 10px;\" src=\"/webim/button.php?i=simple&amp;lang=ru\" border=\"0\" width=\"180\" height=\"95\" alt=\"\"/></a><!-- / webim button -->" + $('td.b2').html());
	
	//Rich media
	$('#kupon-control').click(function() {
		$("#kupon_block").hide();
		return false;
		
	});
	
	//Форма заказа в слайдере
	$('a#order').click(function(){
		$('#f_order').show().css('top', $(document).scrollTop()+167);
		$('#o_type').val($(this).attr('href'));
		return false;
	});
	
	//Переход по вкладкам в ниво-слайдере
	$("ul.tabs li").click(
      function() {
		
		var data = $(this).find('a').attr('rel');
		var container = data.split('&container=');
		container = container[1];
		
		$(this).siblings("li").removeClass("activetab");
		
		//Подгрузка и инициализация заново
		$('#'+container).replaceWith('<div id="'+container+'" class="nivoSlider"></div>');
		$('#'+container).load('/getSliderData.php?'+data, function() {
			initNivoSlider('#'+container);		
		});
		        
		$(this).toggleClass("activetab");
		
		return false;
      }
	);
	
	/*var sliderMc = 'tabs_gallery_mebel';
	var sliderMcLinks = 'link_more';
	var sliderMcSecond = 'mc_second';
	var sliderMcSecondLinks = 'mc_second_link_more';
	
	initializeSlider(sliderMc, sliderMcLinks);
	initializeSlider(sliderMcSecond, sliderMcSecondLinks);
	
	
	function initializeSlider(sliderContainer, sliderLinks) {
		if($('#'+sliderContainer).length){
			var	main_elem = $('#'+sliderContainer),
					tabs_m = $('.instab td[id^=tab_m] div span', main_elem),
					active_tab = $('.instab .in3add div span', main_elem),
					links_more = $('#'+sliderLinks+' a');
			
			function changeClass(elem, strClass){
				var	m_td = elem.closest('td'),
						general_td = m_td.add(m_td.prev()).add(m_td.next());
				general_td.each(function(){
					var elem = $(this);
					elem.attr('class', elem.attr('class').replace('add', '')+strClass);
				});
			}
			
			
			
			
			
			function init_tab_m(){
				var	main_inner = $('#inner_block_img_mebel'),
						out_div = $('div', main_inner),
						links_m = $('.main_link_m', out_div),
						span_m = $('span', links_m),
						main_outer = $('#block_img_mebel'),
						max_height = 366,
						//outer_width = 0,
						delay_anim_move = 5000,
						speed_anim_move = 700,
						load_i_c = 0;
				
				links_m.each(function(){
					var elem = $(this);
					elem.children('span').load(function(){
						//outer_width += elem.width();
						if(max_height < elem.height())
							max_height = elem.height();
						
					});
					load_i_c++;
				});
				var check_load_img = setInterval(function(){
					if(links_m.length == load_i_c){
						if(max_height == 0){
							max_height = 366;
							$('#link_more').hide();
						} else {
							$('#link_more').show();
						}
						main_inner.css('width', main_inner.parent().width());
						out_div.css({
							'width': links_m.length * main_inner.width(),
							'height': max_height
						});
							
						span_m.css('width', main_inner.width());
						//links_m.css('width', main_inner.width());
						clearInterval(check_load_img);
					}
				}, 300);
				if($('#list_img').length){
					$('#list_img').remove();
				}
				
				if(links_m.length > 1){
					function move_m_img(plus){
						if(move_screen == links_m.length)
							move_screen = 0;
						if(plus != undefined)
							move_screen = plus;
						out_div.animate({
							marginLeft: -1 * main_inner.width() * move_screen
						}, speed_anim_move, function(){
							curr_list_p.removeClass('curr_m');
							list_p.eq(move_screen).addClass('curr_m');
							curr_list_p = list_p.eq(move_screen);
							links_more.eq(0).attr('href', link_to_order[move_screen]);
							links_more.eq(1).attr('href', link_to_more[move_screen]);
							move_screen++;
						});
					}
			
					var str_html = '<div id="list_img">';
					for(var i = 0; i < links_m.length; i++){
						str_html += '<p></p>';
					}
					str_html += '</div>';
					main_outer.append(str_html);
					
					var	list_p = $('#list_img p'),
						curr_list_p = list_p.eq(0);
						
					curr_list_p.addClass('curr_m');
					
					list_p.click(function(){
						var elem = $(this);
						if(!elem.hasClass('curr_m')){
							move_m_img(list_p.index(elem));
						}
					});
					
					var	scroll_delay = setInterval(function(){
							move_m_img();
						}, delay_anim_move),
						move_screen = 1;
						
					main_outer.hover(function(){
						clearInterval(scroll_delay);
					}, function(){
						scroll_delay = setInterval(function(){
							move_m_img();
						}, delay_anim_move);
					});
				}
				main_inner.css('height', 'auto');
			}
			
			tabs_m.click(function(){
				var	elem = $(this),
						main_inner = $('#inner_block_img_mebel');
				if(elem[0] != active_tab[0]){
					changeClass(active_tab, '');
					changeClass(elem, 'add');
					active_tab = elem;
					main_inner.css('height', main_inner.height());
					main_inner.load(link_to_tab[tabs_m.index(elem)], function(){
						init_tab_m();
						links_more.eq(0).attr('href', link_to_order[0]);
						links_more.eq(1).attr('href', link_to_more[0]);
					});
				}
				return false;
			});
			
			init_tab_m();
		}
	}*/
});


function checkOrder2(){
	var err = '';
	if (jQuery("#o_fio").val()==""){
		err+= "Не указаны ФИО\n";
	}
	if (jQuery("#o_email").val()==""){
		err+= "Не указан email\n";
	}
	if (jQuery("#o_phone").val()==""){
		err+= "Не указан телефон\n";
	}
	if (err!=''){
		alert(err);
	}else{
		$.ajax({
			type: "POST",
			url: "/ord.php",
			data: "main=1&type="+jQuery("#o_type").val()+"&fio="+jQuery("#o_fio").val()+"&email="+jQuery("#o_email").val()+"&phone="+jQuery("#o_phone").val()+"&org="+jQuery("#o_org").val(),
			success: function(msg){
				//alert(msg);
				jQuery("#regForm").hide();
				jQuery("#o_msg").show(); 
			}
		});
	}
}

var save_html;
var sended = false;

//Инициализация ниво-слайдера
//param container - контейнер со слайдером
function initNivoSlider(container) {
	if(!container) {
		container = '.nivoSlider';
	}
	$(document).ready(function() {
		$(container).nivoSlider({
			afterChange: function(){
				var link = $(this).find('a:visible').attr('href');
				$(this).next('div.link_more').find('a').attr('href', link);
			}
		});
	});
}
(function($){
	
	$.randomImage = {
		defaults: {
			
			//you can change these defaults to your own preferences.
			path: 'images/', //change this to the path of your images
			myImages: ['ofdir_phone_1.png', 'ofdir_phone_2.png' ] //put image names in this bracket. ex: 'harold.jpg', 'maude.jpg', 'etc'
			
		}			
	}
	
	$.fn.extend({
			randomImage:function(config) {
				
				var config = $.extend({}, $.randomImage.defaults, config); 
				
				 return this.each(function() {
						
						var imageNames = config.myImages;
						
						//get size of array, randomize a number from this
						// use this number as the array index

						var imageNamesSize = imageNames.length;

						var lotteryNumber = Math.floor(Math.random()*imageNamesSize);

						var winnerImage = imageNames[lotteryNumber];

						var fullPath = config.path + winnerImage;
						
						
						//put this image into DOM at class of randomImage
						// alt tag will be image filename.
						$(this).attr( {
										src: fullPath,
										alt: winnerImage
									});
				
						
				});	
			}
			
	});
	
	
	
})(jQuery);
