
// ALex
// Application's global javascript
// jQuery needed

var Application = {

  FontSize : {
    minSize : 12, 
    
    maxSize : 16, 
    
    step : 2,
  
    increaseSize : function(elemID){
      var currentFontSize = parseInt(jQuery('#'+elemID).css('font-size'));
      if(currentFontSize + this.step > this.maxSize){
        return;
      }
      jQuery('#'+elemID).css('font-size', currentFontSize+this.step);
    },
  
    decreaseSize : function(elemID){
      var currentFontSize = parseInt(jQuery('#'+elemID).css('font-size'));
      if(currentFontSize - this.step < this.minSize){
        return;
      }
      jQuery('#'+elemID).css('font-size', currentFontSize-this.step);
    }
  },
  
  DateTime : {
    months : ['Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαϊόυ','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου'],
    
    days : ['Κυριακή','Δευτέρα','Τρίτη','Τετάρτη','Πέμπτη','Παρασκευή','Σάββατο'],
    
    updateClock : function(){
      var date = new Date();
      var minutes = new String(date.getMinutes());
      if(minutes.length == 1){
        minutes = "0" + minutes;
      }    
     jQuery('ins.time').html(date.getHours()+':'+minutes);    
     
    },
    
    setDate : function(){
      var date = new Date();
      jQuery('ins.day').html(this.days[date.getDay()]+', '+date.getDate()+' '+this.months[date.getMonth()]+' '+date.getFullYear());
    },
    
    init : function(){
      this.updateClock();
      this.setDate();      
      setInterval(Application.DateTime.updateClock, 10000);
    }    
  },
  
  NewsTicker : {
        
    init : function(){
     try{ 
       jQuery('.ticker').jCarouselLite({
          btnNext: ".next",
          btnPrev: ".previous",
          visible : 1,
          circular : true,
          start : 0,
          vertical : true,
          auto : 3000,
          pauseOnHover: true
      });
     }
     catch(e){
    
     }  
    },    
        
    pause : function(){
    
    }
   
  },
  
  
  NewsSlider : {
    mouseover : function(index){
      if(index == 1)
        return;
      jQuery('div#mainslider h3 a').attr('href', jQuery('span#slide_'+index+' span.slide_url').html());
      jQuery('div#mainslider h3 a').html(jQuery('span#slide_'+index+' span.slide_title').html());
      jQuery('div#mainslider a.image').attr('url', jQuery('span#slide_'+index+' span.slide_url').html());
      jQuery('div#mainslider a.image img').attr('src', jQuery('span#slide_'+index+' span.slide_photo').html());
      jQuery('div#mainslider a.image img').attr('alt', jQuery('span#slide_'+index+' span.slide_title').html());
      jQuery('div#mainslider div.description p span#preview').html(jQuery('span#slide_'+index+' span.slide_preview').html());
      jQuery('div#mainslider div.description p a').attr('href', jQuery('span#slide_'+index+' span.slide_url').html());
      if(jQuery('div#mainslider div.description em strong'))
       jQuery('div#mainslider div.description em strong').html(jQuery('span#slide_'+index+' span.slide_producer').html());
    },
    
    mouseout : function(){
      index = 1;
      jQuery('div#mainslider h3 a').attr('href', jQuery('span#slide_'+index+' span.slide_url').html());
      jQuery('div#mainslider h3 a').html(jQuery('span#slide_'+index+' span.slide_title').html());
      jQuery('div#mainslider a.image').attr('url', jQuery('span#slide_'+index+' span.slide_url').html());
      jQuery('div#mainslider a.image img').attr('src', jQuery('span#slide_'+index+' span.slide_photo').html());
      jQuery('div#mainslider a.image img').attr('alt', jQuery('span#slide_'+index+' span.slide_title').html());
      jQuery('div#mainslider div.description p span#preview').html(jQuery('span#slide_'+index+' span.slide_preview').html());
      jQuery('div#mainslider div.description p a').attr('href', jQuery('span#slide_'+index+' span.slide_url').html());
      if(jQuery('div#mainslider div.description em strong'))
        jQuery('div#mainslider div.description em strong').html(jQuery('span#slide_'+index+' span.slide_producer').html());
    }
  },

  Opinions : {
    timeInterval : 60000, //60 secs
    timer : null,
    initSlider : function(){
      this.timer = setTimeout("Application.Opinions.next()", this.timeInterval);
    },
    next : function(){
      try{
        $('div#opinions li.next a').click();
      }
      catch(e){
      
      }
    },    
    move : function(url){
      if(url == 'null'){
        return;        
      }      
      
      jQuery.get(
        url,
        {},
        function(data){
          jQuery('#opinions').html(data);
          clearTimeout(Application.Opinions.timer);
          Application.Opinions.initSlider();
        },
        'html'
      )
    }   
  },

  PopupLogin : {  
    init : function(){
      // Dialog			
				$('div#pop_up_login').dialog({
					autoOpen: false,
					width: 600,
          modal : true,
					buttons: {
						/*
            "Ok": function() {               
							$(this).dialog("close"); 
						}, 
						"Cancel": function() { 
							$(this).dialog("close"); 
						} 
            */
					},
          resizable : false
				});
				
				// Dialog Link
				$('li.login a').click(function(){          
					$('div#pop_up_login').dialog('open');
          $('div#pop_up_login input[type=text]').val('');          
          $('div#pop_up_login input[type=password]').val('');          
          $('div#pop_up_login .error-msg:not(#remind)').html('');
					return false;
				});        
    },    
    
    show : function(){
      $('div#pop_up_login').dialog('open');
      $('div#pop_up_login input[type=text]').val('');          
      $('div#pop_up_login input[type=password]').val('');          
      $('div#pop_up_login .error-msg:not(#remind)').html('');
    },
    
    hide : function(){      
      $('div#pop_up_login').dialog('close');
    },
    
    submit : function(){
      jQuery.ajax(
        {
          async: true,
          url : '/default.php?pname=Login',
          type : 'POST', 
          dataType : 'json',
          data : {
            action : 'ajax',
            username : jQuery('div#pop_up_login input#username').val(), 
            pwd : jQuery('div#pop_up_login input#pwd').val(),
            remember : jQuery('div#pop_up_login input#remember').attr('checked')            
          },
          error : function(XMLHttpRequest, textStatus, errorThrown){
          
          },
          success : function(data, textStatus, XMLHttpRequest){
            if(data['message'] == 'ok'){
              $('div#pop_up_login').dialog('close');
              window.location.reload(true);
            }
            else{
              $('div#pop_up_login label#errors').html(data['errors']);
            }
          }
        }
      );
    }    
  },
  
  PopupRemind : {
    init : function(){
      $('div#pop_up_remind').dialog({
					autoOpen: false,
					width: 600,
          modal : true,
					buttons: {
						/*
            "Ok": function() {               
							$(this).dialog("close"); 
						}, 
						"Cancel": function() { 
							$(this).dialog("close"); 
						} 
            */
					},
          resizable : false
				});
    },
    
    show : function(){
      Application.PopupLogin.hide();
      $('div#pop_up_remind').dialog('open');
      $('div#pop_up_remind div#success').hide();
      $('div#pop_up_remind div#form').show();
      $('div#pop_up_remind label#errorsDiv').html('');
    },
    
    hide : function(){      
      $('div#pop_up_remind').dialog('close');
    },
    
    submit : function(){
      $('div#pop_up_remind label#errorsDiv').show();
      jQuery.ajax(
        {
          async: true,
          url : '/default.php?pname=ForgetPassword',
          type : 'POST', 
          dataType : 'json',
          data : {
            "action" : "ajax", "username" : $('div#pop_up_remind input#username').val(), "email" : $('div#pop_up_remind input#email').val()         
          },
          error : function(XMLHttpRequest, textStatus, errorThrown){
          
          },
          success : function(data, textStatus, XMLHttpRequest){
            if(data['message'] == 'ok'){
              $('div#pop_up_remind div#success').show();
              $('div#pop_up_remind div#form').hide();
            }
            else{
              $('div#pop_up_remind label#errorsDiv').html(data['errors']);
            }
          }
        }
      );
    }
  },
  
  Logout : {
    submit : function(isFBUser){
          
      if(isFBUser == '1'){
        FB.logout(function(response) {
          jQuery.ajax(
          {
            async: true,
            url : '/default.php?pname=Logout',
            type : 'GET', 
            dataType : 'json',
            data : {
                       
            },
            error : function(XMLHttpRequest, textStatus, errorThrown){
            
            },
            success : function(data, textStatus, XMLHttpRequest){
              if(data['message'] == 'ok'){
                window.location.reload(true);
              }
              else{
                // will never be invoked actually
                alert(data['errors']);
              }
            }
          }
        ); 
       });
      }      
      else{    
        jQuery.ajax(
          {
            async: true,
            url : '/default.php?pname=Logout',
            type : 'GET', 
            dataType : 'json',
            data : {
                       
            },
            error : function(XMLHttpRequest, textStatus, errorThrown){
            
            },
            success : function(data, textStatus, XMLHttpRequest){
              if(data['message'] == 'ok'){
                window.location.reload(true);
              }
              else{
                // will never be invoked actually
                alert(data['errors']);
              }
            }
          }
        );
      }  
    }    
  },  

  Sxoliana : {
    toggle : function(id){
      jQuery('div#'+id).toggle();
    }        
  },

  Calendar : {    
    changeWeek : function(direction, date, year){ 
      jQuery.get(
        '/default.php',
        {pname : 'CalendarAjax', date : escape(date), year : year, action : 'change_week', direction : direction},
        function(data){
          jQuery('div.calendar').html(data);           
        },
        'html'
      );   
    },
    
    getEvents : function(id, eventDate, headerDate){
      jQuery('ul.nrs li').removeClass('active');
      jQuery('li#'+id).addClass('active');
      jQuery('p.today').html(headerDate);  
      jQuery.get(
        '/default.php',
        {pname : 'CalendarAjax', eventDate : escape(eventDate), action : 'events'},
        function(data){
          jQuery('ul#events-list').html(data);
        },
        'html'
      );     
    }        
  },

  Poll : {
    submit : function(){
      jQuery('span#poll_not_answered').hide();
      var inputs = $('input[type=radio][name=answer]');
      var checked = false;
      for(i=0; i<=inputs.length-1; ++i){
        if($(inputs[i]).attr('checked')){
          checked = true;
          break;
        }
      }
      if(checked){
        jQuery('form[name=poll_question]').submit();
      }
      else{
        jQuery('span#poll_not_answered').show();
      }
    },
    showResults  : function(){
      jQuery('div.result span#results').show();
      jQuery('div.result span#answers').hide();
    },
    hideResults : function(){
      jQuery('div.result span#results').hide();
      jQuery('div.result span#answers').show();
    }
  },

  Polls : {
    toggle : function(e, id){
      jQuery(e).html( (jQuery(e).html() == 'open' ? 'close' : 'open') );
      jQuery('dl#'+id).toggle();
    }
  },

  SendFriend : {
    init : function(){
      // Dialog			
				$('div.sendfriend').dialog({
					autoOpen: false,
					width: 600,          
          modal : true,
					buttons: {
						/*
            "Ok": function() {               
							$(this).dialog("close"); 
						}, 
						"Cancel": function() { 
							$(this).dialog("close"); 
						} 
            */
					},
          resizable : false
				});
				
				// Dialog Link
				$('li.email a').click(function(){
          $('div.sendfriend').dialog('open');
          $('div.sendfriend input[type=text]:not(#url)').val('');
          $('div.sendfriend textarea').val('');
          $('div.sendfriend .error-msg').html('');
          $('div.sendfriend div#form').show();
          $('div.sendfriend div#success').hide();
					return false;
				});        
    },    
    
    hide : function(){      
      $('div.sendfriend').dialog('close');
    },
    
    submit : function(){
      jQuery.ajax(
        {
          async: true,
          url : '/default.php?pname=SendFriend',
          type : 'POST', 
          dataType : 'json',
          data : {
            action : 'ajax',
            url : jQuery('#url').val(),
            friend_name : jQuery('div.sendfriend #friend_name').val(), 
            friend_email : jQuery('div.sendfriend #friend_email').val(), 
            message : jQuery('div.sendfriend #message').val(), 
            user_name : jQuery('div.sendfriend #user_name').val(), 
            user_email : jQuery('div.sendfriend #user_email').val(), 
            captcha_send_friend : jQuery('div.sendfriend #captcha_send_friend').val()
          },
          error : function(XMLHttpRequest, textStatus, errorThrown){
          
          },
          success : function(data, textStatus, XMLHttpRequest){
            if(data['message'] == 'ok'){
              $('div.sendfriend div#form').hide();
              $('div.sendfriend div#success').show();
            }
            else{
              $('div.sendfriend label#errors').html(data['errors']);
            }
          }
        }
      );
    }
  },

  FacebookPopupLogin : {
    show : function(){
      FB.login(function(response){
        if (response.session) {
          // user successfully logged in
          jQuery.ajax(
          {
          async: true,
          url : '/default.php?pname=FacebookLogin',
          type : 'POST', 
          dataType : 'json',
          data : {
            
          },
          error : function(XMLHttpRequest, textStatus, errorThrown){

          },
          success : function(data, textStatus, XMLHttpRequest){
            if(data['message'] == 'ok'){
                window.location.reload(true);
            }            
          }
          }
          );
        
        }             
      });
      Application.PopupLogin.hide();
    }
  },

  ArticleLightbox : {
    init : function(){
      try{
        $('a.article-images').colorbox({
           
        });
      }
      catch(e){
      
      }
    }
  },

  Comments : {
    reply : function(id){
       $('div#comment-form input#parent_id').val(id);
       $('div#comment-form input#title').val('Re:'+$('div#comment_'+id+' div.comment h4').html()); 
       window.location = '#comment_form_anchor';
    },

    resetForm : function(){
      $('div#comment-form input#title').val('');
      $('div#comment-form textarea#comment').html('');
      $('div#comment-form input#screenname').val('');
      $('div#comment-form input#captcha').val('');
    },

    getPage : function(page){
      jQuery.ajax(
        {
          async: true,
          url : '/default.php',
          type : 'GET', 
          dataType : 'html',
          data : {
            "pname" : "CommentsAjax",
            "action" : "ajax",
            "art_id" : $('form#comments_pager_form input#art_id').val(),
            "cat_id" : $('form#comments_pager_form input#cat_id').val(),
            "bl_id"  : $('form#comments_pager_form input#bl_id').val(),
            "bl_sub" : $('form#comments_pager_form input#bl_sub').val(),
            "bo_id"  : $('form#comments_pager_form input#bo_id').val(), 
            "bo_sub" : $('form#comments_pager_form input#bo_sub').val(),            
            "cpage"  : page
          },
          error : function(XMLHttpRequest, textStatus, errorThrown){
          
          },
          success : function(data, textStatus, XMLHttpRequest){
            $('div#comments').html(data);
          }
        }
      ); 
    }
  },

  ArticleTabs : {
    select : function(li, id){
      $('ul#tabs_menu li').removeClass('active');
      $(li).addClass('active');
      $('div.result ul.article-list').hide();
      $('ul#'+id).show();
    }    
  },

  NewsletterSubscribe : {
    submit : function(){
      jQuery.ajax(
        {
          async: true,
          url : '/default.php?pname=NewsletterSubscribe',
          type : 'POST', 
          dataType : 'json',
          data : {
            "email" : $('input#newsletter_email').val()
          },
          error : function(XMLHttpRequest, textStatus, errorThrown){
          
          },
          success : function(data, textStatus, XMLHttpRequest){
            if(data.message == 'ok'){
              $('input#newsletter_email').val('')
              alert(data.info);
            }
            else if(data.message == 'error'){
              $('input#newsletter_email').focus();
              alert(data.errors); 
            }            
          }
        }
      ); 
    }
  }
}









