﻿/*
 * devPytania JS
 *
 * Copyright (c) 2010 Marek Stój, Rafał Legiędź
 * All rights reserved.
 * Wszelkie prawa zastrzeżone.
 */

function addMailtoToKontaktAtDevmedia() {
  $('#answers div.answer a[href="http://www.devmedia.pl"]').attr('href', 'mailto:kontakt@devmedia.pl');
}

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.devpytania.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, " "));
}

$(document).ready(function () {
  var urlPath = window.location.pathname;

  $('#ask-question-form').insertAfter('#hmenus').show();

  if (urlPath.match(/^\/questions\/577\/najlepsze-krzeslo-dla-programisty/i) != null) {
    addMailtoToKontaktAtDevmedia();
  } else if (urlPath.match(/^\/questions\/ask/i) != null) {
    $('#ask-question-form').hide();
    $('#header').css({ height: "103px" });

    var question = getParameterByName('question');

    $('#title').val(question);
  } else if (urlPath.match(/^\/questions\/[0-9]+/i) != null) {
    $('h2 a.post-tag img').css({ marginTop: "4px" });
  }

  appendDevMediaSideBar('left');
  initializeSidebarUserFlair();

  $('#nav-ask').hide();

  // handler for ask question form submit
  $('#ask-question-form form').submit(function () {
    var question = $(this).children('input[type="text"]').eq(0).val();

    if (question == 'Jakie jest Twoje pytanie?') {
      question = '';
    }

    var encodedQuestion = encodeURIComponent(question);
    $(location).attr('href', 'http://devpytania.pl/questions/ask?question=' + encodedQuestion);

    return false;
  });

});
