function remove_root_url( url ) {
	return url.replace(root_url,'');
}

function submitForm( form_id, response_container ) {
	form_el = $('#'+form_id);
	form_data = form_el.serialize();
	$.ajax({
		type: "POST",
		url: remove_root_url(form_el.attr('action')),
		data: form_data,
		cache: false,
		success: function(html){
			$('#'+response_container).html(html);
		}
	});
	return false;
}

function getBlock( block_url, response_container, a_sync, branch ) {
	if ( null == a_sync ) {
		a_sync = true;
	}
	$.ajax({
		type: "GET",
		async: a_sync,
		url: remove_root_url(block_url),
		data: { branch: branch },
		cache: false,
		success: function(html){
			$('#'+response_container).html(html);
		}
	});
	return false;
}

function ask_us_container( block_url, branch ) {
	$( '.overlay' ).css('opacity','0.4').fadeIn( 200 );
	el = $('#ask_us_container');
	
	if ( el.size() == 0 ) {
		
		var new_div = '<div id="ask_us_container" class="popups_holder"></div>';
		$( 'body' ).append( new_div );
		el = $( '#ask_us_container' );
	}

	if ( el.css('display') == 'none' ) {
		getBlock( block_url, 'ask_us_container', false, branch );
	}

	if ( is_main ) {
	
		var top_pos = $( '.main .ask_us' ).offset().top - 250;
	} else {

		var top_pos = 0;
	}
	el.css( 'top', top_pos + 'px' ).show();

	$( 'html, body' ).animate( { scrollTop: el.offset().top }, 700 );
	return false;
}

function ask_us() {
	
	return ask_us_container( root_url + '?act=js/ask_us' );
}

function faq_container( block_url, branch ) {
	$( '.overlay' ).css('opacity','0.4').fadeIn( 200 );
	el = $('#faq_container');
	
	if ( el.size() == 0 ) {
		
		var new_div = '<div id="faq_container" class="faq_container_pos" style="display:none;"></div>';
		$( '.page .container' ).prepend( new_div );
		el = $( '#faq_container' );
	}
	
	if ( el.css('display') == 'none' ) {
		getBlock( block_url, 'faq_container', false );
	}
	el.show();
	$( 'html, body' ).animate( { scrollTop: el.offset().top }, 600 );
	return false;
}

function pollSubmit( form ) {
	
	form = $( form );
	$( '#poll_block *' ).removeClass( 'error' );
	
	$.ajax( {
		type : 'post',
		url: form.get(0).action,
		data : form.serializeArray(),
		dataType: 'json',
		success : function( response ) {
			
			if ( response.error ) {
				
				alert( response.error_msg );
				
				return false;
			}
			
			if ( response.answers ) {
				
				$( '#poll_block .poll' ).addClass( 'error' );
				
				return false;
			}
			
			if ( response.custom ) {
				
				$( '#poll_block textarea' ).addClass( 'error' );
				
				return false;
			}
			
			if ( response.success ) {
			
				$( '#poll_block' ).replaceWith( response.success );
			}
		}
	} );
}

function checkPollItem( id ) {
	
	var element = $( '#' + id );
	
	if ( element ) {
		
		element.attr( 'checked', true );
	}
}

function uselessData( is_good, item_id ) {
	
	$.ajax({
		url: root_url + '?act=useless/send',
		data: { good: is_good, item_id: item_id },
		dataType: 'json',
		success: function( data ) {
			
			if ( data.step == 1 ) {
				
				$( '#useless_info_block' ).slideUp( 500, function() {
					
					$( '#useless_info_form' ).slideDown( 500 );
				});
			}
			
			if ( data.step == 2 ) {
				
				$( '#useless_info_text' ).html( data.text );
				$( '#useless_info_block' ).slideUp( 500, function() {
					
					$( '#useless_info_text' ).slideDown( 500 );
				});
			}
		}
	});
}

function uselessExplain( item_id ) {
	
	var info = $( '#useless_info_textarea' ).attr( 'value' );
	
	if ( info.length < 5 ) {
		
		$( '#useless_info_textarea' ).parent().addClass('error');
		return false;
	} else {
		
		$.ajax({
			url: root_url + '?act=useless/explain',
			data: { text: info, item_id: item_id },
			dataType: 'json',
			success: function( data ) {
				
				$( '#useless_info_text' ).html( data.text );
				$( '#useless_info_form' ).slideUp( 500, function() {
					
					$( '#useless_info_text' ).slideDown( 500 );
				});
			}
		});
	}
}

var menu_item = {};

function menuOver( id ) {

	$( '#mm_item_' + id ).addClass( 'over' );
	
	clearTimeout( menu_item[ id ] );
	
	menu_item[ id ] = setTimeout( function() {
	
		if ( $.browser.msie && $.browser.version < 9 ) {
			$( '#menu_cont_' + id ).fadeIn( 0 );
		} else {
			$( '#menu_cont_' + id ).fadeIn( 200 );
		}
		dhtmlPosition( id );
	}, 300 );
}

function menuOut( id ) {

	$( '#mm_item_' + id ).removeClass( 'over' );
	
	clearTimeout( menu_item[ id ] );
	
	menu_item[ id ] = setTimeout( function() {
		$( '#menu_cont_' + id ).hide();
	}, 300 );
}

function resizeSlide() {
	
	var window = $( '.title_offers' ).width();
	var min_w = 976;
	if ( window <= min_w ) {
		
		$( '.slide' ).width( min_w );
	} else {
		
		$( '.slide' ).width( window );
	}
}
$( document ).ready( function() { 
	
	$('.text').find('.btn_vw').wrapInner('<span></span>');
	
	$( '.mouse_down' ).mousedown(
		
		function() { $( this ).addClass( 'on' ); }
	);
	$( '.mouse_down' ).mouseout(
		
		function() { $( this ).removeClass( 'on' ); }
	);
	$( '.mouse_down' ).mouseup(
		
		function() { $( this ).removeClass( 'on' ); }
	);

	$( '.mouse_down_in' ).mousedown(
		
		function() { $( this ).parents( '.group_item' ).addClass( 'on' ); }
	);
	$( '.mouse_down_in' ).mouseout(
		
		function() { $( this ).parents( '.group_item' ).removeClass( 'on' ); }
	);
	$( '.mouse_down_in' ).mouseup(
		
		function() { $( this ).parents( '.group_item' ).removeClass( 'on' ); }
	);
	
	$( '#container' ).css({ 'position': 'relative' });
	
	$( '.overlay' ).click( function() {
		
		$( '.popups_holder' ).hide();
		$( '#page_popup .pop_close' ).trigger( 'click' );
		
		$( '.overlay' ).fadeOut( 300 );
		
		setTimeout( function(){
			
			$( '.overlay' ).css('opacity','0');
		}, 300 );
	});
} );

function dhtmlPosition( id ) {
	
	var dhtml_padd_left = 0,
		mitem = $( '#mm_item_' + id ),
		dhtml = $( '#menu_dhtml_' + id );
	
	if ( dhtml.offset() ) {
		
		var mitem_left = mitem.offset().left,
			dhtml_left = dhtml.offset().left,
			mitem_right = mitem_left + mitem.width(),
			dhtml_right = dhtml_left + dhtml.width();
		
		if ( dhtml.parent().hasClass( 'not_pos' ) ) {
			
			// tikrinam kaire
			if ( ( mitem_left < dhtml_left+25 ) ) { // +25 - del seselio
				
				var dhtml_padd_left = 0 - ( dhtml_left - mitem_left + 30 );
				// tikrinam desine
				if ( dhtml.parent().parent().hasClass( 'mm_right' ) ) {
					
					var dhtml_padd_left = 0 - ( dhtml_right - mitem_right - 30 );
				}
				dhtml.parent().css( {'margin-left': dhtml_padd_left + 'px'} ).removeClass( 'not_pos' );
			// tikrinam desine
			} else if ( mitem_right > dhtml_right-25 ) { // -25 - del seselio
				
				var dhtml_padd_left = ( mitem_right - dhtml_right + 30 );
				dhtml.parent().css( {'margin-left': dhtml_padd_left*2 + 'px'} ).removeClass( 'not_pos' ).width( dhtml.parent().width()-dhtml_padd_left*2 );
			}
		}
	}
}

function filterShow() {
	
	$( '#link_hide' ).show();
	$( '#link_show' ).hide();
	$( '#phone_filter_block' ).slideDown( 600 );
}
function filterHide() {
	
	$( '#link_hide' ).hide();
	$( '#link_show' ).show();
	$( '#phone_filter_block' ).slideUp( 600 );
}

function showContent_lv1( id ) {
	
	$( '#lv1_cont_' + id ).toggle();
	$( '#lv1_ico_' + id ).toggleClass( 'arrow_down' );
}
function showContent_lv2( id ) {
	
	$( '#lv2_cont_' + id ).toggle();
	$( '#lv2_ico_' + id ).toggleClass( 'arrow_down' );
}
function showContent_lv3( id ) {
	
	$( '#lv3_cont_' + id ).toggle();
	$( '#lv3_ico_' + id ).toggleClass( 'arrow_down' );
}
function showContent_lv4( id ) {
	
	$( '#lv4_cont_' + id ).toggle();
	$( '#lv4_ico_' + id ).toggleClass( 'arrow_down' );
}
function scrollTo( elem ) {
	
	$( 'html, body' ).animate( { scrollTop: $( '.' + elem ).offset().top }, 1000 );
}

function addToCompare( el, type, ci, product_id ) {
	
	$.ajax( {
		type : 'post',
		url: el.attr( 'href' ),
		dataType: 'json',
		success : function( response ) {
			
			el.hide();
			el.parent().find( '.remove' ).show();
			
			$( '#compare_product_' + product_id ).find( '.add' ).hide();
			$( '#compare_product_' + product_id ).find( '.remove' ).show();
			
			if ( typeof( response.product_count ) != undefined ) {
				
				if ( response.product_count == 3 ) {
					
					$( '.compare_buttons' ).hide();
					
					if ( typeof( response.products ) != undefined ) {
						
						$.each( response.products, function( key, value ) {
							
							$( '#compare_product_' + value ).show();
							$( '#compare_product_' + value ).find( '.compare_link' ).show();
						});
					}
				} else if ( response.product_count < 2 ) {
					
					$( '.compare_buttons' ).show();
					$( '.compare_buttons' ).find( '.no_compare_link' ).show();
					$( '.compare_buttons' ).find( '.compare_link' ).hide();
				} else {
					
					$( '.compare_buttons' ).show();
					$( '.compare_buttons' ).find( '.no_compare_link' ).hide();
					$( '.compare_buttons' ).find( '.compare_link' ).show();
				}
			}
			
			updateCompareBlock( type, ci );
		}
	} );
}

function removeFromCompare( el, type, ci, product_id ) {
	
	$.ajax( {
		type : 'post',
		url: el.attr( 'href' ),
		dataType: 'json',
		success : function( response ) {
			
			el.hide();
			el.parent().find( '.add' ).show();
			
			$( '#compare_product_' + product_id ).find( '.add' ).show();
			$( '#compare_product_' + product_id ).find( '.remove' ).hide();
			
			if ( typeof( response.product_count ) != undefined ) {
				
				if ( response.product_count == 3 ) {
				
					$( '.compare_buttons' ).hide();
					
					if ( typeof( response.products ) != undefined ) {
						
						$.each( response.products, function( key, value ) {
							
							$( '#compare_product_' + value ).show();
							$( '#compare_product_' + value ).find( '.compare_link' ).show();
						});
					}
				} else if ( response.product_count < 2 ) {
					
					$( '.compare_buttons' ).show();
					$( '.compare_buttons' ).find( '.no_compare_link' ).show();
					$( '.compare_buttons' ).find( '.compare_link' ).hide();
				} else {
					
					$( '.compare_buttons' ).show();
					$( '.compare_buttons' ).find( '.no_compare_link' ).hide();
					$( '.compare_buttons' ).find( '.compare_link' ).show();
				}
			}
			
			updateCompareBlock( type, ci );
		}
	} );
}

function updateCompareBlock( type, ci ) {
	
	var timestamp = Number( new Date() );
	
	$.ajax( {
		type : 'get',
		url: root_url + '?act=compare/block&type=' + type + '&ci=' + ci + '&t=' + timestamp,
		success : function( response ) {
			
			$( '#compare_container' ).html ( response );
		}
	} );
}
