if (history.pushState) {
	window.onpopstate = function (e) {
		if (e.state && e.state.page && e.state.page != ''){
			window.location.href = e.state.page;
		}
	}
}

/*

 *      This function validates the text strings entered by the
 *      user in the text search box of the web site.
 *  The string must be at least 3 characters long and
 *  should not contain special characters such as '*', '[', etc.
 *  TODO: provide a way to internationalize javascript messages
*/
function validateTextSearched(textSearched, textInField) {

       if (textSearched == null) {
               alert('Η λέξη αναζήτησης πρέπει να έχει τουλάχιστον 3 χαρακτήρες');
               return false;
       }

       if (textSearched == textInField) {
           alert('Πρέπει να ορίσετε λέξη αναζήτησης');
           return false;
       }

       //Trim leading and trailing spaces
       textSearched = textSearched.replace(/^\s*/, "").replace(/\s*$/, "");

       //Check if lenght is less than 3 characters if there is a value
       //Must escape less than
       if (textSearched.length < 3){
               alert('Η λέξη αναζήτησης πρέπει να έχει τουλάχιστον 3 χαρακτήρες');
               return false;
       }

       //Search for invalid characters
       if (textSearched.indexOf('[') != -1 ||
               textSearched.indexOf(']') != -1 ||
               textSearched.indexOf('\\') != -1 ||
               textSearched.indexOf('/') != -1 ||
               textSearched.indexOf('^') != -1 ||
               textSearched.indexOf('$') != -1 ||
               textSearched.indexOf('.') != -1 ||
               textSearched.indexOf('|') != -1 ||
               textSearched.indexOf('?') != -1 ||
               textSearched.indexOf('*') != -1 ||
               textSearched.indexOf('+') != -1 ||
               textSearched.indexOf(')') != -1 ||
               textSearched.indexOf('(') != -1 ||
               textSearched.indexOf('{') != -1 ||
               textSearched.indexOf('}') != -1 ||
               textSearched.indexOf('&amp;') != -1) {
                       alert('Η λέξη αναζήτησης περιέχει μη επιτρεπτούς χαρακτήρες:[,],{,},\\,/,^,$,.,|,?,*,+,(,),&amp;');
                       return false;
       }

       return true;
}


function initializeMenu(){

	/* Add to bookmarks button */
	$("a.addToFavorites").click(function(e){
		e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
		var bookmarkUrl = this.href;
		var bookmarkTitle = this.title;
	 
		if (window.sidebar) { // For Mozilla Firefox Bookmark
			window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
		} else if( window.external || document.all) { // For IE Favorite
			window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
		} else if(window.opera) { // For Opera Browsers
			$("a.addToFavorites").attr("href",bookmarkUrl);
			$("a.addToFavorites").attr("title",bookmarkTitle);
			$("a.addToFavorites").attr("rel","sidebar");
		} else { // for other browsers which does not support
			 alert('Πατείστε CRTL+D για να προσθέσετε την σελίδα στα Αγαπημένα σας');
			 return false;
		}
	});

	/*Menu Arrow Pointer*/
		$('#menuitem1').hover(
			function(){
				$('.pointer-electricity').css('display', 'block');
				$('.pointer-electricity').css('left', '144px');
			},
			function(){
				$('.pointer-electricity').css('display', 'none');
			}
		);
		$('#menuitem2').hover(
			function(){
				$('.pointer').css('display', 'block');
				$('.pointer').css('left', '238px');
			},
			function(){
				$('.pointer').css('display', 'none');
			}
		);
		$('#menuitem3').hover(
			function(){
				$('.pointer').css('display', 'block');
				$('.pointer').css('left', '338px');
			},
			function(){
				$('.pointer').css('display', 'none');
			}
		);
		$('.groupmenu').hover(
				function(){
					$('#menu4').css('display', 'block');
				},
				function(){
					$('#menu4').css('display', 'none');
				}
			);
		$('#menuitem5').hover(
			function(){
				$('.pointer').css('display', 'block');
				$('.pointer').css('left', '560px');
			},
			function(){
				$('.pointer').css('display', 'none');
			}
		);
		$('#menuitem6').hover(
			function(){
				$('.pointer').css('display', 'block');
				$('.pointer').css('left', '676px');
			},
			function(){
				$('.pointer').css('display', 'none');
			}
		);
		/*Menu Arrow Pointer*/

	/*SubMenu Inlighted Hover*/
		$('.first').hover(
			function(){
				$('.bullet-first').css('color', '#fff');
			},
			function(){
				$('.bullet-first').css('color', '#3399cc');
			}
		);
		$('.second').hover(
			function(){
				$('.bullet-second').css('color', '#fff');
			},
			function(){
				$('.bullet-second').css('color', '#3399cc');
			}
		);
		$('.third').hover(
			function(){
				$('.bullet-third').css('color', '#fff');
			},
			function(){
				$('.bullet-third').css('color', '#3399cc');
			}
		);
		$('.fourth').hover(
			function(){
				$('.bullet-fourth').css('color', '#fff');
			},
			function(){
				$('.bullet-fourth').css('color', '#3399cc');
			}
		);
		$('.fifth').hover(
			function(){
				$('.bullet-fifth').css('color', '#fff');
			},
			function(){
				$('.bullet-fifth').css('color', '#3399cc');
			}
		);
		$('.last').hover(
			function(){
				$('.bullet-last').css('color', '#fff');
			},
			function(){
				$('.bullet-last').css('color', '#3399cc');
			}
		);
	/*SubMenu Inlighted Hover*/	
}

/*Top News Rollover*/
function applyTopNewsRollover(){
	
	var container = $('div.showcase');
	var mainStory = container.find('.showcase-main');

	$('li.showcase-rollover').hover(
			function () {
				$(this).addClass('rollover-active');
				mainStory.addClass('rollover-active-main');
				mainStory.find('div.showcase-strap-link').hide();
				container.find('div.rollover-left').css('visibility','hidden');
				container.find('div.rollover-arrows').css('visibility','hidden');
				mainStory.find('div.showcase-overlay').show();
				$(this).find('div.showcase-rollover-left').show();
				
				var rightSide = $(this).find('div.showcase-rollover-right');
				rightSide.find('div.showcase-strap-link').hide();
				rightSide.find('div.showcase-strap-link-image').show();
			
			},
			function () {
				$(this).removeClass('rollover-active');
				$(this).find('div.showcase-rollover-left').hide();
				mainStory.removeClass('rollover-active-main');
				container.find('div.rollover-left').css('visibility','visible');
				container.find('div.rollover-arrows').css('visibility','visible');
				mainStory.find('div.showcase-strap-link').show();
				mainStory.find('div.showcase-overlay').hide();
				
				var rightSide = $(this).find('div.showcase-rollover-right');
				rightSide.find('div.showcase-strap-link-image').hide();
				rightSide.find('div.showcase-strap-link').css('display', 'table');

			}
		);
	
}
/*Top News Rollover*/


/* Radio active hover */
function enableRadioActiveHover(){

$('.blog-entry').hover(
		function(){
			$(this).prev().css('visibility', 'visible');
		},
		function(){
			$(this).prev().css('visibility', 'hidden');
		}
	);
}
/* Radio active hover */

/* Home page tabs */
function activateTabs(){
	$('#most-popular-commented-tab-container').easytabs({
		defaultTab: "li#popularTab",
		updateHash: false,
		animate: false
	});
	
	$('#latest-tabs').easytabs({
		defaultTab: "li#allNewsTab",
		updateHash: false,
		animate: false
	});

	$('#latest-tabs').bind('easytabs:before', function(evt, tab, panel, data) {
	    
		var subject = 'allPublications';
		if ( panel[0].id == 'electicity-news') {
			subject = 'electricPower';
		}
		else if ( panel[0].id == 'oil-news') {
			subject = 'oil';
		}
		else if ( panel[0].id == 'gas-news') {
			subject = 'gas';
		}
		else if ( panel[0].id == 'res-news') {
			subject = 'res';
		}
		else if ( panel[0].id == 'solidFuels-news') {
			subject = 'solidFuels';
		}
		else if ( panel[0].id == 'all-news') {
			subject = 'allPublications';
		}
		
		var parentId = panel[0].id;
		
		loadLatestPublicationsForTab(parentId, subject, 'greece-international');
		
	});
	
	loadLatestPublicationsForTab('all-news', 'allPublications', 'greece-international');
	
}

function loadLatestPublicationsForTab(tabId, subject, location){
	
	var url = '/latest-publications?subject='+subject;
	
	if (location != 'greece-international'){
		url = url +'&location='+location;
	}
	
	var tabHeader = $('#'+tabId+'-header').text();
	var width = $('#'+tabId+'-header').css('width');
	
	$('#'+tabId+'-header').html('');
	$('#'+tabId+'-header').width(parseInt(width));
	$('#'+tabId+'-header').append('<img  id="loader" src="/image/ajax-loader.gif" alt="loader" width="16" height="16" style="position:relative;left:41%;"/>');
	
	$('#hidden-div').load(url, function(response, status, xhr){ 
		
		if (status != 'error') {
			
			$('div#'+tabId).html($('#hidden-div').html());
			$('#hidden-div').html('');
			//$('div#'+tabId).animate({opacity:0.3}, 500, function(){
			//	$('div#'+tabId).animate({opacity:1}, 500);
			//});
			
			if (location=='greece-international'){
				$('div#'+tabId+' a.greece-international-link').addClass('active');
				$('div#'+tabId+' a.greece-link').removeClass('active');
				$('div#'+tabId+' a.international-link').removeClass('active');
			}
			else if (location=='greece'){
				$('div#'+tabId+' a.greece-link').addClass('active');
				$('div#'+tabId+' a.greece-international-link').removeClass('active');
				$('div#'+tabId+' a.international-link').removeClass('active');
			}
			else if (location=='international'){
				$('div#'+tabId+' a.international-link').addClass('active');
				$('div#'+tabId+' a.greece-international-link').removeClass('active');
				$('div#'+tabId+' a.greece-link').removeClass('active');
			}
			
			$('div#'+tabId+' a.greece-international-link').click(
					function(){
						loadLatestPublicationsForTab(tabId, subject, 'greece-international');
						return false;
					}
			);
			
			$('div#'+tabId+' a.greece-link').click(
					function(){
						loadLatestPublicationsForTab(tabId, subject, 'greece');
						return false;
					}
			);

			$('div#'+tabId+' a.international-link').click(
					function(){
						loadLatestPublicationsForTab(tabId, subject, 'international');
						return false;
					}
			);


		}
		
		//$('#loader').hide();
		$('#'+tabId+'-header').html('');
		$('#'+tabId+'-header').text(tabHeader);
		//$('#'+tabId+'-header').width(width-10);
	});
}

function loadHandlersForFontSizeControl(){
	  // Reset Font Size
	  var originalFontSize = $('.pub-body').css('font-size');
	    $(".reset-font-size").click(function(){
	    $('.pub-body').css('font-size', originalFontSize);
	    return false;
	  });
	  // Increase Font Size
	  $(".increase-font-size").click(function(){
	    var currentFontSize = $('.pub-body').css('font-size');
	    var currentFontSizeNum = parseFloat(currentFontSize, 10);
	    var newFontSize = currentFontSizeNum*1.2;
	    $('.pub-body').css('font-size', newFontSize);
	    return false;
	  });
	  // Decrease Font Size
	  $(".decrease-font-size").click(function(){
	    var currentFontSize = $('.pub-body').css('font-size');
	    var currentFontSizeNum = parseFloat(currentFontSize, 10);
	    var newFontSize = currentFontSizeNum*0.8;
	    $('.pub-body').css('font-size', newFontSize);
	    return false;
	  });

}


function scrollToSelectedBlog(selectedBlogId){
	$('html,body').animate({scrollTop: $("#"+selectedBlogId).offset().top},'slow');
}

function initializeFooterHandlers(){
	
	$(".footer-top").click(function(){
		$('html,body').animate({scrollTop: 0},{duration: 1500});
	});
	
}

function initializePaginationHandlers(containerId){
	$('a.page-indicator').click(
			function(){
				
				if (!history.pushState){
					return true;
				} 
				
				var url = $(this).attr('href');
				var parent = $(this).parent();
				
				parent.html('');
				parent.append('<img  id="loader" src="/image/ajax-loader.gif" alt="loader" width="16" height="16"/>');

				var urlToLoad = url;
				
				if (url.indexOf("?") == -1){
					urlToLoad = url +'?ajax=true';
				}
				else{
					urlToLoad = url +'&ajax=true';
				}
				
				$('#hidden-div').load(urlToLoad, {}, function(response, status, xhr){ 
					
					if (status != 'error') {
						
						$('#'+containerId).animate({opacity:0}, 100, function(){
							$('#'+containerId).html($('#hidden-div').html());
							$('#hidden-div').html('');
							$('#'+containerId).animate({opacity:1}, 100);
							
							initializePaginationHandlers(containerId);
							
							if (history.pushState) {
								var stateObj = { page: url };
								history.pushState(stateObj, "page", url);
							}

						});
					}
					else{
						$('#loader').hide();
					}
				});
				
				return false;
			});
}

function initializeRegistrationForm(){
	
	$("a[rel^='prettyPhoto[registrationiframe]']").prettyPhoto({
		overlay_gallery: false,
		social_tools: '',
		deeplinking: false 
	});
}

function initializeLoginForm(){
	
	$("a[rel^='prettyPhoto[loginiframe]']").prettyPhoto({
		overlay_gallery: false,
		social_tools: '',
		deeplinking: false 
	});
}


/*
 * Closes any modal window opened with prettyPhoto
 */
function closeLoginFormAndReloadPage(){
	$.prettyPhoto.close();
	
	//$("#login").replaceWith(newUserArea);
	
	//activateUserMenu();
	
	location.reload(true);
}

function activateUserMenu(){
	$('#user-menu-arrow').click(
			function(){
				$('#user-menu').css('display', 'block');
				$(this).css('background','#fff');
				$(this).css('border-top','1px solid #999');
				$(this).css('border-right','1px solid #999');
 			    $(this).css('border-left','1px solid #999');
			}
		);
}

function initializeVerticalBarHandlers(){

	$("#like_it").children('#top').click(function(){
		$('html,body').animate({scrollTop: 0},{duration: 1500});
	});
	
	/*Like it Hover*/
	$('#info').hover(
		function(){
			$('.info').css('visibility', 'visible');
		},
		function(){
			$('.info').css('visibility', 'hidden');
		}
	);
	$('#rss-icon').hover(
		function(){
			$('.rss-icon').css('visibility', 'visible');
		},
		function(){
			$('.rss-icon').css('visibility', 'hidden');
		}
	);
	$('#mail').hover(
		function(){
			$('.mail').css('visibility', 'visible');
		},
		function(){
			$('.mail').css('visibility', 'hidden');
		}
	);
	$('#fb').hover(
		function(){
			$('.fb').css('visibility', 'visible');
		},
		function(){
			$('.fb').css('visibility', 'hidden');
		}
	);
	$('#tw').hover(
		function(){
			$('.tw').css('visibility', 'visible');
		},
		function(){
			$('.tw').css('visibility', 'hidden');
		}
	);
	/*Like it Hover*/

}


function openLoginPanel () {
	$('#login-button').click();
}

function openCommentForm() { 
	$('#commentForm').show();
	$('#comment-messages').hide();
}

function closeCommentForm() { 
	$('#commentForm').hide();
}

