this.hidePanel = function() {
  $("div.textpanel").animate({ 
    marginLeft: "-372px"
  }, 1000, 0, function() {
    $(".expand").show(300);
    $(".hide").hide(300);
  } );
  
  $("#scrollable").fadeOut(200);
}
this.showPanel = function() {
  $("div.textpanel").animate({ 
     marginLeft: "0px"
  }, 1000, 0, function() {
     $(".expand").hide(300);
     $(".hide").show(300); 
     $("#scrollable").fadeIn(200);
 } );
}
showGallery = function() {
  
  $("#gallery").animate({ 
     right: "0px"
  }, 500); 
  $(".caption_with_gal").animate({ 
     bottom: "0px"
  }, 500); 
  $("#scrollarea").fadeIn("fast");
  $(".imagetitle").fadeIn("fast");
}
showCaption = function() {
  $(".caption_normal").animate({ 
     bottom: "0px"
  }, 500);   
  $(".imagetitle").fadeIn("fast");
}
hideGallery = function() {
  $(".imagetitle").fadeOut("fast");
  $("#gallery").animate({ 
     right: "-104px"
  }, 500); 
  $(".caption_with_gal").animate({ 
     bottom: "-35px"
  }, 500); 
  $(".caption_normal").animate({ 
     bottom: "-35px"
  }, 500);   
  $("#scrollarea").fadeOut("fast");
}
this.showContent = function(node) {
    $("#scrollable").fadeOut("slow");
    $("div.textpanel").animate({ 
      marginLeft: "-372px"
    }, 1000, 0, function() {
      if(node.background != 'gallery') {
          var howmuch = "0px";
      } else {
          var howmuch = "-372px";
      }
      $("div.textpanel").animate({ 
         marginLeft: howmuch
      }, 1000, 0, function() {
         if(node.background != 'gallery') {
           $(".expand").hide(300);
           $(".hide").show(300);   
         } else {
           $(".expand").show(300);
           $(".hide").hide(300);           
         }
          div_scroll1.resetScroll();
        	$.getJSON(
        	"json.php?type=showarcticle&node="+node.id,
        	function(content){
            $("#scrollable").html(""); 
            $.each(content, function(j,child){
              $("#scrollable").append(child);
              $("#scrollable").fadeIn("fast");
            });
        	})        
      } );
      
    } );
  
}
this.selectMain = function(node) {
	$.getJSON(
	"json.php?type=menu",
	function(data){
      $.each(data, function(i,menu){
        if(menu.id == node) {
          $("li."+menu.id).addClass("selected"); 
        } else {
          $("li."+menu.id).removeClass("selected");
        }
      });
	});
}
this.selectChild = function(node,parent) {
  
	$.getJSON(
	"json.php?type=childmenu&id=" + parent,
	function(data){
      $.each(data, function(i,menus){
      if(menus.type == "link") {
        if(menus.blank == 1) {
          window.open(menus.url); 
        } else {
          window.location.href=menus.url;  
        }
      } else {
        if(menus.id == node) { 
          $("li."+menus.folder).addClass("selected");  
        } else {
          $("li."+menus.folder).removeClass("selected");
        }
      }
      });
	});
}
this.loadImage = function(src) {
    $(function () {
        $("#vari_back").fadeIn("fast"); 
        
        var img = new Image();
            
        $(img).load(function () {
            
            $(".back_image .images").html(this);
             
             
             $("#vari_back").fadeOut("fast");
        }).error(function () {
        }).attr('src', src).attr('width', 900).attr('height', 400);
       
    });
}
this.changeContent = function(menu) {
  if(menu.type == "link") {
    if(menu.blank == 1) {
      window.open(menu.url); 
    } else {
      window.location.href=menu.url;  
    }
  } else {
    hideGallery();
    
    
    selectMain(menu.id);
    
    
    
    if(menu.background == "gallery") {
      getGallery(menu.gallery,"gallery");
    } else if (menu.background == "image") {
      getGallery(menu.image,"image");
    } else {
      
    }
    
    //Generate submenu
    getSubmenu(menu);
    
    //selectChild(menu.id,menu.parent);
    //generate content                 
    showContent(menu);
  }
}
this.getSubmenu = function(menu) {
   	$.getJSON(
  	"json.php?type=childmenu&id="+menu.id,
  	function(childdata){
        $("#submenu").animate({ 
           top: "-22px"
        }, 400, 0, function() {
          $("#submenu").html("");
          $.each(childdata, function(j,child){
            var li =
   						"<li class=\"" + child.folder + "\"><a href=\""+ child.url +"\" class=\""+child.folder+"\">"+child.title+"</a></li>"
  					$(li).appendTo("#submenu");
            $("a")
             .filter("."+child.folder)
               .click(function(eventc){
                hideGallery();
                selectChild(child.id,menu.id);
                //generate content  
                showContent(child);
                if(child.background == "gallery") {
                  getGallery(child.gallery,"gallery");
                  
                } else if (child.background == "image") {
                  getGallery(child.image,"image");
                }                   
                eventc.preventDefault();
              })
         
             .end()                 
        
        } );   
       $("#submenu").animate({ 
            top: "0px"}, 400);
        });
       
  	})
}
this.getGallery = function(id, type) {
	var jsonquery = "json.php?type=getimages";
  if (type == "gallery") {
    jsonquery = "json.php?type=getimages&gallery="+id;
  } else if (type == "image") {
    jsonquery = "json.php?type=getimages&image="+id;
  } 
    $.getJSON(
  	  jsonquery,
  	function(data){ 
      $(".scrollarea .pictures").html("");
      var first = 0;
      var current = 0;
      var currentext = 0;
      var currenttitle = 0;
      var order = new Array();
      var clicked = 0;
        $.each(data, function(i,image){ 
  
        order[i] = new Array();
        order[i]["prev"] = current;
        order[i]["prevext"] = currentext;
        order[i]["prevtitle"] = currenttitle;
        
        currenttitle = image.title;
        currentext = image.ext;
        
        $("<img class=\"" + image.id + "\" src=\"images/gallery/thumbs/" + image.id + "." + image.ext + "\" width=\"100\" height=\"69\" alt=\"" + image.title + "\" />").appendTo(".scrollarea .pictures"); 
        if(first == 0) {
            loadImage("images/gallery/" + image.id + "." + image.ext);
            $(".imagetitle").html(image.title);
            first = 1;      
            clicked = i;
        } else {
          order[current]["next"] = i;
          order[current]["nextext"] = image.ext;
          order[current]["nexttitle"] = image.title;
        }
        
        $("img")
         .filter("." + image.id)
           .click(function(eventc){
            loadImage("images/gallery/" + image.id + "." + image.ext);
            $(".imagetitle").html(image.title);
            clicked = i;
          })
         .end()  
        current = i;
  
      });
    
       if (type == "gallery") {
        showGallery(); 
      } else {
        showCaption(); 
      }    
      
        $("img")
         .filter(".caption_previous")
           .click(function(){
            if(order[clicked]["prev"] != 0) {
              loadImage("images/gallery/" + order[clicked]["prev"] + "." + order[clicked]["prevext"]);
              $(".imagetitle").html(order[clicked]["prevtitle"]);
              clicked = order[clicked]["prev"];
            }
          })
         .end()  
         .filter(".caption_next")
           .click(function(){
            if(order[clicked]["next"] != undefined) {
              loadImage("images/gallery/" + order[clicked]["next"] + "." + order[clicked]["nextext"]);
              $(".imagetitle").html(order[clicked]["nexttitle"]);
              clicked = order[clicked]["next"];
            }
          })
         .end()  
        if(first == 0) {
          loadImage("./images/content_bg.jpg");
          $(".imagetitle").html("");
        }                   
  	});
 
}
this.getLocation = function() {
  var url = window.location.href;
  var brokenurl = url.split("/");
  if(brokenurl[brokenurl.length-1].toLowerCase() == "content") {
    return "home";
  } else if(brokenurl[brokenurl.length-2].toLowerCase() == "content") { 
    return brokenurl[brokenurl.length-1]; 
  } else {
    return brokenurl[brokenurl.length-2] + "/" + brokenurl[brokenurl.length-1];
  }
}
function validateNewsletterRemove(){
    var accept = true;    
    if (!$('#newsletterremove input[name=email]').val().match(new RegExp('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$'))){
        $('#newsletterremove input[name=email]').parent().parent().find('label').addClass('error');
        accept = false;
    } else {
        $('#newsletterremove input[name=email]').parent().parent().find('label').removeClass('error');
    }
    return accept;    
}
$(document).ready(function(){
 
  $('#newsletterremove').submit(function(){
      return validateNewsletterRemove();      
  })
  
	$.getJSON(
	"json.php?type=defaultview&location=" + getLocation(),
	function(data){
    changeContent(data);
  });
  
  $("#vari_back").fadeOut(); 
  
  $(".textpanel .rel .btn_expand").css({'visibility' : 'visible'});
  $(".txt_scroll").css({'visibility' : 'visible'});
  $("#vari_back").css({'visibility' : 'visible'});
    //IMAGES
   
 
 $(".expand").hide();
 $("img")
   .filter(".hide")
     .click(function(){
      hidePanel();
    })
   .end()  
   .filter(".expand")
     .click(function(){
      showPanel();
    })
   .end()   
  
  
  
  //main menu onclick actions
	$.getJSON(
	"json.php?type=menu",
	function(data){
      
      $.each(data, function(i,menu){
        
         $("a")
          .filter("."+menu.id)
            .click(function(event){
                        
              changeContent(menu);
              event.preventDefault();
           })
        .end()
      });
	});
	
  $('.news_item a.read_more').live('click', function(event){
    var node_id = $(this).attr('id').split('_')[1];
    var news_id = $(this).attr('id').split('_')[2];
    $("#scrollable").fadeOut("slow");
    div_scroll1.resetScroll();
    $("div.textpanel").animate({ 
      marginLeft: "-372px"
    }, 1000, 0, function() {
      $.getJSON(
        "json.php?type=showarcticle&node="+node_id+"&id="+news_id,
        function(content){
        
        
          $("#scrollable").html(""); 
          
           $("div.textpanel").animate({ 
              marginLeft: "0px"
            }, 1000, function(){
                $.each(content, function(j,child){
                $("#scrollable").append(child);
                $("#scrollable").fadeIn("fast");
              });          
            });        
          
        }) 
    })
    
    return false;
  })
});
