//var REPEAT_BLOCK = "inline";

function linkingSetup() {
    DWRUtil.addOptions("MonthSelect", ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]);
    $("MonthSelect").onchange = fillDaySelect;
  
    $("MonthSelect").selectedIndex = 0;

    fillYearSelect();
    $("YearSelect").onchange = fillDaySelect;

    fillDaySelect();
    fillWeekSelect();
    fillHourSelect();
    fillMinuteSelect();
  
    DWRUtil.addOptions("RepeatSelect", ["None", "Yearly", "Monthly", "Daily", "Weekly"]);
    $("RepeatSelect").onchange = linkRepeatChanged;

    document.getElementById("TitleInput").onchange = codeUpdate;
    document.getElementById("UrlInput").onchange = codeUpdate;
    document.getElementById("LogoInput").onchange = codeUpdate;
    document.getElementById("SoundInput").onchange = codeUpdate;
    document.getElementById("CalUrlInput").onchange = codeUpdate;

    document.getElementById("YearSelect").onchange = codeUpdate;
    document.getElementById("MonthSelect").onchange = codeUpdate;
    document.getElementById("DaySelect").onchange = codeUpdate;
    document.getElementById("WeekSelect").onchange = codeUpdate;
    document.getElementById("HourSelect").onchange = codeUpdate;
    document.getElementById("MinuteSelect").onchange = codeUpdate;

    document.getElementById("BackgroundInput").onchange = codeUpdate;
    
    linkingParseParameters();
    
    codeUpdate();
}

function linkRepeatChanged() {
  repeatChanged();
  codeUpdate();
}

function linkingParseParameters() {
  try {
    var search = location.search;
    if (search != null && search.length > 0) {
      var logo = getParameter(search, "logo", "");
      $("LogoInput").value = logo;
      var sound = getParameter(search, "alert", "");
      $("SoundInput").value = sound;
      var url = getParameter(search, "calendar", null);
      if (url != null) {
        $("CalUrlInput").value = url;
        $("TimeMethod").selectedIndex = 1;
        methodChanged();
        return;
      }
      //Adding Date Coundown
      var title = getParameter(search, "title", "Countdown");
      $("TitleInput").value = title;
      var repeat = parseInt(getParameter(search, "repeat", 0));
      $("RepeatSelect").selectedIndex = repeat;
      var timeString = getParameter(search, "time", null);
      if (timeString != null) {
        var numbers = timeString.split("_");
        $("YearSelect").value = parseInt(numbers[0]);
        $("MonthSelect").selectedIndex = parseInt(numbers[1], 10) - 1;
        fillDaySelect();
        $("DaySelect").value = parseInt(numbers[2], 10);
        $("HourSelect").selectedIndex = parseInt(numbers[3], 10);
        $("MinuteSelect").selectedIndex = parseInt(numbers[4], 10);
        var timeZone = parseInt(getParameter(search, "timeZone", 99));
        if (timeZone != 99) {
        }
        var date = new Date($("YearSelect").value, $("MonthSelect").selectedIndex, 
                            $("DaySelect").selectedIndex + 1, $("HourSelect").selectedIndex, 
                            $("MinuteSelect").selectedIndex);
        $("WeekSelect").selectedIndex = date.getDay();                    
        
      }
      repeatChanged();
      $("TimeMethod").selectedIndex = 0;
      methodChanged();
    }
  }
  catch (e) {
    alert("Failed to parse URL parameters: " + e);
    throw e;
  }
}

function codeUpdate() {
  var logo = $("LogoInput").value;
  var sound = $("SoundInput").value;
  var title = $("TitleInput").value;
  var url = $("UrlInput").value;
  var calendar = $("CalUrlInput").value;
  var repeat = $("RepeatSelect").selectedIndex;
  var background = document.getElementById("BackgroundInput").value;           
  if ($("TimeMethod").selectedIndex != 0) {
    var text = '<a href="http://www.countdownr.com?logo=' + encodeURIComponent(logo) 
               + '&amp;alert=' + encodeURIComponent(sound) 
               + '&amp;calendar=' + encodeURIComponent(calendar) 
               + '">Calendar</a>';
    $("Code").value = text;
    $("TestLink").innerHTML = text;
    
    text = '<a href="http://www.widgetop.com?addCalCountdown=' + encodeURIComponent(calendar) 
               + '&amp;alert=' + encodeURIComponent(sound) 
               + '&amp;logo=' + encodeURIComponent(logo) 
               + '" target="_blank"><img src="http://www.widgetop.com/images/AddToWidgetopTiny.png" alt="Calendar" style="border: 0px; vertical-align: middle"/></a>';
    $("CodeWidgetop").value = text;
    $("PreviewWidgetop").innerHTML = text;
    
    text = 'external.html?calendar=' + encodeURIComponent(calendar) 
               + '&amp;logo=' + encodeURIComponent(logo)
               + '&amp;alert=' + sound 
               + '&amp;background=' + encodeURIComponent(background)
               + '" frameborder="0" width="320" '
               + 'height="130" scrolling="no"><a href="http://www.countdownr.com">Countdownr</a></iframe>';    
  }
  else {
    if (title.length == 0) {
      title = "Event";
    }
    var time = getTimeString();
    var link = 'countdownr.com?logo=' + encodeURIComponent(logo) 
               + '&amp;alert=' + encodeURIComponent(sound) 
               + '&amp;time=' + encodeURIComponent(time) 
               + '&amp;title=' + encodeURIComponent(title) 
               + '&amp;repeat=' + encodeURIComponent(repeat) 
               + '&amp;url=' + encodeURIComponent(url);

    var text = '<a href="http://www.' + link + '">' + title + '</a>';
    document.getElementById("Code").value = text;
    $("TestLink").innerHTML = text;

    var text = "mailto:?subject=CountDownr: " + title 
    + "&body=Add to CountDownr: " 
    + encodeURIComponent("http://www." + link)
    + "<br><br>Add to iPhone: "
    + encodeURIComponent("http://m." + link);
    $("MailLink").href = text;

    text = '<a href="http://www.widgetop.com?addCountdown=' + encodeURIComponent(title) 
               + '&amp;alert=' + encodeURIComponent(sound) 
               + '&amp;time=' + encodeURIComponent(time) 
               + '&amp;logo=' + encodeURIComponent(logo) 
               + '&amp;repeat=' + encodeURIComponent(repeat) 
               + '&amp;url=' + encodeURIComponent(url) 
               + '" target="_blank"><img src="http://www.widgetop.com/images/AddToWidgetopTiny.png" alt="Countdown" style="border: 0px; vertical-align: middle"/></a>';
    $("CodeWidgetop").value = text;
    $("PreviewWidgetop").innerHTML = text;

    text = 'external.html?logo=' + encodeURIComponent(logo) 
               + '&amp;alert=' + encodeURIComponent(sound)
               + '&amp;time=' + encodeURIComponent(time) 
               + '&amp;title=' + encodeURIComponent(title) 
               + '&amp;repeat=' + encodeURIComponent(repeat) 
               + '&amp;url=' + encodeURIComponent(url) 
               + '&amp;background=' + encodeURIComponent(background)
               + '" frameborder="0" width="320" '
               + 'height="130" scrolling="no"><a href="http://www.countdownr.com">Countdownr</a></iframe>';    
  }             
  document.getElementById("CodeFrame").value = '<iframe src="http://www.countdownr.com/' + text;
  document.getElementById("Preview").innerHTML = '<iframe src="' + text;           
}

function methodChanged() {
  var isDate = $("TimeMethod").selectedIndex == 0;//$("TimeMethod").checked;
  $("LinkDateSettings").style.display = isDate ? "block" : "none";
  $("LinkCalSettings").style.display = isDate ? "none" : "block";
  codeUpdate();
}

function emailLink() {
  if (current) {
    window.open("mailto:?subject=WorldWatchr: " + current.title 
    + "&body=Add to WorldWatchr: " 
    + encodeURIComponent("http://www.worldwatchr.com?add=" + encodeURIComponent(current.url) + "&title=" + encodeURIComponent(current.title))
    + "<br><br>Add to iPhone: "
    + encodeURIComponent("http://m.worldwatchr.com?add=" + encodeURIComponent(current.url) + "&title=" + encodeURIComponent(current.title)), "_self");
  }
}    