jQuery('document').ready(function(){
	var pg, pp, al, mode, pg_old, category; var cust_id = '';
	pp = 16; al = 1; pg = 1; pg_old = pg; category = '';
	patt = Array("&");
	repl = Array("_db_amp_");
	// Получить заглавия блоки
	workwithdb('get_blocks', 'site_elements', '', '', '');
	// Получить меню
	workwithdb('get_menu', '', 0, 0, jQuery('#top_menu'));
	// Получить нижние баннеры
	workwithdb('get_banners', '', 0, 0, jQuery('#banners'));
	// Получить главный блок
//	workwithdb('get_block', '', 'main_page', '', jQuery('.content'));
	// Получить новости слева
	workwithdb('get_news_left', 3, '', '', jQuery('#left_block'));
	// Получить слайдер
//				var animate = 3000;
//				jQuery("#top_menu").animate({opacity: 1}, animate, function(){
	workwithdb('get_slider', '', '', '', jQuery('#fpss-outer-container'));					
//				});

	// Получить заглавия блоки
//	workwithdb('get_blocks', 'site_content', 0, '', jQuery('.medium_right'));
	// Получить категории
//	workwithdb('get_selected', '', 0, 0, jQuery('.gridView'));
	// Получить категории
//	workwithdb('get_news', 2, '', '', jQuery('.news_left'));
	// Подсветка стрелки с подробностями о товаре при наведении мыши

	// Получить страницы меню
	jQuery('.menu_top, .dir').click(function(){
		var block = jQuery(this).attr('id');
		if ((block != 'price_list')&&(block != 'news')) workwithdb('get_block', '', block, '', jQuery('.content'));
		else if (block == 'price_list') workwithdb('get_price_list', '', '', '', jQuery('.content'));
		else workwithdb('get_news', '', '', '', jQuery('.content'));
	});
	// Перейти по меню
	jQuery('.menu-title').live('click', function(){
		if (jQuery(this).html() == 'Література') {workwithdb('get_books', 'books', jQuery(this).attr('id'), '', jQuery('#fpss-outer-container'));}
		else workwithdb('get_content', 'category', jQuery(this).attr('id'), '', jQuery('#fpss-outer-container'));
	});
	// Перейти по баннеру
	jQuery('.joo-bot-a').live('click', function(){
		workwithdb('get_content', 'banners', jQuery(this).attr('id'), '', jQuery('#fpss-outer-container'));
	});
	// Перейти по слайдеру
	jQuery('.slide').live('click', function(){
		workwithdb('get_content', 'slider', jQuery(this).attr('id'), '', jQuery('#fpss-outer-container'));
	});
	// Перейти по новости
	jQuery('.nsp_header').live('click', function(){
		workwithdb('get_content', 'news', jQuery(this).attr('id'), '', jQuery('#fpss-outer-container'));
	});
	// Перейти по книге
	jQuery('.div_book').live('click', function(){
		workwithdb('get_content', 'books', jQuery(this).attr('id'), '', jQuery('#fpss-outer-container'));
	});	
	// Перейти по тегам новостей
	jQuery('.tag').live('click', function(){
		workwithdb('get_news', jQuery(this).html(), '', '', jQuery('.content'));
	});
	// Get Good
	jQuery('.title, .more_g, .prev_g, .next_g').live('click', function(){
		 if (jQuery(this).attr('id') != 0) workwithdb('get_good', '', jQuery(this).attr('id'), '', jQuery('.content'));
	});
	// Navigation
	jQuery('.s_nav').live('click', function(){
		if (pg == '') pg = 1;
		pg_old = pg;
		if (jQuery(this).attr('id') == 'prev_s') pg = pg*1 - 10;
		if (jQuery(this).attr('id') == 'prev') pg = pg*1 - 1;
		if (jQuery(this).attr('id') == 'next') pg = pg*1 + 1;
		if (jQuery(this).attr('id') == 'next_s') pg = pg*1 + 10;
		if (pg > jQuery('#page_num').html()*1) pg = jQuery('#page_num').html()*1;
		if (pg < 1) pg = 1;		
		jQuery("#page").val(pg);
		if (pg != pg_old) {
			mode = pg+';' +pp;
			workwithdb('get_goods', jQuery('#sort_list').val(), category, mode, jQuery('.content'));
		}
	});
	jQuery("#page").live('keydown', function(e){
//		var unicode = event.charCode ? event.charCode : event.keyCode;
		var code = e.which;
		if (code == '13') {
			pg = jQuery("#page").val();
			if (pg > jQuery('#page_num').html()*1) pg = jQuery('#page_num').html()*1;
			if (pg < 1) pg = 1;		
			jQuery("#page").val(pg);
			mode = pg+';' +pp;
			workwithdb('get_goods', jQuery('#sort_list').val(), category, mode, jQuery('.content'));
		}
	});
	jQuery('#sort_list').live('change', function(){
		pg = 1;
		mode = pg+';' +pp;
		workwithdb('get_goods', jQuery('#sort_list').val(), category, mode, jQuery('.content'));
	});	

	jQuery('.m_span').live('click', function(){	
        var container = jQuery(this).parent('li').parent('ul').children('li').children('ul');
		for(var i=0;i<container.size();i++) {
			container.eq(i).css("display", "none");
		}
        var container = jQuery(this).parent('li').parent('ul').children('li').children('span');
		for(var i=0;i<container.size();i++) {
			container.eq(i).removeClass("active");
		}
        var container = jQuery(this).parent('li').children('ul').children('li').children('span');
		for(var i=0;i<container.size();i++) {
//			container.eq(i).removeClass("active2");
			container.eq(i).removeClass("active");
		}
		jQuery(this).parent('li').children('ul').css("display", "block");
		jQuery(this).toggleClass("active");
		category = jQuery(this).attr('id');
		workwithdb('get_category', '', category, '', jQuery('.content'));
		return false;
    });

	jQuery('.a_arrow').live('click', function(){	
	    var container = jQuery(this).parent('.g_title').parent('.properties').parent('.goods_item').parent('.goods').children('.goods_item');
		for(var i=0;i<container.size();i++) {
			container.eq(i).children('.properties').children('.g_text').children('.p_descript').css("height", "12px");
		}
//		jQuery('#response').html(jQuery(this).parent('p').parent('.g_title').html());
		jQuery(this).parent('.g_title').parent('.properties').children('.g_text').children('.p_descript').css("height", "auto");
    });

	jQuery('.i_span, .more').live('click', function(){	
        var container = jQuery(this).parent('li').parent('ul').children('li').children('span');
		for(var i=0;i<container.size();i++) {
//			container.eq(i).removeClass("active2");
			container.eq(i).removeClass("active");
		}
//		jQuery(this).toggleClass("active2");
		jQuery(this).toggleClass("active");
		category = jQuery(this).attr('id');
		workwithdb('get_goods', '', category, '', jQuery('.content'));
		return false;
    });
	// Выбор особых товаров: "Новинки", "Скидки", "Идеальная покупка".
	jQuery('.special_g, .lable').live('click', function(){	
		category = jQuery(this).attr('id');
		workwithdb('get_goods', '', category, '', jQuery('.content'));
	});

	jQuery('.li_content').live('click', function(){
//		jQuery('#response').html('');
		category = 0;
		var container = jQuery('.m_span');
		for(var i=0;i<container.size();i++) {
			container.eq(i).removeClass("active");
			container.eq(i).parent('li').children('ul').css("display", "none");
			container.eq(i).parent('li').children('ul').children('li').children('span').removeClass("active2");
			if (container.eq(i).attr('id') == jQuery(this).attr('id')) {
				container.eq(i).toggleClass("active");	
				container.eq(i).parent('li').children('ul').css("display", "block");
				container.eq(i).parent('li').children('ul').children('li').children('span').eq(0).toggleClass("active2");
				category = container.eq(i).parent('li').children('ul').children('li').children('span').eq(0).attr('id');
			}
		}
//		jQuery('#response').html(category);
		if (category > '')	{workwithdb('get_goods', '', category, '', jQuery('.content'));}
//		return false;
    });

});

// Работа с базой данных
function workwithdb(wstr, table, id, mode, element){
	var datastr = 'WhatDo='+wstr+'&Table='+table+'&ID='+id+'&Mode='+mode;
	jQuery("#load_img").show();
	jQuery.ajax({
		type: "POST",
		url: "admin/workwithdb.php",
		data: datastr,
		cache: false,
		success: function(html){
//			html = str_replace (repl, patt, html);
			if (wstr != 'get_blocks') {
//				if (wstr == 'get_news_left') element.after(html);
//				else element.html(html);
				element.html(html);
			}
			else {
				var site = eval("(" + html + ")");
				var I = 0;
				while(I<site.length) {
					if ((mode == '')||(mode == 0)) jQuery('#'.concat(site[I])).html(site[I+1]);
					else element.html(site[I+1]);
				I = I + 2;
				}
				// Инициализация слайдера
			}
//			pg_old = jQuery("#page").val();
//			if (pg_old == '') pg_old = 1;
//			get_goods
			if (wstr == 'get_goods') {
				var params = {changedEl: "#sort_list", scrollArrows: false};
				cuSel(params);
			}
//			get_news
			if ((wstr == 'get_news')&&(id == '')) {
				jQuery(".short:odd").toggleClass("colors");
//				jQuery(".short:even").toggleClass("colors");
			}
//			get_cart			
			if (wstr == 'get_cart') {
				var params = {changedEl: "#order_list", scrollArrows: false};
				cuSel(params);
			}
//			get_good		
			if (wstr == 'get_good') {
				jQuery('#photos').galleryView({
					panel_width: 298,
					panel_height: 318,
					frame_width: 70,
					frame_height: 91,
					transition_speed: 200,
					transition_interval: 0
				});
				hs.graphicsDir = 'js/highslide/graphics/';
				hs.wrapperClassName = 'wide-border';				
			}	
//			get_menu
			if (wstr == 'get_menu') {
				var megamenu = new jaMegaMenuMoo ('joo-megamenu', {
					'bgopacity': 0, 'delayHide': 300, 'slide': 1, 'fading': 1,
					'direction':'down', 'action':'mouseover', 'tips': false,
					'duration': 300, 'hidestyle': 'fastwhenshow'});			
			}
			if (wstr == 'get_content') {
//				jQuery(".logo").animate({opacity: 1}, 500, function(){
					var height = jQuery('#fpss-outer-container').height();
					jQuery('.joo-box-tl').height(height);
//					if (jQuery('.joo-box-tl').height() > jQuery('#fpss-outer-container').height()) {jQuery('.joo-box-tl').height() = jQuery('#fpss-outer-container').height()};
//					jQuery('.phone').html(height);
//				});						
				jQuery("#fpss-outer-container").children('img').load(function(){
					var height = jQuery('#fpss-outer-container').height();
					jQuery('.joo-box-tl').height(height);
				});
				jQuery("#fpss-outer-container").children('center').children('img').load(function(){
					var height = jQuery('#fpss-outer-container').height();
					jQuery('.joo-box-tl').height(height);
				});				
				
			}
			if (wstr == 'get_books') {
					var height = jQuery('#fpss-outer-container').height();
					jQuery('.joo-box-tl').height(height);
			}			
			if (wstr == 'get_slider') {
/*				var fpssPlayText = "Play";
				var fpssPauseText = "Pause";
				var navTrigger = "click";
				var crossFadeDelay = 6000;
				var crossFadeSpeed = 1000;
				var fpssLoaderDelay = 800;
				var autoslide = true;
				var CTRtext_effect = true;
				var CTRtransitionText = 1000;*/
//				document.all.page.src=document.location.concat('js/mootools-fpss-comp.js');
//				eval("js/mootools-fpss-comp.js")				
				jQuery.getScript('js/mootools-fpss-comp.js', function() {});

//		document.write('<script type="text/javascript" src="js/mootools-fpss-comp.js"></script>');		
			}
			jQuery("#load_img").hide();		
		}
	});
}
// Проверка ввода данных
function check_empty(mode){
	var empty = false;
	if (mode == 'send') {
		if (jQuery('.in_data').eq(0).val() == '') {empty = true;}
		if (jQuery('.in_data').eq(1).val() == '') {empty = true;}
		if (jQuery('.in_data').eq(2).val() == '') {empty = true;}
		if (jQuery('.in_data').eq(3).val() == '') {empty = true;}
		if (jQuery('.in_data').eq(4).val() == '') {empty = true;}
	}
	if (mode == 'reg') {
		if (jQuery('.reg_data').eq(0).val() == '') {empty = true;}
		if (jQuery('.reg_data').eq(1).val() == '') {empty = true;}
		if (jQuery('.reg_data').eq(2).val() == '') {empty = true;}
		if (jQuery('.reg_data').eq(3).val() == '') {empty = true;}
	}
	return empty;
}
// str_replace
function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
