$( function () {
	
// Resize the home column heading text so that it always fits inside the designated area...
	$( '.home-column-heading' ).fitText( 0.9 );


// Add a class to the last navigation item
	$( '#navigation li:last' ).addClass( 'last' );


// Homepage slideshow
	// First let's center the text vertically, while the slideshow is invisible...
	$( '#homepage-slideshow' ).css( 'visibility', 'hidden' );
	
	$( '#homepage-slideshow .slide .slide-content' ).each(
		function( index, paragraph ) {
	    	height = $( paragraph ).height();
			$( paragraph ).css( 'paddingTop', (215-height)/2 + 'px' );
	});
	
	// ...now, let's prepare the slideshow...
	$( '#homepage-slideshow' ).slideshow({
	    slide: '.slide',
	    navigation: '#slideshow-nav a',
	    navigationSelectedClass: 'selected',
	    slideshow: true,
	    slideshowSpeed: 5000,
	    startFromIndex: 1,
	    transition: 'fade',
	    transitionSpeed: 400,
	    mouseOverFreeze: true
	});
	
	// ...and, finally, show the slideshow now that everything's in place.
	$( '#homepage-slideshow' ).css( 'visibility', 'visible' );


// PAGES MENU
	// Hide all children lists, except for the ones that are above the current page and the one directly below it
	$ ( '#menu .children' ).not( $ ( '.current_page_item' ).parents() ).not( $ ( '.current_page_item .children:first' ) ).css( 'display', 'none' );


// PRODUCTS MENU
	// Check the cookies and if this is a product page that is being accessed from a category, mark the right menu item as "legitimate" parent
	if ( LOC_COOKIE == 'product_category' ) { 
		$ ( '#product-menu a[href="' + CAT_COOKIE + '"]' ).closest( 'li' ).addClass( 'legitimate' );
	} else {
		//Otherwise, mark the first parent as "legitimate"
		$ ( '#product-menu .current-menu-parent:first' ).addClass( 'legitimate' );
	}
	
	// Now hide all children lists, except for the ones that are above the current page and the one directly below it
	$ ( '#product-menu .children, #product-menu .sub-menu' ).not( $ ( '.current-cat, .current-menu-item, .current-menu-parent.legitimate' ).parents() ).not( $ ( '.current-cat .children:first, .current-menu-item .sub-menu:first, .current-menu-parent.legitimate .sub-menu:first, .current-product-ancestor.legitimate .sub-menu:first' ) ).css( 'display', 'none' );

	
// SLIDER PAGES EFFECTS
	// Hide all answers
	$ ( '.slider-page blockquote' ).css( 'display', 'none' );
	
	// Show an answer on question click
	$ ( '.slider-page h2' ).click( function(){
		
		if ( $ ( this ).next( 'blockquote' ).hasClass('open') ) {
		
			$ ( this ).next( 'blockquote' ).slideUp();
			$ ( this ).next( 'blockquote' ).removeClass('open')

		} else {
			
			$ ( '.slider-page blockquote' ).not( $ ( this ).next( 'blockquote' ) ).slideUp();
			$ ( this ).next( 'blockquote' ).slideDown();
			$ ( this ).next( 'blockquote' ).addClass('open');	
		}
	});


// READ MORE
	$( '.full-version' ).hide();
	
	$( '.more-link' ).click( function() {
		
		$( '.short-version' ).slideUp( 200 );
		$( '.full-version' ).slideDown( 250 );
		
		return false;
	});

	
// TABLES
	$ ( '.short-version table tr:first, .full-version table tr:first' ).addClass('first');


// Contact form validation and AJAX submission
	$( '.messages' ).css('display', 'none');
	
    $( '.contact-form:not(#career-form)' ).formValidation({ 
	
    	callback: function ( $_error_elements ) {
			
			$('.messages').fadeOut();

            if ( $_error_elements ) {
	
                $( '.messages' ).addClass( 'failure' ).text( 'Συμπληρώστε σωστά όλα τα πεδία!' );
				$('.messages').fadeIn();
				
                return false;
            }

			$('.messages').fadeOut( 'fast', function() { 
				$( '.messages' ).removeClass( 'failure' ).html( '<img src = "' + TEMPLATE_URL + '/images/ajax-loader-small.gif" atl = "Loading..." /> Παρακαλώ περιμένετε... ' ).fadeIn(); 
			});

            $.ajax({
                url: BLOG_URL + '/wp-admin/admin-ajax.php',
                type: 'GET',
                data: 'action=contact_form&' + $( '.contact-form' ).serialize(),
                dataType: 'text', 
                success: 
                    function ( results ) {
                        if ( results == 'success' ) {
                            $( '.contact-form input' ).attr( 'value', '' );
                            $( '.contact-form .submit' ).attr( 'disabled', 'disabled' ).fadeOut( 'fast' );
							$('.messages').fadeOut( 'fast', function() {
								 $( '.messages' ).addClass( 'success' ).text( 'Το μήνυμά σας έχει σταλεί!' ).fadeIn();
							});
		                } else {
							$('.messages').fadeOut( 'fast', function() {
								 $( '.messages' ).addClass( 'failure' ).text( 'Παρακαλώ δοκιμάστε ξανά.' ).fadeIn();
							});
							
                        }
                    },
                error: 
                    function () {
                        alert( 'We be finding dem errors.' );
                    }
            });

            return false;
        }
    });

// Product finder
	var loader = '<div style = "text-align: center; padding-top: 3px"><img src = "' + TEMPLATE_URL + '/images/ajax-loader.gif" alt = ""/></div>';
	var initDropdown = $ ( '#product-finder .button:first' ).text();
	
	$ ( '#product-finder .button').click ( function() {
		
		if ( ! $ ( this ).hasClass( 'current-method' ) ) {
			
			$ ( '#product-finder .finder-options-container').slideUp('fast');
			$ ( '#product-finder .current-method').removeClass( 'current-method' );
			
			$ ( this ).addClass( 'current-method' );
			$ ( this ).next( '.finder-options-container' ).slideDown('fast');

			return false;
			
		} else {
			
			$ ( this ).next( '.finder-options-container' ).slideUp('fast');
			$ ( this ).removeClass( 'current-method' );
			
			return false;
		}
	});
	
	$ ( '#product-finder .finder-options a').click ( function() { 
		$ ( '#product-finder .finder-options-container').slideUp('fast');
		$ ( '#product-finder .finder-options a' ).removeClass( 'active' );
		$ ( this ).addClass( 'active' );
		$ ( '#product-finder .button ').text( initDropdown );
		$ ( this ).closest( '.finder-method' ).find( '.button ').text( $(this).text() );
		$ ( '#product-finder .current-method').removeClass( 'current-method' );
		$ ( '#product-finder .active-method' ).removeClass('active-method');
		$ ( this ).closest( '.finder-method' ).find( '.button ').addClass('active-method');
		$ ( '#product-finder-results' ).html( loader );

		//AJAX
		$.ajax({
            url: BLOG_URL + '/wp-admin/admin-ajax.php',
            type: 'GET',
            data: 'action=product_finder&lang=' + LANGUAGE + '&taxonomy=' + $ ( this ).attr('name') + '&term=' + $ ( this ).attr('id').substring( $ ( this ).attr('id').indexOf('-')+1 ),
            dataType: 'html',
            success: 
                function ( results ) {
					$ ( '#product-finder-results' ).html( results );
                },
            error: 
                function () {
                    $ ( '#product-finder-results' ).text( 'problem' );
                }
        });
		
		return false;
		
	});
	
	// Searching by name
	$ ( '#product-finder #search-by-name input').focus ( function() {
		$ ( '#product-finder .current-method').removeClass( 'current-method' ); //Clear all buttons that are current selections
		$ ( '#product-finder .finder-options-container').slideUp('fast');
		
	});
	
	$ ( '#product-search-submit' ).click ( function() { 
			$ ( '#product-finder #search-by-name').submit();
			
			return false;
		});
	
	$ ( '#product-finder #search-by-name').submit ( function() { 
		$ ( '#product-finder .finder-options-container').slideUp('fast');
		$ ( '#product-finder .active-method' ).removeClass( 'active-method' );
		$ ( this ).closest( '.finder-method' ).addClass( 'active-method' );	
		$ ( '#product-finder-results' ).html( loader );

		//AJAX
		$.ajax({
            url: BLOG_URL + '/wp-admin/admin-ajax.php',
            type: 'GET',
            data: 'action=product_finder&keyword=' + encodeURIComponent ( $ ( this ).find( 'input' ).attr( 'value' ) ),
            dataType: 'html', 
            success: 
                function ( results ) {
					$ ( '#product-finder-results' ).html( results );
                },
            error: 
                function () {
                    $ ( '#product-finder-results' ).text( 'problem' );
                }
        });
		
		return false;
		
	});

	
// FORM DROPDOWNS
	$ ( '.dropdown a' ).click( function() {
		
		if ( $ ( this ).closest( '.dropdown' ).find('.dropdown-options').hasClass('open') ) {
			$ ( this ).closest( '.dropdown' ).find('.dropdown-options').slideUp('fast').removeClass('open');
			$ ( this ).closest( '.dropdown' ).find( '.dropdown-selection').removeClass('blur');
		} else {
			$ ( this ).closest( '.dropdown' ).find('.dropdown-options').slideDown('fast').addClass('open');
			$ ( this ).closest( '.dropdown' ).find( '.dropdown-selection').addClass('blur');
		}
		
		return false;
	});
	
	$ ( '.dropdown-options a' ).click( function() {
		
		$ ( '.dropdown-options a' ).removeClass( 'selected' );
		$ ( this ).addClass('selected');
		$ ( this ).closest( '.dropdown' ).find('.dropdown-options').slideUp('fast').removeClass('open');
		$ ( this ).closest( '.dropdown' ).find( '.dropdown-selection').text( $ ( this ).text() );
		$ ( this ).closest( '.dropdown' ).find( 'input' ).val( $ ( this ).text() );
		return false;
	});
	
// CAREER FORM VALIDATION
	$( '#career-form' ).formValidation({
         
		callback: function ( $_error_elements ) {

					$( '.messages' ).fadeOut();

		            if ( $_error_elements ) {

		                $( '.messages' ).addClass( 'failure' ).text( 'Συμπληρώστε σωστά όλα τα πεδία!' );
						$('.messages').fadeIn();

		                return false;
		            }
		
					$('.messages').fadeOut( 'fast', function() { 
						$( '.messages' ).removeClass( 'failure' ).html( '<img src = "' + TEMPLATE_URL + '/images/ajax-loader-small.gif" atl = "Loading..." /> Παρακαλώ περιμένετε... ' ).fadeIn(); 
					});

		     }
	});

});

// CAREER FORM SPECIFIC JS
function form_submission_aftermath ( status, message ) {

	if ( status == 4 ) {
        $( '#career-form input' ).attr( 'value', '' );
        $( '#career-form .submit' ).attr( 'disabled', 'disabled' ).fadeOut( 'fast' );
		$('.messages').fadeOut( 'fast', function() {
			 $( '.messages' ).addClass( 'success' ).text( message ).fadeIn();
		});
    } else {
		$('.messages').fadeOut( 'fast', function() {
			 $( '.messages' ).addClass( 'failure' ).text( message ).fadeIn();
		});
	}

}
