/**
 * Kai paspaudi submeniu elementa (esant produktu isvedime) flashe isvedamas pasirinktas produktas
 */
function onProductMenuPress( item_id ) {
	var flash = document.getElementById( 'flash_products' );
	
	if ( flash != 'undefined' ) {
		flash.changeActiveItem( item_id );
		$('#submenu_'+submenuActive).removeClass('active');
		$('#submenu_'+item_id).addClass('active');
		submenuActive = item_id;
	}
}

/**
 * Kai flashe paspaudziamas produktas, submeniu jis pazymimas kaip aktyvus
 */
function changeActiveItem( item_id ) {

	$('#submenu_'+submenuActive).removeClass('active');
	$('#submenu_'+item_id).addClass('active');
	submenuActive = item_id;
}


function toggleList( number, txt ) {
	
	if ( activeNumber ) {
		$( '#'+activeTxt+'_dd_'+activeNumber ).slideUp();
		
		$( '#'+activeTxt+'_dt_'+activeNumber ).attr('class', '');
	}
	if ( activeNumber != number  || activeTxt != txt ) {
		$( '#'+txt+'_dd_'+number ).slideDown();
		$( '#'+txt+'_dt_'+number ).addClass('active');
		activeTxt = txt;
		activeNumber = number;
	} else {
		if ( activeNumber == number  && activeTxt == txt ) {
			$( '#'+txt+'_dt_'+number ).addClass('ov');
		}
		activeTxt = false;
		activeNumber = false;
	}
}


function validateInput( value, error_id ) {
	
	if ( value.length == 0 ) {
		
		$( '#' + error_id ).addClass( 'error' );
		
		return false;
	} else {
		
		$( '#' + error_id ).removeClass( 'error' );
		
		return true;
	}
}

function validateDate( value1, value2, value3, error_id ) {
	
	var value1 = parseInt( value1 );
	var value2 = parseInt( value2 );
	var value3 = parseInt( value3 );
	
	if ( isNaN( value1 ) ) {
		
		value1 = 0;
	}
	
	if ( isNaN( value2 ) ) {
		
		value2 = 0;
	}
	
	if ( isNaN( value3 ) ) {
		
		value3 = 0;
	}
	
	if ( value1 <= 0 || value1 > 31 || value2 <= 0 || value2 > 12 || value3 <= 1930 || value3 > 2003 ) {
		
		$( '#' + error_id ).addClass( 'error' );
		
		return false;
	} else {
		
		$( '#' + error_id ).removeClass( 'error' );
		
		return true;
	}
}

function validateEmail( value, error_id ) {
	
	if ( value.length == 0 ) {
		
		$( '#' + error_id ).addClass( 'error' );
		
		return false;
	} else {
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		if ( reg.test( value ) === false ) {
			
			$( '#' + error_id ).addClass( 'error' );
			
			return false;
		} else {
			
			$( '#' + error_id ).removeClass( 'error' );
			return true;
		}
	}
}

function tellAFriend() {
	
	var name = $( '#taf_name' ).attr( 'value' );
	var surname = $( '#taf_surname' ).attr( 'value' );
	var email = $( '#taf_email' ).attr( 'value' );
	var friends_name = $( '#taf_friends_name' ).attr( 'value' );
	var friends_surname = $( '#taf_friends_surname' ).attr( 'value' );
	var friends_email = $( '#taf_friends_email' ).attr( 'value' );
	
	var n = validateInput( name, 'taf_name_div' );
	var s = validateInput( surname, 'taf_surname_div' );
	var e = validateEmail( email, 'taf_email_div' );
	var fn = validateInput( friends_name, 'taf_friends_name_div' );
	var fs = validateInput( friends_name, 'taf_friends_surname_div' );
	var fe = validateEmail( friends_email, 'taf_friends_email_div' );
	
	if ( s & n & e & fn & fs & fe ) {
		
		$( '#tell_a_friend_form' ).submit();
		return true;
	} else {
		
		$( '#st_error' ).slideDown( 500 );
		return false;
	}
	
	return false;
}

function submitForm( form_id, response_container ) {

	action = $( '#' + form_id ).attr( 'action' );
	fields = $( '#' + form_id ).serialize();
	
	$.post(
		action,
		fields,
		function( data, textStatus ) {
		
			if ( textStatus == 'success' ) {
			
				$( '#' + response_container ).html( data );
				
			}
		},
		'html'
	);
};
function loadBlock( post_url, response_container ) {
	
	$.post(
		post_url,
		function( data, textStatus ) {
		
			if ( textStatus == 'success' ) {
			
				$( '#' + response_container ).html( data );
			}
		},
		'html'
	);
};


function showDecoration() {
	$( '.banner_left' ).css({
		display: 'block',
		opacity: 0
	}).animate({
		opacity: 1
	}, 1400 );
}

function openBottomMenuItem( link, scrollToMeniu ) {
	if ( $( '#bottom_menu_popup' ).hasClass( 'register_popup' ) ) {
		scrollToMeniu = false;
	}
	if ( typeof( scrollToMeniu ) == 'undefined' ) {
		scrollToMeniu = true;
	}
	
	$.get( link, function( data ) {
		
		$( '#bottom_menu_popup_inner' ).html( data );
		$( '#bottom_menu_popup' ).show();
		//alert($( window ).width() / 2 - 250 + 'px');
		$( '#bottom_menu_popup' ).css( 'left', $( '#container' ).width() / 2 - 250 + 'px' );
		
		if ( scrollToMeniu ) {
			$.scrollTo( '#bottom_menu_popup', 500 );
		}
	});
}

$( document ).ready( function() { 
	
	var scrollbar_bar = $( '#scrollbar_bar' );
	
	if ( scrollbar_bar ) {
		
		var scrollbar_parent = scrollbar_bar.parent();
		
		// dummy height 743px, default 537px
		var scroll_area = $( '.scroll' );
		
		// position where you've pressed mouse button
		window.click_position = 0;
		window.click_obtained = false;
		window.scroll_min = scrollbar_bar.offset().top;
		window.scroll_parent_top = scrollbar_parent.offset().top;
		window.scroll_max = window.scroll_min + 111;
		window.scroll_height = scroll_area.height() - 537;
		window.scroll_step = window.scroll_height / 111.0;
		window.scroll_top_pos = scrollbar_bar.offset().top;
		
		documentMouseMove = function(e) {
			
		    var position = window.scrollbar_offset + e.pageY - window.click_position;
			
			if ( window.click_obtained == true ) {
			
				if ( position >= 0 && position <= 111 ) {
					
					scrollbar_bar.css( 'top', ( 1 + position ).toString() + 'px' );
					scroll_area.css( 'margin-top', '-' + ( position * window.scroll_step ).toString() + 'px' );
				} else if ( position < 0 ) {
					
					scrollbar_bar.css( 'top', '1px' );
					scroll_area.css( 'margin-top', '0px' );
				} else if ( position > 111 ) {
					
					scrollbar_bar.css( 'top', '112px' );
					scroll_area.css( 'margin-top', '-' + window.scroll_height.toString() + 'px' );
				}
			}
		}
		
		// on mouse down event
		scrollbar_bar.mousedown( function(e) {
			
			if ( window.click_obtained == false ) {
			
				window.click_obtained = true;
				window.click_position = e.pageY;
				window.scrollbar_offset = scrollbar_bar.offset().top - window.scroll_top_pos;
				$('#a1').text(window.scrollbar_offset);
				$( document ).mousemove( function(e) {
					
					documentMouseMove(e);
				} );
			}
			
			return false;
		} );
	
		// on mouse up	
		scrollbar_bar.mouseup( function(e) {
			
			$(document).trigger('mouseup', e);
		} );
		
		// on mouse up (document)
		$(document).mouseup( function(e) {
			
			if ( window.click_obtained == true ) {
				
				window.click_obtained = false;
				window.moving = false;
				$( document ).mousemove( undefined );
			}
		} );
		
		scrollbar_bar.mousemove( function(e) {
			
			if ( window.moving != true ) {
				
				$(document).trigger('mousemove', e);
				window.moving = true;
			}
		} );
		
		contentUp = function() {
	
			if ( scrollbar_bar.offset().top - 20 >= window.scroll_top_pos ) {
				scrollbar_bar.css( 'top', ( parseInt( scrollbar_bar.css( 'top' ) ) - 20 ).toString() + 'px' );
				scroll_area.css( 'margin-top', '-' + ( parseInt( scrollbar_bar.css( 'top' ) ) * window.scroll_step ).toString() + 'px' );
			} else {
				scrollbar_bar.css( 'top', '0px' );
				scroll_area.css( 'margin-top', '-' + ( parseInt( scrollbar_bar.css( 'top' ) ) * window.scroll_step ).toString() + 'px' );
			}
		}
		
		contentDown = function() {
			
			if ( scrollbar_bar.offset().top + 20 <= window.scroll_top_pos + 111 ) {
				scrollbar_bar.css( 'top', ( parseInt( scrollbar_bar.css( 'top' ) ) + 20 ).toString() + 'px' );
				scroll_area.css( 'margin-top', '-' + ( parseInt( scrollbar_bar.css( 'top' ) ) * window.scroll_step ).toString() + 'px' );
				
			} else {
				scrollbar_bar.css( 'top', '111px' );
				scroll_area.css( 'margin-top', '-' + ( parseInt( scrollbar_bar.css( 'top' ) ) * window.scroll_step +((isIE6 || isIE7) ? 85 : 0) ).toString() + 'px' );
			}
		}
	}
} );

/**
 * Actimel kids GA trackinimas
 */
function trackKidsGA( type ) {
	
	try {
		pageTracker._trackPageview(type);
	} catch(e){};
	
}