// JavaScript Document
var limit=4;
var actual=1;
$(document).ready(function(){
  
  $("#top_menu li").click(function(){
  var cat=$(this).attr("pos");
  var where="#top_menu_content";
  if(isAdmin())
      where="top_content";
      
   
    
      switch (cat) {
              case "1":
                  
                   $("#top_menu li").css("background-color","#FF6600");
                   $("#top_menu li").attr("active",0);
                   $(this).css("background-color","#C0480B");
                   $(this).attr("active",1);
                   load_top_content(where,"O nas");
                
              break
              case '2':
                  $("#top_menu li").css("background-color","#FF6600");
                   $("#top_menu li").attr("active",0);
                   $(this).css("background-color","#C0480B");
                   $(this).attr("active",1);
                   
                   load_top_content(where,'Kontakt');
                  
              break
              case '3':
                  $("#top_menu li").css("background-color","#FF6600");
                   $("#top_menu li").attr("active",0);
                   $(this).css("background-color","#C0480B");
                   $(this).attr("active",1);
                   load_top_content(where,cat);
              break
              default:
              break;
      }
 });
 $("#top_menu li").mouseover(function(){
      $("#top_menu li").css("background-color","#FF6600");
      $("#top_menu li[active=1]").css("background-color","#C0480B");
      $(this).css("background-color","#C0480B");
  });
  $("#top_menu li").mouseout(function(){
      $("#top_menu li").css("background-color","#FF6600");
      $("#top_menu li[active=1]").css("background-color","#C0480B");
      
  });
  $("#left_menu li").mouseover(function(){
      $("#left_menu li").css("text-decoration","none");
      $(this).css("text-decoration","underline");
  });
  
 $("#left_menu li").click(function(){
  var cat=$(this).attr("pos");
 
  
      switch (cat) {
              case '1':
                 $("#center_column").html('<img style="margin:240px 0 0 200px" src="'+Path()+'img/loadinfo.gif" />');
                  load_top_content('#center_column',"Start");
                   normalSize();
              break
              case '2':
                  showProducts("Damskie",limit,1);
                  showNavigation("Damskie",limit,1)
                
              break
              case '3':
                   showProducts("Meskie",limit,1);
                   showNavigation("Meskie",limit,1)
                  
              break
              case '4':
                  showProducts("Obuwie",limit,1);
                  showNavigation("Obuwie",limit,1)
              break
              case '5':
                  showProducts("Dodatki",limit,1);
                  showNavigation("Dodatki",limit,1)
                  
              break
              case '6':
                
                  showProducts("Sklepy",limit,1);
                  showNavigation("Sklepy",limit,1)
                  
              break
              default:
              break;
      }
      
 });
$(function() {
	$('a.photos').lightBox();
});
  
});
function countPages(cat,lt){
        $.post(Path()+'include/functions/count_pages.php', {category: cat,limit:lt}, function(p) {
              alert(p);
              return p;
        });
        
}
function setColumnSize(){
      var h=$("#center_column").css("height");
      $("#left_column").css("height",h);
      $("#right_column").css("height",h);
      
}
function showAnotherPage(){
      
      alert($("#marked").text());
}
function showPrevPage(cat,lt){
    var act=$("#marked").text();
    act--;
    showProducts(cat,lt,act);
}
function showNextPage(cat,lt){
    var act=$("#marked").text();
    act++;
    showProducts(cat,lt,act);
}
function showPage(cat,lt,txt){
    showProducts(cat,lt,txt)
    return false;
}
function showNavigation(cat,lt,actual){ 

     normalSize();
    $.post(Path()+'include/functions/count_pages.php', {category: cat,limit:lt,page:actual}, function(pg) {  
      if(pg>1){
             var nav='<ul id="pasek">' ;
                if(actual>1){
                    $("li.prev").fadeIn("slow");  
                }
                
                          nav+='<li class="prev"><<</li>';
                          
               
                for(i=1;i<=pg;i++){
                   
                    if(i==actual){
                        nav+='<li id="marked">'+i+'</li>';
                    }else{
                        nav+='<li class="normal">'+i+'</li>';
                    }
                   
                }
                
                 nav+='<li class="next" >>></li>';
                 nav+='</ul>';
             
            $("#navigation").html(nav);
              
                $('#pasek li').livequery('click', function() { 
                   
                     
                     setNavMargin(actual,pg);
                    if($(this).attr("class")=="next"){
                         actual++; 
                         $("li#marked").next("li").attr("id","marked");
                         $("li#marked").removeAttr("id");
                         showProducts(cat,lt,actual);
                        
                    }else if($(this).attr("class")=="prev"){
                          actual--;
                          $("li#marked").prev("li").attr("id","marked");
                          $("li#marked").removeAttr("id");
                          
                          showProducts(cat,lt,actual);
                    }else{
                         $('#pasek li').removeAttr("id");
                         $(this).attr("id","marked");
                         actual=$(this).text();
                         showProducts(cat,lt,$(this).text());  
                         
		                }
		                 showHideButt(actual,pg);
		                return false;
	              });
	             
	         
	          showHideButt(actual,pg);
	           $("#center_column").css("min-height","51.25em");
	          $("#center_column").css("height","51.25em");
	          $("#navigation").fadeIn("slow");
	          setNavMargin(actual,pg);
	           }
	    });
}

function showHideButt(actual,pg){
      if(actual>1){
              $("li.prev").fadeIn("slow");  
      }else{
            $("li.prev").hide();
      }   
     
      if(actual<pg){
           $("li.next").fadeIn("slow"); 
           
      }else if(actual==pg){
            $("li.next").hide();
      }
      
}
function setNavMargin(actual,pg){
     var lm=13*pg;
     if(actual>1){
        lm+=12;
     }
     if(actual<pg){
           lm+=12;
           
      }
      lm/=2;
	  
	   $("#pasek").css("margin-left",-lm);
	  /* $("#navigation").css("width","15em");*/
}
function normalSize(){
    $("#navigation").hide();
    $("#center_column").css("min-height","53.25em");
	  $("#center_column").css("height","53.25em");
}
function showProducts(cat,lt,actual){
    var tl=cat; 
     $("#center_column").html('<img style="margin:240px 0 0 200px" src="'+Path()+'img/loadinfo.gif" />');
     
    $.post(Path()+'include/functions/load_product_data.php', {category: cat,limit:lt,page:actual}, function(xml) {
   
           var prod='<p id="adder_title">'+tl+'</p>';
       
            $(xml).find('id').each(function(){
                
                  var i = $(this);
                  prod+='<div class="product_div"><p class="prod_title">'+i.find('name').text()+'</p><a href="'+Path()+i.find('photo').text()+'" class="photos" title="'+i.find('name').text()+'"><img src="'+Path()+'include/functions/miniaturka.php?pic=../../'+i.find('photo').text()+'&amp;width=120" class="product_photo" title="'+i.find('name').text()+'" alt="botani"/></a>'
                 
                 /* $.post(Path()+'include/functions/load_view.php', {id: i.find('it_id').text()}, function(z) {
                        prod+=z;
                        
                  });*/ 
                 prod+='<p>'+i.find('description').text()+'<p>';
                 if ( i.find('price').text() !="" ){
                    prod+='<p class="price">Cena: '+i.find('price').text()+' zl<p>';
                 }
                 if(isAdmin()){
                        prod+='<a href=""  class="delete" cat="'+i.find('category').text()+'" id="'+i.find('it_id').text()+'">usuń</a><a href="#" class="edit" cat="'+i.find('category').text()+'" id="'+i.find('it_id').text()+'">edytuj</a>';
                  }
                  
                  
	               prod+='</div>'; 
	               $("#center_column").html(prod); 
            });
            $("#center_column").html(prod);  
           
           
            $('a.photos').lightBox();
       });
               if(isAdmin()){
                      $('a.edit').livequery('click', function() {
                             showEditWindow($(this).attr("id"),$(this).attr("cat"));
                             $('a.edit').expire();
                             return false;
		                  });
	                    $('a.delete').livequery('click', function() { 
		                        delete_product($(this).attr("id"),$(this).attr("cat"));
		                         $('a.delete').expire();
		                        return false;
	                      }); 
	                      
                  }
                 
}
function init(){
      
      $("#main_li").css("background-color","#C0480B");
      $("#main_li").attr("active",1);
      load_top_content("#top_menu_content","O nas");
      load_top_content("#center_column","Start");
      loadCorners();
}

function loadCorners(){
      
      $("div#center").corner();
      $("div#right_column").corner();
      $("div#left_column").corner();
}
function load_top_content(where,cat){
        $(where).html('<img style="margin:10px 0 0 350px" src="'+Path()+'img/loadinfo.gif" />');
       
        $.post(Path()+'include/functions/load_top_content.php', {category: cat}, function(z) {
            if(isAdmin()){
                var ed = tinyMCE.get(where);
                ed.setProgressState(1);
                window.setTimeout(function() {
                        ed.setProgressState(0);
		                     ed.setContent(z);  
                          
	                 }, 300);
            }else{
              
              $(where).html(z);
              
             
              }
       });
        
}
function isAdmin(){
   if((location.href.substring(0, location.href.lastIndexOf('\/'))).search('admin')>-1){
      return true;
  }else{
      return false;
  }
}
function Path(){
   
  if((location.href.substring(0, location.href.lastIndexOf('\/'))).search('admin')>-1){
      return '../';
  }else{
      return '';
  }
}
