$(function(){
    $('ul#topnav>li')
        .mouseover(function(){
                $(this).find('ul#submenu').stop(true,true).slideDown('fast');
                $(this).hover(function(){
                    },function(){
                        $(this).find('ul#submenu').stop(true,true).slideUp('fast');
                    });
            });//telos mouse on,mouse off nebu
	
    updateContent = function(menu,id){			
		if(menu=='publications')
			start_timer('publications');
		else if(menu=='home')
			start_timer('home');
		else
			stop_timer();

        $('div.content-container').fadeOut(10,function(){
            $.ajax({
            type    :   "POST",
            url     :   "ajax_manager.php",
            data    :   "action="+menu+"&id="+id,
            success :   function(msg){
							$('div#footer').hide();//hide the footer section
							if(menu!='home' && menu!='fetchgalery')//display the footer everywhere but home page and galleries pages
								$('div#footer').show();
								
                            $('div.content-container').html(msg).show();
                            first_pic = $('ul.gallery_demo li:nth-child(1) img');
                            showthumb(first_pic);
                        }//telos success function
          });//telos ajax call
        });//telos function slideUp
        
    }//telos function updateContent
      
    showthumb = function(pic){
		//we use the class active in order to know witch li is shown for the prev/next navigation function below(navigateGallery)
		$('ul.gallery_demo li img').removeClass('active');
		pic.addClass('active');
       
		$('div#main_image').hide().html('<img src="'+pic.attr('src')+'" />').show();
		$('div#main_image img').hover(
			function(){
				$().stop(true);toggle_desc('show');
			},
			function(){
				$().stop(true);toggle_desc('hide');
			}
		);
        $('div#description').html(pic.attr('title'))
			.hover(
				function(){
					$().stop(true);toggle_desc('show');
				},
				function(){
					$('div#main_image img').hover(function(){
						$().stop(true);toggle_desc('show');
					},function(){});
				}
			).mousemove(function(){$(this).stop(true,true);});
			
		$('div#main_image img,div#description').mouseout(function(){
			toggle_desc('hide');
		});
        $('ul.gallery_demo img').css('opacity','0.3');
        pic.css('opacity','1.0');
    }//telos function showthuymb
   
	navigateGallery = function(mode){
		var active_pic = $('ul.gallery_demo li').find('img.active').parent();//get the active li from the class of the img
		var ul_length = $('ul.gallery_demo li').length;
		
		if(mode=='+'){
			var next_li = active_pic.next('li');//retrive the next list element
			if(next_li.length<1)//if there is not any show the first list element
				var new_pic = $('ul.gallery_demo li:first-child').find('img');
			else
				var new_pic = next_li.find('img');
		}//telos if next mode
		else{
			var prev_li = active_pic.prev('li');//rerieve the previous list element
			if(prev_li.length<1)//if there is not any show the last list element
				var new_pic = $('ul.gallery_demo li:last-child').find('img');
			else
				var new_pic = prev_li.find('img');
		}//telos if prev mode
		showthumb(new_pic);
	}//telos function show next
	
//    updateContent('home');
	
	sendMail = function(name,email,subject,msg){
		 $.ajax({
            type    :   "POST",
            url     :   "ajax_manager.php",
            data    :   "action=sendMail&name="+name+"&email="+email+"&subject="+subject+"&msg="+msg,
            success :   function(msg){
							$("div#errors").html(msg)
							.css({
								background:"#c6ffa4",
								color:"#4a4a4a",
								"text-align":"center",
								"font-weight":"bold",
								"padding":"10px"
							}).fadeIn();
                        }//telos success function
          });//telos ajax call
	}//telos function sendMail
	
   //disable right click
   $(document).bind("contextmenu",function(e){return false;});
   
   $.history.init(loadPage);
});//telos jQuery

loadPage = function(url){
   if(url=="" || url===undefined) url="home";
    try{
        url = url.replace(/^.*#./, '');
        url = url.replace(/\//, '');
        var data = url.split('/');
        load(data[0],data[1]);
    }catch(exc){console.log(exc);}
}//telos function loadPage

load = function(page,ID){
    if(page=="gallery") page = "fetchgalery";
    updateContent(page,ID);
}//telos function load
