﻿//rme = Replay Main Events
var leftDialogPos = 330;
    var title = "";
    
    $(window).load(function() {    
      
      $('#il').fadeIn(1000);
      setTimeout(function() {
        $('#il').fadeOut(1000)
      }, 4000);
      
      setTimeout(function() {
        $('#im').fadeIn(1000)
      }, 2000);
      
      setTimeout(function() {
        $('#im').fadeOut(1000)
      }, 6000);
      
      $('li').hover(function () {
        $(this).animate({opacity:0.5, speed: 500});
      }, function() {
        $(this).animate({opacity:1, speed: 500});
      });
      
      $('#menu ul li#facebook').click(function() {
        window.open('http://www.facebook.com/pages/Replay/46465284618');
        return false;
      });
      
      $('#menu ul li:not(#facebook)').click(function() {
        var isLook = ($(this).attr('id') == 'look');
        var isBook = ($(this).attr('id') == 'write');
        var isListen = ($(this).attr('id') == 'listen');
        var isHome = ($(this).attr('id') == 'home');
        
        if(!isLook && !isListen) { 
          //dialog loading
          if($('#gallery').css('display') != "none")
            $('#gallery').hide('drop');
          $('#dialog').html('<div style="width:90%; text-align:center;"><img src="img/loading3.gif" alt="loading"  /></div> '); 
        } else if(!isListen) {
          //look was clicked
          if($('#dialog').parents('.ui-dialog:first').css('display') != "none") 
            $('#dialog').dialog('close');
          $('#gallery').show('drop');
        }
        
        if(!isListen) {
          //every option but listen and look
          switch($(this).attr('id')) {
            case 'home':
              //title = 're-play.be';
              //resizeDialog(200, 100);
              $('#dialog').dialog('close');
              break;
            case 'news':
              title = 'het.laatste';
              resizeDialog(350, 300);
              break;
            case 'about':
              title = 'over.ons';
              resizeDialog(400, 400);
              break;
            case 'play':
              title = 'we.spelen';
              resizeDialog(400, 300);
              break;
            case 'glorious':
              title = 'glorieus';
              resizeDialog(200, 400);
              break;
            case 'ask':
              title = 'vraag';
              resizeDialog(160, 240);
              break;
            case 'write':
              title = 'schrijf';
              resizeDialog(430, 210);
              break;
            case 'links':
              title = 'niet.rechts';
              resizeDialog(240, 150);
              break;
          }
          
          switch($(this).attr('id')) {
            case 'about':
              $.get('ashx/about.txt', function(data) {
                $('#dialog').dialog('option', 'buttons', {});
                $('#dialog').html('<p>' + data + '</p>');
              });
              break;
            case 'look':
              $('#musicplayer').parents('.ui-dialog:first').animate({left: 70, top: 30}, 500);
              $.get('ashx/gallery.txt', function(data) {
                $('#gallery').html(data);
                $(function(){
              $("#g1").mbGallery( {
                  galleryMaxWidth:0,
                  galleryWidth:100,
                  galleryHeight:100,
                  galleryColor:"transparent",
                  galleryFrameBorder: 12,
                  galleryFrameColor:"#fff",
   
                  thumbStripPos: "right",
                  thumbStripWidth:250,
                  thumbSelectColor: "#fff",
                  thumbOverColor : "#cccccc",
                  thumbStripColor: "#000",
                  thumbsBorder: 3,
                  thumbHeight:50,
                  headerOpacity: 0.9,
   
                  labelColor: "#333333",
                  labelColorDisactive:"#333333",
                  labelTextColor: "#ffffff",
                  labelTextSize:"11px",
                  labelHeight:20,
   
                  startFrom:0,
                  fadeTime: 200,
                  autoSlide:true,
                  slideTimer: 3000,
                  autoSize : true,
                  iconFolder: "img/white",
                  startTimer:0
              }
                      )}
                  );
              });
              break;
            default:
                $.get('ashx/reqdata.ashx?part=' + $(this).attr('id'), function(data) {
                  $('#dialog').html('<p>' + data + '</p>');
                  if(isBook) {
                    $('#dialog').dialog('option', 'buttons', {"gastenboek tekenen" : function() {
                      $.get('ashx/gbform.txt', function(data) {
                        $('#gbform').html(data);
                        $('#gbform').dialog({
                                              show: 'drop',
                                              hide: 'fold',
                                              bgiframe : true,
                                              modal : true, 
                                              width : 340,
                                              buttons : {
                                                          "verzenden" :
                                                          function() { sendMessage(); },
                                                          "annuleren" : 
                                                          function() { $('#gbform').dialog('close'); }
                                                        }
                                           });
                        $('#gbform').dialog('open');
                      });
                    }});          
                  } else {  
                    $('#dialog').dialog('option', 'buttons', {});          
                  }
                }); 
                break; 
          }
          if(! isLook && ! isHome) {
            $('#dialog').dialog('option', 'title', title);
            $('#dialog').dialog('option', 'position', [leftDialogPos,'center']);
            $('#dialog').dialog('open');
          }
        
        //listen was clicked          
        } else {
          //open mp3 player
          //hack to ensure margin-bottom for collapsing
          $('#musicplayer').css('margin-bottom', '0px');
          $('#musicplayer').dialog('open');
              //check if collapsed, when hovering: display
              $('#musicplayer').parents('div').hover(function() {
                if($('#musicplayer').css('margin-top')=='-140px') {
                  $('#musicplayer').css('margin-top', '0px');
                  $('#musicplayer').css('margin-bottom', '-10px');
                }
              }, function() {
                if($('#musicplayer').css('margin-top')=='0px') {
                  $('#musicplayer').css('margin-top', '-140px');
                  $('#musicplayer').css('margin-bottom', '20px');
                }
              });
          if($('#dialog').parents('.ui-dialog:first').css('display') != "none") {
            placeListen();
          }
          
          if($('#gallery').css('display')!="none") {
            $('#musicplayer').parents('.ui-dialog:first').animate({left: 70, top: 30}, 500);
          }
        }
      });
      
      
      
      //init the dialog
      $('#dialog').dialog({ 
                            focus : false,
                            autoOpen : false,
                            bgiframe : true,
                            resizable : false
                         });
      //init the mp3 player
      $('#musicplayer').dialog({
                            bgiframe:true,
                            autoOpen:false, 
                            width: 215, 
                            height:100,
                            resizable : false,
                            stack: true,
                            zIndex: 3999
                          });
      //preloading the images from gallery
      $.preloadImages("img/loading3.gif", "img/white/loaded.gif", "img/white/next.gif", "img/white/play.gif", "img/white/prev.gif", "img/white/separator.gif",
                      "img/white/stop.gif", "img/white/thumb.gif", 
		      "img/gallery/IMG_876_th.JPG", "img/gallery/IMG_881.JPG",
                      "img/gallery/IMG_840_th.JPG", "img/gallery/IMG_853_th.JPG", "img/gallery/IMG_900_th.JPG", 
                      "img/gallery/IMG_911_th.JPG", "img/gallery/IMG_939_th.JPG", "img/gallery/IMG_998_th.JPG", 
                      "img/gallery/IMG_999_49_th.JPG");
    });
    
    
    var resizeDialog = function(w, h) {
      $('#dialog').parents('.ui-dialog:first').animate(
        {width:w, height:h, left: leftDialogPos, top: $('#container').position().top - (h/2)}, 500,
        function() {
          $('#dialog').height(h - 45);
          if($('.ui-dialog-buttonpane').height() > 0)
            $('#dialog').parents('.ui-dialog').animate({height : ($('#dialog').parents('.ui-dialog').height() + $('.ui-dialog-buttonpane').height() + 10)});
          placeListen();
        }
      );
      
    }
    

    
    var placeListen = function() {
      $('#musicplayer').parents('.ui-dialog:first').animate({
                  left: leftDialogPos + $('#dialog').parents('.ui-dialog:first').width() + 30,
                  top: $('#container').position().top - ($('#musicplayer').parents('.ui-dialog:first').height() / 2 )
                }
               , 500,
               function() {
                var pos = $('#musicplayer').parents('.ui-dialog:first').position().left + $('#musicplayer').parents('.ui-dialog:first').width();
                if(pos > $(window).width()) {  
                  $('#musicplayer').css('margin-top', '-140px');
                  $('#musicplayer').css('margin-bottom', '20px');
                  //if listen dialog is out of window sight: replace to top of other dialog
                  if($('#dialog').parents('.ui-dialog:first').position().top > $('#musicplayer').parents('.ui-dialog:first').height() + 20) {
                    $('#musicplayer').parents('.ui-dialog:first').animate({
                        left: $(window).width() - $('#musicplayer').parents('.ui-dialog:first').width() - 30,
                        top: $('#dialog').parents('.ui-dialog:first').position().top - $('#musicplayer').parents('.ui-dialog:first').height() - 10
                      }, 500);
                  } else {
                    //else move to default left top position
                    $('#musicplayer').parents('.ui-dialog:first').animate({left: 70, top: 30}, 500);
                  }
                } else {
                  //$('#musicplayer').show();
                  $('#musicplayer').css('margin-top', '0px');
                  $('#musicplayer').css('margin-bottom', '-20px');
                }
               });
    }
    
    var toName, toMail, toReaction
    var sendMessage = function() {
      //clearing possible errors
      clearInterval(toName);
      clearInterval(toMail);
      clearInterval(toReaction);
      $('#gbform #errName').fadeOut('fast');
      $('#gbform #errMail').fadeOut('fast');
      $('#gbform #errReaction').fadeOut('fast');
      
     var validated = true;
     
     name = $('#gbform #name').val();
     email = $('#gbform #email').val();
     reaction = $('#gbform #reaction').val().replace('\n', '<br />').replace('replay', '<b>Replay</b>').replace('Replay', '<b>Replay</b>');
    
     //checking mandatory fields
     if(name == "") {
      $('#gbform #errName').fadeIn('fast');
      validated=false;
      toName = setTimeout("$('#gbform #errName').fadeOut('slow')", 5000);
     }
     if(reaction == "") {
      $('#gbform #errReaction').fadeIn('fast');
      validated=false;
      toReaction = setTimeout("$('#gbform #errReaction').fadeOut('slow')", 5000);
     }
     //checking mail
     var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
     if(email != "" && !pattern.test(email)) {
      $('#gbform #errMail').fadeIn('fast');
      validated=false;
      toMail = setTimeout("$('#gbform #errMail').fadeOut('slow')", 5000);
     }
     
     if(validated) {
      $.get('ashx/reqdata.ashx?part=insertbook&n=' + name + '&m=' + email + '&r=' + reaction,
        function(data) { 
            $('#gbform #name').val("");
            $('#gbform #email').val("");
            $('#gbform #reaction').val("");
            $('#gbform').dialog('close');
            
            $('#insert').append(data);
         }
      );
     }
    }
    
jQuery.preloadImages = function()
{
  var imgs = new Array(arguments.length + 1)
  for(var i = 0; i<arguments.length; i++)
  {
    imgs[i] = new Image();
    imgs[i].src = arguments[i];
  }
}