var curr_cat = 0; // home
var curr_prod = 0; // home
var prod_img_count = new Array();
var curr_prod_img = 1;
var bg_img_interval;
var bg_img_playing = true;
var groen = "border-color: #00bb00";
var rood = "border-color: #bb0000";
var contact_check_timer;
var subscr_check_timer;
var media_count = 0;
var media_is_open = false;
var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

/*
1 contact
2 contact ingevuld
3 gallery
4 subscribed
5 unsubscribed
6 unsubscribe
7 availability
8 video (wordt op speciale manier geopend! -> openMedia()
9 witte overlay (moet altijd de laatste blijven)
*/
var special_page_count = 9;

document.observe("dom:loaded", function() {
  for (var i = 1; i <= cat_count; i++) {
    new Effect.Opacity('thumb_category_' + i, { from: 1, to: .5, duration: .5 });
    $('products_category_' + i).hide();
    $('triangle_category_' + i).hide();
  }
  for (var i = 1; i <= prod_count; i++) {
    $('product_' + i).hide();
    if (prod_img_count[i] != undefined) {
      for (var j = 2; j <= prod_img_count[i]; j++) {
        $('prod_'+i+'_img_'+j).hide();
      }
    }
  }
  for (var i = -1; i >= -special_page_count; i--) {
    $('page_' + i).hide();
  }
  
  if (window.location.hash == "" || window.location.hash == "#")
    window.location.hash = "#" + init_prod;
  
  setInterval('checkAnchor()', 250);
  setInterval('moveLichtkrant()', 201);
});

function openCategory(cat) {  
  var catArr = cat.id.split("_");
  var prev_cat = curr_cat;
  curr_cat = parseInt(catArr[2]);
  var j = 0;
  
  if (curr_cat == prev_cat) {
    goHome();
    return;
  }
  
  for (var i = 1; i <= cat_count; i++) {
    if (i != curr_cat) {
      if (prev_cat == 0)
        setTimeout('new Effect.Morph("category_" + ' + i + ', { style: "height: 80px", duration: .5 })', j*200);
      else {
        setTimeout('new Effect.Morph("category_" + ' + i + ', { style: "height: 80px", duration: .5 })', 400);
        if (prev_cat >= 3)
          new Effect.Shrink("products_category_" + prev_cat, { duration: .4, direction: 'bottom-right' });
        else
          new Effect.Shrink("products_category_" + prev_cat, { duration: .4, direction: 'bottom-left' });
        new Effect.Fade("triangle_category_" + prev_cat, { duration: .2 });
      }
      new Effect.Opacity("thumb_category_" + i, { to: .5, duration: .4 });
      new Effect.Morph("overlay_text_category_" + i, { style: "color: #333333", duration: .5 });
    } else {
      setTimeout('new Effect.Morph("category_" + ' + i + ', { style: "height: 399px", duration: .5 })', 300);
      if (i >= 3)
        setTimeout('new Effect.Grow("products_category_" + ' + i + ', { duration: .4, direction: "bottom-right" })', 700);
      else
        setTimeout('new Effect.Grow("products_category_" + ' + i + ', { duration: .4, direction: "bottom-left" })', 700);
      setTimeout('new Effect.Appear("triangle_category_" + ' + i + ', { duration: .2 })', 700);
    }
    j++;
  }
}

function goHome() {
  var prev_cat = curr_cat;
  curr_cat = 0;
  var j = 0;
  
  closeMedia();
  
  if (prev_cat == 0)
    return;
  
  if (prev_cat >= 3)
    new Effect.Shrink("products_category_" + prev_cat, { duration: .4, direction: 'bottom-right' });
  else
    new Effect.Shrink("products_category_" + prev_cat, { duration: .4, direction: 'bottom-left' });
  
  new Effect.Fade("triangle_category_" + prev_cat, { duration: .2 });
  
  for (var i = 1; i <= cat_count; i++) {
      setTimeout('new Effect.Morph("category_" + ' + i + ', { style: "height: 399px", duration: .5 })', j*200);
      j++;
      new Effect.Opacity("thumb_category_" + i, { to: .6, duration: .4 });
      new Effect.Morph("overlay_text_category_" + i, { style: "color: #333333", duration: .5 });
  }
  
  pageTracker._trackPageview("pages/0");
}

function openPage(page) {
  if (page > 0 && page <= prod_count) {
    new Effect.Grow("product_" + page, { duration: .5 });
    playBgImg();
  }
  else if (page < 0 && page >= -special_page_count) {
    new Effect.Grow("page_" + page, { duration: .5 });
  }
}

function closePage(page) {
  if (page > 0 && page <= prod_count) {
    new Effect.Shrink("product_" + page, { duration: .5 });
    curr_prod_img = 1;
    stopBgImg();
  }
  else if (page < 0 && page >= -special_page_count) {
    new Effect.Shrink("page_" + page, { duration: .5 });
  }
}

function checkAnchor() {
  if (window.location.hash.length <= 1) {
    window.location.hash = "#0";
    return;
  }
  
  if (window.location.hash != "#" + curr_prod) {
    closePage(curr_prod);
    curr_prod = parseInt(window.location.hash.substring(1));
    openPage(curr_prod);
    if (curr_prod != 0)
      pageTracker._trackPageview("pages/" + curr_prod);
  }
}

function openMedia(media) {
  media = fixEmbeddedVideo(media);
  new Effect.Grow("page_-8", { duration: .5 });
  setTimeout('$("video_frame").update("'+media+'")', 500);
  media_is_open = true;
}

function closeMedia() {
  if (media_is_open) {
    $("video_frame").update("");
    new Effect.Shrink("page_-8", { duration: .5 });
    media_is_open = false;
  }
}

function rotateBgImg(forward) {
  if (curr_prod > 0 && curr_prod <= prod_count && prod_img_count[curr_prod] != undefined && prod_img_count[curr_prod] > 1) {
    var prev_prod_img = curr_prod_img;
    if (forward)
      curr_prod_img++;
    else
      curr_prod_img--;
    
    if (curr_prod_img > prod_img_count[curr_prod])
      curr_prod_img = 1;
    if (curr_prod_img <= 0)
      curr_prod_img = prod_img_count[curr_prod];
    
    if ($('prod_'+curr_prod+'_img_'+curr_prod_img) != undefined)
      Effect.Appear('prod_'+curr_prod+'_img_'+curr_prod_img, { duration: 1 });
    if ($('prod_'+curr_prod+'_img_'+prev_prod_img) != undefined)
      setTimeout("Effect.Fade('prod_"+curr_prod+"_img_"+prev_prod_img+"', { duration: 1 });", 500);
  }
}

function nextBgImg() {
  clearInterval(bg_img_interval);
  rotateBgImg(true);
  if (bg_img_playing)
    playBgImg();
}

function prevBgImg() {
  clearInterval(bg_img_interval);
  rotateBgImg(false);
  if (bg_img_playing)
    playBgImg();
}

function stopBgImg() {
  bg_img_playing = false;
  clearInterval(bg_img_interval);
  if ($("prod_"+curr_prod+"_play") != undefined) {
    $("prod_"+curr_prod+"_play").show();
    $("prod_"+curr_prod+"_stop").hide();
  }
}

function playBgImg() {
  bg_img_playing = true;
  bg_img_interval = setInterval('rotateBgImg(true)', 5000);
  if ($("prod_"+curr_prod+"_play") != undefined) {
    $("prod_"+curr_prod+"_stop").show();
    $("prod_"+curr_prod+"_play").hide();
  }
}

function hoverMenu(button) {
  //if (page != p && page != p + '_' + q)
    new Effect.Opacity("thumb_" + button.id, { to: 1, duration: .4 });
    new Effect.Morph("overlay_text_" + button.id, { style: "color: #880000", duration: .5 });
}

function unhoverMenu(button) {
  var catArr = button.id.split("_");
  if (catArr[0] == "category" && curr_cat == parseInt(catArr[catArr.length - 1]))
    return;
  //if (page != p && page != p + '_' + q)
    new Effect.Opacity("thumb_"+button.id, { to: .6, duration: .4 });
    new Effect.Morph("overlay_text_" + button.id, { style: "color: #333333", duration: .5 });
}



var hoverLicht = false;

function hoverLichtkrant() {
  hoverLicht = true;
}

function unhoverLichtkrant() {
  hoverLicht = false;
}

function moveLichtkrant() {
  if ($('lichtkrant') == undefined)
    return;
  
  if ($('lichtkrant_text').getWidth() + $('lichtkrant_text').viewportOffset()[0] <= 0)
    new Effect.Move('lichtkrant_text', { x: 800, y: 0, duration: 0.1, transition: Effect.Transitions.full, mode: 'absolute' } );
  else if (!hoverLicht)
    new Effect.Move('lichtkrant_text', { x: -15, y: 0, duration: 0.2, transition: Effect.Transitions.linear, mode: 'relative' } );
}



function startFormValidator() {
  contact_check_timer = setInterval('validateForm()', 1000);
}
function stopFormValidator() {
  clearInterval(contact_check_timer);
}
function validateForm() {
  var naam = false;
  var email = false;
  var telefoon = false;
  var bericht = false;
  
  if ($('contact_email').value.match(emailRegEx)) {
    email = true;
    new Effect.Morph('contact_email', { style: groen, transition: Effect.Transitions.full });
  }
  else
    new Effect.Morph('contact_email', { style: rood, transition: Effect.Transitions.full });
  
  if ($('contact_naam').value.length > 0) {
    naam = true;
    new Effect.Morph('contact_naam', { style: groen, transition: Effect.Transitions.full });
  }
  else
    new Effect.Morph('contact_naam', { style: rood, transition: Effect.Transitions.full });
  
  if ($('contact_telefoon').value.length > 0) {
    telefoon = true;
    new Effect.Morph('contact_telefoon', { style: groen, transition: Effect.Transitions.full });
  }
  else
    new Effect.Morph('contact_telefoon', { style: rood, transition: Effect.Transitions.full });
  
  if ($('contact_bericht').value.length > 0) {
    bericht = true;
    new Effect.Morph('contact_bericht', { style: groen, transition: Effect.Transitions.full });
  }
  else
    new Effect.Morph('contact_bericht', { style: rood, transition: Effect.Transitions.full });
  
  if (naam && email && telefoon && bericht) {
    $('contact_submit').disabled = false;
    return true;
  }
  else {
    $('contact_submit').disabled = true;
    return false;
  }
}

function startSubscriptionValidator() {
  subscr_check_timer = setInterval('validateSubscription()', 1000);
}
function stopSubscriptionValidator() {
  clearInterval(subscr_check_timer);
}
function validateSubscription() {
  if ($('subscription_email').value.match(emailRegEx)) {
    $('subscription_submit').disabled = false;
    return true;
  }
  else {
    $('subscription_submit').disabled = true;
    return false;
  }
}



function fixEmbeddedVideo(embedCode) {
   if (embedCode && embedCode.toLowerCase().indexOf('classid') == -1) {
      var objPos = embedCode.toLowerCase().indexOf('object ') + 'object '.length;
      return embedCode.substr(0, objPos) + 'classid=\\\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\\\" ' + embedCode.substr(objPos);
   } else {
      return embedCode;
   }
}
