// Title:  Interaction Functions
// Author: Jordan Lowe 
// Email:  jlowe@precisiongraphics.com
jQuery(document).ready(function ($) {

	//REPLACEMENT FOR CSS :last-child pseudo element													 
	$('#nav ul li ul li:last-child').addClass('last-child');															 
	$('.section p:last-child').addClass('last-child');															 
	$('.section-dark p:last-child').addClass('last-child');															 
	$('#pg-why .stylesheet p:last-child').addClass('last-child');															 
	$('#pg-folio .catlist .clist .listtitle li:last-child').addClass('last-child');															 
	$('#pg-folio .catlist .clist .listtitle .sublist ul li:last-child').addClass('last-child');															 
	
  jQuery("#pg-folio > .testrow1").hide();
  var catDisplayStatus = 0;
  jQuery("#catdisplay").click(function () {
    if (catDisplayStatus === 0) {
      jQuery("#pg-folio > .testrow1").slideDown(500);
      catDisplayStatus = 1;
    } else {
      jQuery("#pg-folio > .testrow1").slideUp(400);
      catDisplayStatus = 0;
    }
  });
  $("#nav-contact").tooltip('#tip-contact');
  $("#nav-demoreel").tooltip('#tip-demo');
  $("#nav-album").tooltip('#tip-lightbox');
  // REMOVE NAVIGATION TITLE ATTRIBUTE
  // Tooltip breaks dropdown menu, so it is removed	
  jQuery("li.cat_item a").removeAttr("title");
  jQuery("li.page_item a").removeAttr("title");
  // MENU BUTTON DEFAULT
  // Hides the menus on default and sets up arrays
  jQuery("#nav-contact > .menu").hide(); //hide the slideout menu
  jQuery("#nav-album > .menu").hide(); //hide the slideout menu
  jQuery("#nav-demoreel > .menu").hide();
  var menuBtn1 = ["#nav-contact", false, "menuBtn1"]; //array(id, menuUpDown, tag)  //currently contactmenu
  var menuBtn2 = ["#nav-album", false, "menuBtn2"]; //array(id, menuUpDown, tag)  //currently albummenu
  var menuBtn3 = ["#nav-demoreel", false, "menuBtn3"]; //array(id, menuUpDown, tag)  //currently albummenu
  // MENU BUTTON HOVER
  // Handles the hover action of the buttons
  // Switches background images for the corresponding buttons
  jQuery(menuBtn1[0]).hover(
  function () {
    if (menuBtn1[1] == false) { //checks to see if button is in active state
      jQuery(menuBtn1[0] + " > .button").css("background-position", "0 -35px");
    }
  },
  function () {
    if (menuBtn1[1] == false) {
      jQuery(menuBtn1[0] + " > .button").css("background-position", "0 0");
    }
  }); //end hover menuBtn1
  jQuery(menuBtn2[0]).hover(
  function () {
    if (menuBtn2[1] == false) {
      jQuery(menuBtn2[0] + " > .button").css("background-position", "0 -35px");
    }
  },
  function () {
    if (menuBtn2[1] == false) {
      jQuery(menuBtn2[0] + " > .button").css("background-position", "0 0");
    }
  }); //end hover menuBtn2
  jQuery(menuBtn3[0]).hover(
  function () {
    if (menuBtn3[1] == false) {
      jQuery(menuBtn3[0] + " > .button").css("background-position", "0 -35px");
    }
  },
  function () {
    if (menuBtn3[1] == false) {
      jQuery(menuBtn3[0] + " > .button").css("background-position", "0 0");
    }
  }); //end hover menuBtn2
  // MENU BUTTON CLICK
  // Handles the click action of the buttons
  // 1) Closses any open button menus
  // 2) Opens or closes menu, changes button background image
  jQuery(menuBtn1[0] + " > .button").click(function () {
    if (menuBtn2[1] == 1) {
      menuSlideUp(menuBtn2);
      jQuery(menuBtn2[0] + " > .button").css("background-position", "0 0");
    }
    if (menuBtn3[1] == 1) {
      menuSlideUp(menuBtn3);
      menuBtn3[1] = 0;
      jQuery(menuBtn3[0] + " > .button").css("background-position", "0 0");
    }
    if (menuBtn1[1] == 0) {
      menuSlideDown(menuBtn1);
      jQuery(menuBtn1[0] + " > .button").css("background-position", "0 -70px");
    } else if (menuBtn1[1] == 1) {
      menuSlideUp(menuBtn1);
      jQuery(menuBtn1[0] + " > .button").css("background-position", "0 -35px");
    }
  }); //end menuBtn1click
  jQuery(menuBtn2[0] + " > .button").click(function () {
    if (menuBtn1[1] == 1) {
      menuSlideUp(menuBtn1);
      jQuery(menuBtn1[0] + " > .button").css("background-position", "0 0");
    }
    if (menuBtn3[1] == 1) {
      menuSlideUp(menuBtn3);
      menuBtn3[1] = 0;
      jQuery(menuBtn3[0] + " > .button").css("background-position", "0 0");
    }
    if (menuBtn2[1] == 0) {
      menuSlideDown(menuBtn2);
      jQuery(menuBtn2[0] + " > .button").css("background-position", "0 -70px");
    } else if (menuBtn2[1] == 1) {
      menuSlideUp(menuBtn2);
      jQuery(menuBtn2[0] + " > .button").css("background-position", "0 -35px");
    }
  }); //end menuBtn2click
  jQuery(menuBtn3[0] + " > .button").click(function () {
    if (menuBtn1[1] == 1) {
      menuSlideUp(menuBtn1);
      jQuery(menuBtn1[0] + " > .button").css("background-position", "0 0");
    }
    if (menuBtn2[1] == 1) {
      menuSlideUp(menuBtn2);
      jQuery(menuBtn2[0] + " > .button").css("background-position", "0 0");
    }
    if (menuBtn3[1] == 0) {
      menuSlideDown(menuBtn3);
      menuBtn3[1] = 1;
      jQuery(menuBtn3[0] + " > .button").css("background-position", "0 -70px");
    } else if (menuBtn3[1] == 1) {
      menuSlideUp(menuBtn3);
      menuBtn3[1] = 0;
      jQuery(menuBtn3[0] + " > .button").css("background-position", "0 -35px");
    }
  }); //end menuBtn3click
  // MENU SLIDE ANIMATION
  function menuSlideDown(menuItem) {
    jQuery(menuItem[0] + " > .menu").slideDown(300);
    menuItem[1] = 1; //sets menu status to 1, dropped out		
  }
  function menuSlideUp(menuItem) {
    jQuery(menuItem[0] + " > .menu").slideUp(100);
    menuItem[1] = 0; //sets menu status to 1, dropped out											 
  }
  // COOKIE HANDLER //////////////////////////////////////////////////////////////////////////
  var COOKIE_NAME = 'pg_album_items';
  var COPTIONS = {
    hoursToLive: 87600,
    path: '/',
    secure: false
  }
  function createAlbumCookie() {
    jQuery.cookies.set(COOKIE_NAME, "", COPTIONS);
  }
  function setAlbumCookie(itemval) {
    jQuery.cookies.set(COOKIE_NAME, itemval, COPTIONS);
    return false;
  }
  function getAlbumCookie() {
    var cookieval = jQuery.cookies.get(COOKIE_NAME);
    return (cookieval);
    //alert("got cookie");
  }
  function deleteAlbumCookie() {
    jQuery.cookies.del(COOKIE_NAME);
    return false;
  }
  // CART FUNCTIONS //////////////////////////////////////////////////////////////////////////
  // GET/SET COOKIE 
  // Gets cookie value and assigns it to cartARRAY
  var cartARRAY = new Array();
  if (jQuery.cookies.get(COOKIE_NAME) == null) {
    createAlbumCookie();
  } else {
    var getcookie = jQuery.cookies.get(COOKIE_NAME).split(",");
    var cartARRAY = cartARRAY.concat(getcookie);
  }
  // BUILD ALBUM ITEM
  // builds lightbox cart item, allows only 4 items in visible cart
  // appends when deleting an item from cart	
  // prepends when adding item to cart
  var itemCOUNT = jQuery("#nav-album .menu").children().size();
  function buildAlbumItem(itemID, itemURL, direction) {
    if (itemCOUNT <= 4) { //first 5 items
      if (direction == "prepend") {
        jQuery("#nav-album .menu").prepend("\n<li class=\"item" + itemID + "\"><div class=\"item\"><img src=\"" + itemURL + "\"></div><div class=\"cartremove\"></div></li>");
      } else {
        jQuery("#nav-album .menu .more").before("\n<li class=\"item" + itemID + "\"><div class=\"item\"><img src=\"" + itemURL + "\"></div><div class=\"cartremove\"></div></li>");
      }
      itemCOUNT += 1;
    } else { //removes last item when another is added. keeps only 5 items in album list.
      if (jQuery("#nav-album .menu li:nth-child(4)").attr("class") != "more") {
        jQuery("#nav-album .menu li:nth-child(4)").remove();
      }
      if (direction == "prepend") {
        jQuery("#nav-album .menu").prepend("<li class=\"item" + itemID + "\"><div class=\"item\"><img src=\"" + itemURL + "\"></div><div class=\"cartremove\"></div></li>");
      } else {
        jQuery("#nav-album .menu .more").before("<li class=\"item" + itemID + "\"><div class=\"item\"><img src=\"" + itemURL + "\"></div><div class=\"cartremove\"></div></li>");
      }
    }
  }
  // LIGHTBOX PREVIEW TIMER
  // Drops lightbox album for period of time
  var pausetime = 1500;
  var aipt;
  function albumItemPreview() { //drop cart menu after item is added
    clearTimeout(aipt);
    menuSlideDown(menuBtn2);
    jQuery(menuBtn2[0] + " > .button").css("background-position", "0 -70px");
    aipt = setTimeout(function () {
      menuSlideUp(menuBtn2);
      jQuery(menuBtn2[0] + " > .button").css("background-position", "0 0px");
    },
    pausetime);
  }
  // ADD ITEM TO CART ARRAY 
  // adds item id to cart array
  function addTocartARRAY(itemID) {
    cartARRAY.push(itemID);
  }
  // ADD ITEM TO CART ARRAY 
  // adds item id to cart array
  function addNextInLine() {
    var getcookieupdate = jQuery.cookies.get(COOKIE_NAME).split(",");
    var arrCount = getcookieupdate.length;
    var cartARRAYupdate = getcookieupdate.reverse();
    var value = cartARRAYupdate[3];
    if (arrCount >= 4) {
      buildAlbumItem(itemidnum[value], itemidimg[value], "append");
    }
  }
  // CLEAN CART ARRAY 
  // remove empty or null items from array
  function cleanCartArray() {
    for (i = cartARRAY.length - 1; i >= 0; i--) {
      if (cartARRAY[i] == "") {
        cartARRAY.splice(i, 1);
      }
    }
  }
  // REMOVE ARRAY ITEM
  // removes item id from cartARRAY
  function removeArrayItem(itemID) {
    for (var arrayval in cartARRAY) {
      if (cartARRAY[arrayval] == itemID || cartARRAY[arrayval] == "") {
        cartARRAY.splice(arrayval, 1);
      }
    }
  }
  // SEARCH ARRAY
  // search to determine if item already exists in cartARRAY
  function searchArray(itemID) {
    if (cartARRAY.length > 0) { //if has elements then check for an existing
      var isfound = 0;
      for (var i = 0, len = cartARRAY.length; i < len; ++i) {
        val = cartARRAY[i];
        if (val == itemID) {
          isfound = 1;
          return;
        }
      }
      if (isfound == 1) {
        return (true);
      } else {
        return (false);
      }
    } else { // do not check for duplicates if no elements exist
      return (false);
    }
  }
  // ADD TO ALBUM
  // adds item to the lightbox album
  // adds item id to album array
  // adds item to cart 
  // sets new cookie with added item id
  jQuery(".addToAlbum").click(function () {
    var buttonval = jQuery(this).attr("class");
    buttonval = buttonval.split(" ");
    buttonval = buttonval[1].substring(7, 11);
    var itemURL = window["item_" + buttonval + "_url"];
    var itemID = window["item_" + buttonval + "_id"];
    if (searchArray(itemID) == false) {
      if (!itemID) { //do not continue if itemID has no value
        return;
      }
      jQuery("#nav-album .menu").children(".cartdefault").remove();
      buildAlbumItem(itemID, itemURL, "prepend");
      addTocartARRAY(itemID);
      cleanCartArray();
      setAlbumCookie(cartARRAY);
      albumItemPreview();
    } else {
      alert("This item already exists in your album");
    }
  });
  // CART REMOVE
  // removes item from cart 
  jQuery(".cartremove").live("click", function () {
    var parentTag = jQuery(this).parent();
    var parentTagName = jQuery(this).parent().attr("class");
    parentTagName = parentTagName.substring(4, 8)
    parentTagName = parseInt(parentTagName);
    parentTag.remove();
    jQuery(".albumitem" + parentTagName).remove(); //removes item from album cart view
    jQuery(".item" + parentTagName).remove(); //removes item from album
    cleanCartArray();
    removeArrayItem(parentTagName);
    setAlbumCookie(cartARRAY);
    addNextInLine(); // adds next album item back to album list
    defaultCartDisplay(); //displays default text if no items are in cart
  });
  // ALBUM REMOVE
  // removes item id from album array
  // sets new cookie with removed item id
  jQuery(".albumremove").live("click", function () {
    var albumItemTag = jQuery(this).parent().parent();
    var albumItemTagName = jQuery(this).parent().parent().attr("id");
    var albumItemValue = albumItemTagName.substring(9)
    albumItemValue = parseInt(albumItemValue);
    albumItemTag.remove();
    jQuery(".item" + albumItemValue).remove(); //removes item from album
    jQuery(".albumitem" + albumItemValue).remove(); //removes item from album cart view
    cleanCartArray();
    removeArrayItem(albumItemValue);
    setAlbumCookie(cartARRAY);
  });
  // CLEAR ALBUM BUTTON
  // removes every item in the lightbox album
  // removes all from lightbox page
  // removes all from lightbox cart
  jQuery("#clearalbumbtn").click(function () { //clears all samples from the album
    var r = confirm("Are you sure you would like to delete all of the samples in your current lightbox? This is a permanent change and cannot be undone");
    if (r == true) {
      for (var arritem in cartARRAY) { //remove all samples from album and album cart			
        jQuery(".item" + cartARRAY[arritem]).remove(); //removes item from album
        jQuery(".albumitem" + cartARRAY[arritem]).remove(); //removes item from album cart view
      }
      setAlbumCookie(null);
    } else {
      return;
    }
  });
  //FOR COPYRIGHT POPUP DIALOG BOX
// TERMS OF USE ALERT
function termsAlert() {
    var COOKIE_NAME2 = 'copyrightAccept';
    var COPTIONS2 = {
      hoursToLive: 0,
      path: '/',
      secure: false
    };
    var accept = confirm('Samples in our PG portfolio are protected under copyright laws. It is illegal to copy and distribute any of the following work, unless permitted. \n\nIf you do not agree to follow these laws and our website\'s terms of service, click "cancel" to go back to the previous page.');
    if (accept) {
      jQuery.cookies.set(COOKIE_NAME2, 1, COPTIONS2);
      return;
    } else {
      history.go(-1); //take them back to previous page
    }
  }
  function isTermsAccepted() {
    var COOKIE_NAME2 = 'copyrightAccept';
    var COPTIONS2 = {
      hoursToLive: 24,
      path: '/',
      secure: false
    };
    if (!jQuery.cookies.get(COOKIE_NAME2) || jQuery.cookies.get(COOKIE_NAME2) == 0) {
      termsAlert();
    } else {
      return;
    }
  }
  if (typeof copyrightpage != "undefined") {
    if (copyrightpage == true) {
      setTimeout(function () {
        isTermsAccepted()
      },
      1000);
    }
  }
}); //END DOCUMENT.READY
// DEFAULT CART DISPLAY
// default display when there are no items in lightbox cart
function defaultCartDisplay() {
  if (jQuery("#nav-album .menu").children().size() == 1) {
    jQuery("#nav-album .menu").prepend("\n<li class=\"cartdefault\">You currently do not have any samples in your lightbox</li>");
  }
}
function validateAlbumForm() {
  var firstname = document.forms[0].firstname.value;
  var lastname = document.forms[0].lastname.value;
  var emailaddress = document.forms[0].emailaddress.value;
  var alertText = "";
  if (firstname == "" || lastname == "" || emailaddress == "") {
    if (firstname == "") {
      alertText = "First Name\n";
    }
    if (lastname == "") {
      alertText = alertText + "Last Name\n";
    }
    if (emailaddress == "") {
      alertText = alertText + "Email Address";
    }
    alert("Please complete the following fields:\n\n" + alertText);
    return false;
  } else {
    return true;
  }
}
// SUBMIT ALBUM COPYRIGHT ALERT
function copyrightAlert() {
  var r = confirm("By submitting your lightbox, you agree to all of our terms of use and will not violate any applicable copyright laws.");
  if (r == true) {
    return;
  } else {
    //alert("Your album was not submitted because you did not agree.");
    return false;
  }
}