﻿/*
* antylama JS
*
* Copyright (c) 2010 Marek Stój, Rafał Legiędź
* All rights reserved.
* Wszelkie prawa zastrzeżone.
*/

function initializeSidebarUserFlair() {
  var currentUserId = g_currentUserId; // g_currentUserId comes from localize.js

  if (currentUserId == null) {
    currentUserId = 2;
  }

  $('#sidebar-user-flair a#sidebar-user-flair-link').html('<img src="http://dynamic.antylama.pl/users/flair/default/' + currentUserId + '.png" alt="" />');
}

// parses query string and retrieve a parameter value of given name
function getParameterByName(name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

// entry point
$(document).ready(function () {
  var urlPath = window.location.pathname;

  $('#ask-question-form').insertAfter('#hmenus').show();

  if (urlPath.match(/^\/questions\/ask/i) != null) {
    $('#ask-question-form').hide();
    $('#header').css({ height: "103px" });

    var question = getParameterByName('question');

    $('#title').val(question);
  }

  appendDevMediaSideBar('left');
  initializeSidebarUserFlair();

  $('#nav-ask').hide();

  // handler for ask question form submit
  $('#ask-question-form form').submit(function () {
    var question = $('#question').val();

    if (question == 'Jakie jest Twoje pytanie?') {
      question = '';
    }

    var encodedQuestion = encodeURIComponent(question);
    $(location).attr('href', 'http://antylama.pl/questions/ask?question=' + encodedQuestion);
    return false;
  });
});
