function parse_str(str){
	var glue1 = '=';
	var glue2 = '&';

	var array2 = str.split(glue2);
	var array3 = {};
	for(var x=0; x<array2.length; x++){
		var tmp = array2[x].split(glue1);
		array3[unescape(tmp[0])] = unescape(tmp[1]).replace(/[+]/g, ' ');
	}

	return array3;
}

/*$(window).load(function() {
  if ($('#full_photo img').length > 0) {
    var pleft = $('<div><a href="#"></a></div>').addClass('pnav pleft').hide().appendTo('#full_photo'),
      pright = $('<div><a href="#"></a></div>').addClass('pnav pright').hide().appendTo('#full_photo'),
      photo = $('#full_photo img'),
      poffset = photo.position();

    pleft.find('a').click(function() {
      var curr_photo = $('#photo_list_c li.selected');
      var prev_photo = curr_photo.prev();
      if (prev_photo.length > 0) {
        window.location.href = prev_photo.find('a').attr('href');
      }
      return false;
    }).height(photo.height());

    pleft.css({
      marginTop : '-' + photo.height() + 'px',
      marginLeft : poffset.left + 'px'
    }).show();

    pright.find('a').click(function() {
      var curr_photo = $('#photo_list_c li.selected');
      var next_photo = curr_photo.next();
      if (next_photo.length > 0) {
        window.location.href = next_photo.find('a').attr('href');
      }
      return false;
    }).height(photo.height());

    pright.css({
      marginTop : '-' + photo.height() + 'px',
      marginLeft : poffset.left + photo.width() - 30 + 'px'
    }).show();
  }
});
*/

$(function() {
  if($('#cities_of_ukraine').length > 0){
    var url = window.location.href, cur = '';
    $('#cities_of_ukraine li a').each(function(){
      var reg = new RegExp(RegExp.quote($(this).attr('href')), 'i');
      if (reg.test(url)) {
        cur = $(this);
      }
    });
    if(cur.length > 0){
      cur.parents('#cities_of_ukraine li').each(function(){
      var link = $(this).find('a:first');
      var item = link.next();
      if (item.length > 0) {
        if (item.is(':hidden')) {
          link.addClass('selected');
          item.show();
        }
      }
      });
    }
  };

  $('#filter_firm_form input:submit').click(function(){
    var fields = [],
    url = $('#filter_firm_form').attr('action');
    $('#filter_regionsearch, #filter_citysearch, #filter_keywordssearch').each(function(){
      if($(this).val().length > 0){
        var name = $(this).attr('name');
        var value = $(this).val();
        var obj = {name: name, value: value};
        fields.push(obj);
      }
    });
    
    if(fields.length > 0){
      url = url+'?'+decodeURIComponent($.param(fields));
    };
    
    window.location.href = url;
    return false;
  });
  
  $('#pvideo_manage_left').click(function() {
    var widget = $(this).closest('.widgets_pvideo_manage');
    var block = widget.find('.pvideo_manage .icontainer:visible');
    var link = block.find('a:first');
    var pvm_url = link.attr('pvm_url');
    var pvm_id = link.attr('pvm_id');
    $.getJSON(pvm_url, {
      nav : 'prev',
      id : pvm_id
    }, function(data) {
      link.attr({
        href : data.url,
        pvm_id : data.id
      }).find('img:first').attr({
        src : data.img_url,
        title : data.title,
        alt : data.alt
      });
      block.find('.title').text(data.title);
    });
  });

  $('#pvideo_manage_right').click(function() {
    var widget = $(this).closest('.widgets_pvideo_manage');
    var block = widget.find('.pvideo_manage .icontainer:visible');
    var link = block.find('a:first');
    var pvm_url = link.attr('pvm_url');
    var pvm_id = link.attr('pvm_id');
    $.getJSON(pvm_url, {
      nav : 'next',
      id : pvm_id
    }, function(data) {
      link.attr({
        href : data.url,
        pvm_id : data.id
      }).find('img:first').attr({
        src : data.img_url,
        title : data.title,
        alt : data.alt
      });
      block.find('.title').text(data.title);
    });
  });

  $('#main_menu li a').bind({
    click : function() {
      if($(this).attr('href') == '#' | $(this).attr('href').length == 0) {
        $('#main_menu li').removeClass('selected');
        var parent = $(this).parent();
        parent.addClass('selected');
        if (parent.children('ul:first').length) {
          $('#main_menu_dop').show().html($(this).children('ul:first').clone());
        } else {
          $('#main_menu_dop').html('');
        }
        return false;
      }
    }
  });

  if($('#main_menu').length > 0){
    var url = window.location.href,
      cur = $('#main_menu li:first a:first'),
      parent = cur.parent(),
      max_length = 0;
    $('#main_menu li a').each(function(){
      var reg = new RegExp(RegExp.quote($(this).attr('href')), 'i');
      if (reg.test(url)) {
        if($(this).attr('href').length > max_length) {
          cur = $(this);
          max_length = $(this).attr('href').length;
        }
      }
    });

    cur.parents('#main_menu li').each(function(){
      parent = $(this);
    });

    $('#main_menu li').removeClass('selected');
    parent.addClass('selected');
    if (parent.children('ul:first').length) {
      $('#main_menu_dop').show().html(parent.children('ul:first').clone());
    } else {
      $('#main_menu_dop').html('');
    }
  };

  $('#pvideo_manage_tabs li').hover(function() {
    var block = $($(this).find('a').attr('href'));
    if (block.is(':hidden')) {
      $('#pvideo_manage_tabs li').each(function() {
        var cblock = $($(this).find('a').attr('href'));
        if (cblock.attr('id') == block.attr('id')) {
          $(this).addClass('selected');
          cblock.show();
        } else {
          $(this).removeClass('selected');
          cblock.hide();
        }
      })
    }
  });

  $('#cities_of_ukraine li a').click(function() {
    var item = $(this).next();
    if (item.length > 0) {
      if (item.is(':hidden')) {
        $(this).addClass('selected');
        item.show();
      } else {
        $(this).removeClass('selected');
        item.hide();
      }
      return false;
    }
  });

  $('#list_css li a').click(function(){
    var item = $(this).next();
    if (item.length > 0) {
      if (item.is(':hidden')) {
        //$('#list_css li a').removeClass('selected');
        //$('#list_css li ul').hide();
        $(this).addClass('selected');
        item.show();
      } else {
        $(this).removeClass('selected');
        item.hide();
      }
      return false;
    }
  });
  
  $('#pvideo_manage_tabs li a').click(function() {
    return false;
  });

  $('#video_prev').click(function() {
    var curr_video = $('#video_playlist li.selected');
    var prev_video = curr_video.prev();
    if (prev_video.length > 0) {
      window.location.href = prev_video.find('a').attr('href');
    }
    return false;
  });

  $('#video_next').click(function() {
    var curr_video = $('#video_playlist li.selected');
    var next_video = curr_video.next();
    if (next_video.length > 0) {
      window.location.href = next_video.find('a').attr('href');
    }
    return false;
  });

  $('#category_list a').click(function(){
    if($(this).attr('href') == '#') {
      return false;
    }
  });
})

