$(document).ready(function() {                   
    
    $("#textdiv").load("shopmain.jsp");
    
    $(".toplinksmain").click(function(){
       var parent = $(this).attr("title");
       var value = $(this).attr("id");
       $("#sublinksdiv").load("sublinks",{'parent':parent}).show("slow");
       $("#mainimage").attr("src","images/image"+value+".png");
       if (parent == "shop"){
           $("#textdiv").html("<img src='images/loading.gif' alt='loading...' title='loading...' />")
           $("#textdiv").load("shopmain.jsp");
       }
    });
    
    $(".sublinks").livequery("click", function(data){
       var page = $(this).attr("id");
       $("#textdiv").html("<img src='images/loading.gif' alt='loading...' title='loading...' />")       
       $("#textdiv").load("contents",{'page':page});            
    });
    
    $(".bottomlinks").click(function(){
        var destination = $(this).attr("id");
        if (destination == "links"){
            $("#textdiv").load("contents",{'page':'nopage','title':'links'});
        } else if (destination == "feedback"){
            $("#textdiv").html("<img src='images/loading.gif' alt='loading...' title='loading...' />")
            $("#textdiv").load("feedback.jsp");
        } else if (destination == "contactus"){
            $("#textdiv").html("<img src='images/loading.gif' alt='loading...' title='loading...' />")
            $("#textdiv").load("contactus.html");
        } else if (destination == "competition"){
            $("#textdiv").html("<img src='images/loading.gif' alt='loading...' title='loading...' />")
            $("#textdiv").load("contents",{'page':'Whats new','title':'competition'});
        }
    });
    
    $(".bottomlinks2").click(function(){
        var destination = $(this).attr("id");
        if (destination == "method8"){
            window.location = "http://www.method8.net";
        } else if (destination == "adonisarts"){
            window.location = "http://www.adoniscreations.net/";
        }
    });

    function twoDigits(a){
        if (a < 10) return "0"+a;
          else return a;
    }
    $("#submitbutton").livequery("click", function(data){
                    var name = $("#namefield").val();
                    var email = $("#emailfield").val();
                    var message = $("#messagefield").val();
                    var date = new Date();
                    var h = date.getHours();
                    var m = date.getMinutes();
                    var s = date.getSeconds();
                    var dy = date.getDate();
                    var mt = date.getMonth()+1;
                    var yr = date.getFullYear();
                    var formattedDate = twoDigits(dy)+"/"+twoDigits(mt)+"/"+twoDigits(yr)+" at "+twoDigits(h)+":"+twoDigits(m)+":"+twoDigits(s);
                    if (name.length == 0 || email.length == 0 || message.length == 0){
                        alert("All fields are required!");
                    } else {
                        $.post("post",{"date":formattedDate, "name":name,"email":email,"message":message}, function(data){
                           if (data.indexOf("ERROR") > -1){
                               //alert("Sorry, an error occured while processing your request. Please try again later");
                               alert(data);
                           } else {
                               $("#namefield").attr("value","");
                               $("#emailfield").attr("value","");
                               $("#messagefield").val("");
                               alert("Thanks for your post");
                               $("#textdiv").load("feedback.jsp");
                           }
                        });                        
                    }
                });
                $("#olderlink").livequery("click", function(data){
                    $(".older").show("medium");
                    $(this).hide();
                });
                
     $("#submitcontact").livequery("click", function(data){
          var name = $("#namefield").val();
          var telephone = $("#telephonefield").val();
          var email = $("#emailfield").val();
          var confirm = $("#confirmfield").val();
          var date = $("#datefield").val();
          var guests = $("#guestsfield").val();
          var packagef = $("#packagefield").val();
          var message = $("#messagefield").val();
          var where = $("#wherefield").val();
          if (email == confirm && name.length != 0 && telephone.length != 0 && email.length != 0 && date.length != 0 && guests.length != 0 && packagef.length != 0 && message.length != 0 && where.length != 0){
              $.post("contact",{'name':name,'telephone':telephone,'email':email,'date':date,'guests':guests,'package':packagef,'message':message,'where':where},function(data){
                  if (data.indexOf("ERROR") < 0){
                      alert("Thanks for your message. We will contact you shortly");
                  } else {
                      alert("Sorry an error occured while processing your request. Please try again later");
                  }
              });
          } else {
              alert("All fields are required -OR- Email and Confirm Email do not match");
          }
     });
     
     $(".nextcategory").livequery("click", function(data){        
        var categoryId = $(this).attr("id");
        var categoryTitle = "<span class='titlecats' title='"+categoryId+"'>"+$(this).attr("title")+"</span>";
        var currentPath = $("#categoriespath").html();
        $("#categoriespath").html(currentPath+" > "+categoryTitle);
        $("#items").load("getproducts",{"category":categoryId},function(data){
              if(data.indexOf("ERROR") > 0){
                  alert("Sorry, an error occured while processing your request. Please try again later");
              } else {
                  
              }
        });                        
        $("#categories").load("getcategory",{"category":categoryId}, function(data){
           if (data.indexOf("ERROR") > 0) {
               alert("Sorry, an error occured while processing your request. Please try again later.");
           }
        });
     });
     
     $(".titlecats").livequery("click", function(data){
          var categoryId = $(this).attr("title");
          var categoryTitle = $(this).html();
          var position = $("#categoriespath").html().indexOf(categoryTitle);
          var newPath = $("#categoriespath").html().substring(0, position+categoryTitle.length);
          $("#categoriespath").html(newPath);
          $("#items").load("getproducts",{"category":categoryId},function(data){
              if(data.indexOf("ERROR") > 0){
                  alert("Sorry, an error occured while processing your request. Please try again later");
              } else {
                  
              }
        });                        
     
     $("#categories").load("getcategory",{"category":categoryId}, function(data){
           if (data.indexOf("ERROR") > 0) {
               alert("Sorry, an error occured while processing your request. Please try again later.");
           }
        });
     });
     
     $(".addtocart").livequery("click", function(data){
        var amount = $(this).prev().val();
        var productId = $(this).attr("id");
        var productTitle = $(this).attr("title");
        var actualcart = $("#actualcart").html();
        $("#actualcart").html(actualcart+","+productId+"x"+amount);
        var shoppingcart = $("#shoppingcart").html();
        $("#shoppingcart").html(shoppingcart+"<li>"+productTitle+"&nbsp;["+amount+"]<span id='"+productId+"' title='"+productId+"x"+amount+"' class='removefromcart'>X</span>");
     });
     
     $(".removefromcart").livequery("click",function(data){
        if (confirm("Are you sure you want to remove this item from the shopping cart?")){
              var actualcart = $("#actualcart").html();
              var item = ","+$(this).attr("title");
              actualcart = actualcart.replace(item,"");
              $("#actualcart").html(actualcart);              
              $(this).parent().hide();
        }        
     });
     $("#checkout").livequery("click",function(data){
         var cart = $("#actualcart").html();
         $.post("getform", {"products":cart}, function(data){
            if (data.indexOf("ERROR") < 0){
                $("#checkout").html("");
                $("#paypal").html(data);                
                $("#shoppingcart").html("");
                $("#actualcart").html("");
            } else {
                alert("Sorry, an error occured while processing your request. Please try again later.");
            }
         });         
     });
});
