/*
* common.js 
* - set design and layout stuff. Depends on jQuery.
* - common functions 
* started by dprelec, 2010-06-02
*/


/* set custom background on site load and fix design issues */
function set_background (url) {
  $(document).ready(function() {
    var root = 'http://www.oglasnik.hr/redesign/images/';
      var bkg = root + 'spacer.gif';
    var bkg_logo = root + 'podloga_header_sredina_prazno.png';

    // change header for IE6
//    if ($.browser.msie && $.browser.version == 6) {
//      bkg_logo = root + 'podloga_header_sredina_prazno.gif';
//    }

    // apply design fixes
    $('body').css('backgroundImage', 'url(' + url + ')');
    $('body').css('backgroundPosition', 'center 0px');
    $('td.sjena_lijevo').css('backgroundImage', 'none');
    $('td.sjena_desno').css('backgroundImage', 'none');
    $('td.sjena').css('backgroundImage', 'none');
    $('td.shadow_left').attr('width', 19);
    $('td.shadow_background').css('backgroundImage', 'none');
    $('div.shadow_background').css('backgroundImage', 'none');
    $('div.footer').css({
        'width' : '991px',
        'backgroundImage' : 'url(/redesign/images/podloga_main_shadowless.gif)',
        'backgroundRepeat' : 'repeat-y',
        'backgroundPosition' : 'center top'
    });
    $('td.middle_shadowless').css('backgroundColor', 'white');
    $('img.shadow').attr('src', bkg);
    $('img.shadow').attr('width', 18);
    $('img.menu_patch_left').attr('src','/redesign/images/menu_zakrpa_lijevo_branding.gif');
    $('img.menu_patch_right').attr('src','/redesign/images/menu_zakrpa_desno_branding.gif');
    $('img.menu_patch_left').attr('width', 27);
    $('img.menu_patch_right').attr('width', 24);
    $('td.menu_patch_left').css('text-align', 'right');
    $('td.menu_patch_right').css('text-align', 'left');
    $('td#header_main').css('backgroundImage', 'url(' + bkg_logo + ')');
  });
}


/* set fixed non-scrollable background */
function set_fixed_background (url) {
  set_background(url);
  $(document).ready(function() {
    $('body').css('backgroundAttachment', 'fixed');
  });
}


/* send ajax image click for display ads */
function img_click (id) {
  $.ajax({
    'type'    : 'GET',
    'url'     : '/click?id=' + id,
    'success' : function (res) { },
    'error'   : function ()    { }
  });
}


/* checking password input in new password */
var PassChecker = (function () {
  var checker = {};
  checker.min_pass_len = 6;

  checker.msg = {};
  checker.msg.pass_len = 'Lozinke su prekratke. Unesite minimalno ' + checker.min_pass_len + ' znakova.'
  checker.msg.pass_eq = 'Lozinke se ne poklapaju. Molimo provjerite unos!';
  checker.msg.clear = '';

  checker.ids = {};
  checker.ids.p1 = 'p1';
  checker.ids.p2 = 'p2';
  checker.ids.msg = 'msg';
  checker.ids.form = 'pass_change';

  /* check password equality */
  checker.pass_eq = function (p1, p2) {
    if (p1 == p2) {
      return true;
    }
    else {
      return false;
    }
  }

  /* check password length */
  checker.pass_len_ok = function (p1, p2) {
    if (p1.length < checker.min_pass_len || p2.length < checker.min_pass_len) {
      return false;
    }
    else {
      return true;
    }
  }

  /* check password input and submit if ok */
  checker.check_and_submit = function () {
    var p1 = $('#' + checker.ids.p1).attr('value');
    var p2 = $('#' + checker.ids.p2).attr('value');

    if (!checker.pass_eq(p1, p2)) {
      checker.message(checker.msg.pass_eq);
      return false;
    }

    if (!checker.pass_len_ok(p1, p2)) {
      checker.message(checker.msg.pass_len);
      return false;
    }
    $('#' + checker.ids.form).submit();
  }

  /* clear error message */
  checker.clear_err_msg = function () {
    checker.message(checker.msg.clear);
  }

  /* set message */
  checker.message = function (msg) {
    $('#' + checker.ids.msg).html(msg);
  }

  return checker;
}());


/* check search form seach keyword is bigger than 1 character if entered */
function check_search_word () {
  var word = document.search_form.q.value;
  if (word) {
    word = word.replace(/^\s+/, '');
    word = word.replace(/\s+$/, '');
    if (word.length == 1) {
      $("#search_err").html("Pojam mora biti dugačak najmanje dva znaka!");
      $("#q").keypress(function() { $("#search_err").html(''); });
    }
    else {
      document.search_form.submit();
    }
  }
  else {
    document.search_form.submit();
  }
}


function setFloatboxOptions() {
  fb.enableCookies = 'false';
  fb.resizeDuration = 0;
  fb.imageFadeDuration = 0;
  fb.overlayFadeDuration = 0;
  fb.navType = 'upper';
  fb.outsideClickCloses = 'true';
  fb.strImageCount = 'Slika %1 od %2';
  fb.showClose = 'true';
  fb.showUpperNav = 'always';
  fb.theme = 'blue';
  fb.strHintNext = 'sljedeća';
  fb.strHintPrev = 'prethodna';
  fb.strHintClose = 'zatvori';
  fb.upperNavPos = 1;
}


var orig_charset ='';
function doccharset () {
    if( typeof( document.characterSet ) != "undefined" ){
        return document.characterSet ;
    } else if( typeof( document.charset ) != "undefined" ){
        return document.charset ;
    }
}


/* handle search form on main index page */
var SearchForm = (function () {
    var form_clicked = 0;
    var msg_init = 'Upišite pojam za pretraživanje...';
    var msg_err = 'Pojam mora biti dugačak najmanje dva znaka!';

    var self = {};

    self.init = function () {
      if ($("#q").attr('value') == '') {
        $("#q").attr('value', msg_init);
      }
      $("#q").focus(
        function() {
          if ($(this).attr('value') == msg_init) {
            $(this).attr('value', '');
          }
          $(this).css({'color' : 'black'});
          $("#search_err").html('');
          form_clicked = 1;
        }
      ).blur(
        function () {
          if ($(this).attr('value') == '') {
            $(this).attr('value', msg_init);
            $(this).css({'color' : 'gray'});
            form_clicked = 0;
          }
        }
      );
      
      $("#sbm").mousedown(
        function () { 
          $(this).css({'margin-left' : '10px', 'margin-top' : '1px'});
        }
      ).mouseup(
        function () {
          $(this).css({'margin-left' : '9px', 'margin-top' : '0px'});
        }
      ).blur(
        function () {
          $(this).css({'margin-left' : '9px', 'margin-top' : '0px'});
        }
      ).click(
        function() {
          if (submit_form()) {
            $("#search_form").submit();
          }
        }
      );

      function submit_form () {
        var word = $("#q").attr('value');
        if (word && word != '') {
          word = word.replace(/^\s+/, '');
          word = word.replace(/\s+$/, '');
          $("#q").attr('value', word);
          if (word.length < 2) {
            $("#search_err").html(msg_err);
            return false;
          }
          if (word != msg_init) {
            return true;
          }
        }
        return false;
      }

      $("#search_form").submit(submit_form);

    };

    return self;
})();


/* if banner is loaded for the given element, call fn() */
jQuery.fn.bannerLoad= function (fn) {
  var event = 'resize',
      dt = 500,
      max = 20,
      h1 = 0, 
      hn = $(this).height();

  var poll = function (elem) {
    max = max - 1;
    if (max >= 0) {
      hn = $(elem).height();
      if (h1 < hn) {
        $(elem).trigger(event);
        h1 = hn;
      }
      else {
        setTimeout(function(){poll(elem)}, dt);
      }
    }
  };

  var id = '#'+ $(this).attr('id');
  $(this).bind(event, fn);
  poll(id);
};



