function makeid()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

function replace_txtarea()
{
   $("li.repl").each(function () {
      var code = $(this).children("textarea").val();
      //alert(code);
      //$(this).replaceWith(code);
   });
}

//$(replace_txtarea);

var menu_is_clickable=true;

$(function () {
   $(".gal_wrapper").each(function () {
      if ( !$(this).children().length )
         $(this).remove();
   });
});

var prev_location_hash = window.location.hash;
//prev_location_hash = '#comment-4';
var hide2level = false;

$(function () {
   if (hide2level)
   {
      $(".nav_div > ul ul").remove();
   }
   var bn = 0;
   $(".uniform").each(function () {
      bn++;
      $("[name]", this).each(function () {
         $(this).attr("id", $(this).attr("name")+"_"+bn );
      });
      $(this).attr("id", $(this).attr("name")+"_"+bn );
   });
});
var dt_settings = {
   // how long will it take the dots to disappear
   dots_duration: 7500,
   // how many dots to display on sliders
   dots_count:    15,
   // which elements should have highslide attached
   highslide: 'a.pf, .post-i a.alignleft_left, .prettyPhoto',
   // options for hs.expand(...)
   highslide_gall: '.gallery_item:not(.single) a.shadow_light:not(.no_hs), a.single_hs, ul li a.hs_me'
   // options for hs gallery
};


// prettyPhoto options, deprecated!
var pf_options = {
   theme: 'light_rounded',
   gallery_markup: ''
};



$(function () {
   
   $(".pxs_slider").css('overflow', 'visible');
   
   // fix menu
 //    $(".pxs_thumbnails > li > ul").wrap('<div />');
  //   $(".pxs_thumbnails > li > div").append('<i />');
   //  $(".pxs_thumbnails > li > div > ul").show();
   //  $(".pxs_thumbnails > li > div > ul > li:first").addClass('first');
   
   $(".menu-item").each(function () {
      var cl = $(this).attr("class");
      var matches = /menu-item-(\d+)/.exec(cl);
      $(this).children("a").attr("id", "page_"+matches[1]);
   });
   
  //   $(".pxs_thumbnails li ul li.current_page_item").each(function () {
    //    var a = $(this).parent().parent().parent().children("a");
  //      a.addClass("selected").addClass("act");
   //  });

   $(".page_item, .page-item, .current_page_item").each(function () {
      var cl = $(this).attr("class");
      var matches = /page-item-(\d+)/.exec(cl);
      if (!matches)
      {  
         return;
      }
      var e = $(this).children("a");
      if (!e.attr("id"))
         e.attr("id", "page_"+matches[1]);
   });
   
   $(".pxs_thumbnails > li:first > a").each(function () {
      $(this).attr("id", "page_0");
   });
   
   // fix cursor
   $("a.nofollow").css('cursor', 'default');
   
   upd_ev();
});



var slider_auto = 0;



function upd_ev()
{

   
   $(".gallery").each(function () {
      var ch = $(this).children().length;
      var cols = ch;
      var pad = 16;
      var one_w = 304 + pad;
      var all_w = one_w * cols - pad;
      //all_w += 18;
      //all_w -= 1;
      $(this).css({
         'width': all_w+'px'
      });
      $(this).children(":eq("+(cols-1)+")").css('margin-right', 0);
      $(this).children(":last").css('margin-right', 0);
   });
   
   

	
	if (prev_location_hash)
	{
	   var prev_id = prev_location_hash.replace('#', '');
	   if (prev_id)
	   {
	      var elem = $("."+prev_id, $(".pxs_slider > li:first"));
	      if (!elem.length)
	         elem = $(".hash_"+prev_id, $(".pxs_slider > li:first"));
	      if (!elem.length)
	         elem = $("#"+prev_id, $(".pxs_slider > li:first"));
	      if (elem.length)
	      {
	         var parent_sc = elem.parents(".scroll-pane");
	         if (parent_sc.length)
	         {
	            var t = elem.offset().top;
	            t -= elem.parents(".post").offset().top;
	            parent_sc[0].scrollTo(t, 0);
	            //parent_sc.css( 'top', +"px" );
	         }
	      }
	   }
	   prev_location_hash = "";
	}
	
	var go_possible = true;
	$(".article_link .comments").unbind("click").click(function () {
	   if (!go_possible)
	      return false;
      var parent_sc = $(this).parents(".scroll-pane");
      if (parent_sc.length)
      {
         var elem = $(".hash_comments", $(this).parents(".post"));
         if (!elem.length)
            elem = $(".hash_respond", $(this).parents(".post"));
         var t = elem.offset().top;
         t -= $(this).parents(".post").offset().top;
         //t -= 30;
         //alert(t);
         if (t > 0)
         {
            go_possible = false;
            parent_sc[0].scrollTo(t, 0);
            setTimeout(function () {
               go_possible = true;
            }, 500);
         }
         //parent_sc.css( 'top', +"px" );
      }	   
	   return false;
	});
   
}



$(document).ready(function () {


   
   $(window).resize(function () {
      $(".pxs_slider").children().eq(0).css('margin-left', '0px');
   });
   
   if ($('#pxs_container').length)
   {
   
      var arrow_duration  = 300;
      var arrow_prev      = $( $(".pxs_slider").children()[0] );
      var arrow_left_init = "-"+$("#pxs_container div.desc").width()+"px";
      
      $(".pxs_slider").children().eq(0).css('margin-left', '-10px');
      
      /*
      $("#pxs_container div.desc:gt(0)").css({
         left: arrow_left_init
      });
      */
     
      $(".pxs_slider script").remove();
     
	   var sl = 0;
	   var link = '';
	   if (window.location.hash)
	      if (window.location.hash.match(/^\#.*$/))
	      {
	         sl = window.location.hash.replace('#', '');
	         if ( $("#for_"+sl).length )
	         {
	            var ind = $("#for_"+sl).attr("class");
					var matches = /li_(\d+)/.exec(ind);
					if (matches)
					{
					   ind = matches[1];
					   ind = parseInt( ind );
					}
					else
					{
					   alert("Error. Please report to Dream-Team");
					}
	            if (ind > 0)
	            {
	               link = sl;
                  sl = ind;
	            }
	            else
	            {

	            }
	         }
	      }
	   
	   $('#pxs_container').parallaxSlider({
	      auto: slider_auto,
	      slide: sl,
	      animDone: function (parent) {
	         /*
	         $("div.desc", parent).show().animate({
	            left: '0px'
	         }, {
	            duration: arrow_duration,
	            queue: false, 

	            complete: function () {
	               $("div.desc", arrow_prev).hide().css('left', arrow_left_init);
	               arrow_prev = parent;
	            }
	         });
	         */
	         $(".pxs_slider").show();
	      }
	   });
	   
	   $(window).resize(function () {
	      $(".pxs_slider > li").css({
	         'overflow': 'hidden',
	         'height'  : $(window).height()+"px",
	         'width'  : $(window).width()+"px"
	      });
		   $(".left-box").css({
	         'height'  : $(window).height()+"px",
	      });
	   }).trigger("resize");   
	   
      
      
      //  $(".pxs_thumbnails .menu-item-type-custom > a, .pxs_thumbnails li ul li > a").each(function () {
     //      $(this).unbind('click').click(function () {
     //         return true;
     //      });
     //   });
	   
	   if (link) {
	      $("#"+link).trigger("click");
	   }
	   
	   //$(".pxs_slider").show();
	
	}

   
   var blocks_speed_fade_in  = 300;
   var blocks_speed_fade_out = 300;
   

   ////////////////////////

   $(".post-i .alignleft_list i").css({
      display: 'block',
      opacity: 0
   });
   
   $(".post-i .alignleft_list").hover(function () {
      if ( !$(this).attr("href") )
         return;
      $("i",  $(this) ).animate({
         opacity: 1
      }, {
         duration: blocks_speed_fade_in,
         queue: false,
         complete: function () {
            if ($.browser.msie) this.style.removeAttribute('filter');
         }
      });
   }, function () {
      $("i", $(this) ).animate({
         opacity: 0
      }, {
         duration: blocks_speed_fade_out,
         queue: false
      });
   });
   
   ////////////////////////////////

   $(".info-block").css({
      display: 'none',
      opacity: 0
   });
   
   $(".inf").each(function () {
      var block = $(".info-block",  $(this).parents(".post-item") );
      var tout_h = false;
      function start_hide() {
         if (tout_h)
            clearTimeout(tout_h);
         tout_h = setTimeout(function () {
            //block.hide(); return;
            block.animate({
               opacity: 0
            }, {
               duration: blocks_speed_fade_out,
               queue: false,
               complete: function () {
                  $(this).css('display', 'none');
               }
            });
         }, 400);
      }
      $(this).hover(function () {
         if (tout_h)
            clearTimeout(tout_h);
         block.css('display', 'block').animate({
            opacity: 1
         }, {
            duration: blocks_speed_fade_in,
            queue: false,
            complete: function () {
               if ($.browser.msie) this.style.removeAttribute('filter');
            }
         });
      }, start_hide);
      block.hover(function () {
         if (tout_h)
            clearTimeout(tout_h);
      },
      start_hide);
   });
   
   upd_ev();


      

   
   if (1)
   {
      var popup_options2 = { top: 25 };
      
      var touts2    = new Array();
      var cur_elem2 = null;
      var n2=0;
      $("#mainmenu > li").each(function () {
         if ( !$(this).children("div").length )
            return;
         $(this).attr("n", ++n2).addClass("parent");
      });
      
      $("#mainmenu > li.parent").hover(function () {
         cur_elem2 = $("div", this);
         touts2[ parseInt($(this).attr("n")) ] = setTimeout(function () {
            if ($.browser.msie)
            {
               cur_elem2.show().css({
                  display:    'block',
                  bottom:        popup_options2.top+"px"
               });
            }
            else
            {
               cur_elem2.css({
                  opacity:    0,
                  display:    'block',
                  bottom: ( popup_options2.top + popup_options.jump_height )+"px"
               }).animate({
                  opacity:    1,
                  bottom:        popup_options2.top+"px"
               }, {
                  duration:   popup_options.show_duration,
                  queue:      false
               });
            }
         }, popup_options.tout);
      }, function () {
      
         if (touts2[ parseInt($(this).attr("n")) ]) clearTimeout(touts2[ parseInt($(this).attr("n")) ]);
      
         if ($.browser.msie)
         {
            $("div", this).hide();
         }
         else
         {
            $("div", this).animate({
               opacity:    0
            }, {
               duration:   popup_options.hide_duration,
               queue:      false,
               complete:   function () {
                  $(this).hide();
               }
            });
         }   
      });
   }
   
   $(".gal").attr("rel", "gal[g]");
   if ($.prettyPhoto && $(".gal").length)
   {
      $(".gal").each(function () {
         $(this).attr("rel", "gal[g]")
            .attr("title",  $(this).find("h4").text() );
      });
      $("a[rel=gal\\[g\\]]").prettyPhoto(pf_options);
   }
   
   $(".sh").each(function () {
      var now = 0;
      var maxnow = $(this).children(".item").length-1;
      var ee = $(this);
      $(this).parent().find(".larr, .rarr").click(function () {
         var the_now = now;
         if ( !$(this).hasClass('larr') ) now++; else now--;
         if (now<0) now = maxnow;
         if (now>maxnow) now=0;
         var now_h = ee.height();
         //$(".widget_arr").hide();
         ee.find(".item:eq("+the_now+")").fadeOut(300, function () {
            var gg = ee.find(".item:eq("+now+")");
            gg.show();
            ee.css({ height: 'auto' });
            var new_h = ee.height();
            gg.hide();
            ee.css({ height: now_h }).animate({ height: new_h }, { duration: 300, complete: function () {
               //$(".widget_arr").show();
            } });
            gg.fadeIn(300);
         });
         return false;
      });
   });
   
});


$(function () {
   $(".hash_go").click(function () {
      var h = $(this).attr("href");
      h = h.replace('/^.\#/', '');
      alert(h);
      if (h)
      {
         var e = $(".hash_"+h, $(this).parents(".post"));
         alert(e.length);
         if (e.length)
         {
            alert("ok");
            return false;
         }
      }
   });
   
});

$(function () {
   $(".slider_normal").each(function () {
      var parent_elem = $(this);
      
      var one_sl = dt_settings.dots_duration;
      var one_speed = 1500;
      var max_dots = dt_settings.dots_count;
      var dur_sliding = 500;
      
      var one_sl_timeout = false;
      var current_dots = max_dots;
      var first_run = true;

      if ( $(".cycle", parent_elem).children().length == 1 )
      {
         $(".nav_s", parent_elem).hide();
         var e = $(".cycle", parent_elem).children();
         e.show();
         if (e.hasClass('hide_slide'))
            $(".desc_outer", parent_elem).hide();
      }

      $(".nav_s_inner", parent_elem).html("");
      var j;
      for (j=0; j<current_dots; j++) {
         $(".nav_s_inner", parent_elem).append('<a><img src="'+$('input[name=dir_url]', parent_elem).val()+'/images/timer.png" alt="" /></a>');
      }
      
      var dots = $(".nav_s_inner", parent_elem).find("a");

      var prev_c = current_dots;
      
      var one_tick_t = (one_sl - one_speed) / max_dots;
      //one_tick_t = 500;
      
      function populate_ticks(cb) {
         return;
         if (current_dots > prev_c)
            dots.show();
         else
            dots.eq( dots.find(":visible").length-1 ).fadeOut(one_tick_t, cb);
         prev_c = current_dots;
      }
      
      var current_n = 0;
      
      function start_dots()
      {
         current_n++;
         var now_n = current_n;
         function run_dots()
         {
            if (current_n != now_n)
               return;
            //console.log("run dots");
            var vis = current_dots;
            if (vis == 0)
            {
               $(".nav_s_inner", parent_elem).hide();
               $(".cycle", parent_elem).cycle("next");            
            }
            else if (vis > 0)
            {
               var ee = dots.eq( vis-1 );
               if (!ee.is(":visible"))
                  return;
               ee.fadeOut(one_tick_t, run_dots);
            }
            current_dots--;
         }
         run_dots();
      }
      
      function tick_one() {
         return;
         if (one_sl_timeout)
            clearTimeout(one_sl_timeout);
         populate_ticks();
         current_dots--;
         one_sl_timeout = setTimeout(tick_one, one_tick_t);
         if (current_dots == -1)
         {
            $(".nav_s_inner", parent_elem).fadeOut(1, function () {
               $(".cycle", parent_elem).cycle("next");
            });
         }
      }
      
      //tick_one();
      
      var cycle_first_run = true;
      
      var allow_anim = true;
      
      $(".cycle", parent_elem).cycle({
          fx:     'fade', 
          speed:   one_speed, 
          timeout: one_sl,
          parent_elem: parent_elem,
          dur_sliding: dur_sliding,
          before: function () {
            if (cycle_first_run)
            {
               cycle_first_run = false;
               current_dots = max_dots;
               //run_dots();
            }
          },
          after: function (currSlideElement, nextSlideElement, options) {   
            var a=$(nextSlideElement);   
            $(".desc h2", parent_elem).html( a.attr("title") );
            $(".desc .desc_text", parent_elem).html( a.attr("alt") );
            
            var l = $(".desc .desc_text .go_more", parent_elem);
            if (l.length)
            {
               $(".desc .link", parent_elem).attr("href", l.attr("href"));
            }
            else
            {
               $(".desc .link", parent_elem).removeAttr("href");
            }
           
            
            if ( $(nextSlideElement).hasClass('hide_slide') )
               $(".desc_outer", parent_elem).hide();
            else
               $(".desc_outer", parent_elem).show();
            
            $(".desc_outer", parent_elem).animate({
               left: 0
            }, {
               duration: dur_sliding,
               queue: false,
               complete: function () {
                  dots.show();
                  $(".nav_s_inner", parent_elem).fadeIn(500, function () {
                     current_dots = max_dots;
                     start_dots();
                     allow_anim = true;
                  });
               }
            });
          }
      }).cycle("pause");
      
      $(".pxs_next, .pxs_prev", parent_elem).not(".carousel .pxs_next, .carousel .pxs_prev").click(function () {
         if (!allow_anim)
            return;
         allow_anim = false;
         if (one_sl_timeout)
            clearTimeout(one_sl_timeout);
         var e = ($(this).hasClass('pxs_next') ? "next" : "prev");
         $(".nav_s_inner", parent_elem).fadeOut(500, function () {
            $(".cycle", parent_elem).cycle(e);
         });
      });
   });   
});

var slideshow_group_counter = 2;

function crea_add_slideshows() {
   //$(dt_settings.highslide).prettyPhoto(pf_options);
   var cc = 0;
   $(dt_settings.highslide).each(function () {
      if (!$(this).attr("href"))
         return;
      cc++;
      
      var tid = 'single_thumb_'+cc;
      $(this).attr("id", tid);
      
      $(this).unbind('click');
      
      this.onclick = function () {
         //conf.slideshowGroup = 'boo';
         var config22 = {};
         $.extend(config22, hs_config2)
         config22.thumbnailId = tid;
         //config22.src = $(this).attr("href");
         //hs.expand(null, config22);
         hs.expand(this, hs_config2);
         return false;
      };
   });
   $(".pxs_slider li, .pxs_slider li .single, .pxs_slider li .gallery_inline").each(function () {
   
      elems = $(dt_settings.highslide_gall, this);
   
      if (elems.length == 0)
         return;

      if (elems.length == 1 && elems.not( $(".gallery_inline a", this) ).length)
         return;
   
      var now_options = slideshow_options;
      var group = 'group'+slideshow_group_counter;
      var tid = 'for_'+group;
      
      slideshow_group_counter++;
      
      elems.eq(0).attr("id", tid);
      
      var slideshow_options_bak = {};
      $.extend(slideshow_options_bak, slideshow_options);
      slideshow_options_bak.slideshowGroup = group;
      hs.addSlideshow(slideshow_options_bak);
      
      elems.each(function () {
         //if ( $(this).hasClass("hs_attached") ) return;
         $(this).addClass("hs_attached");
         if (!$(this).attr("href"))
            return;
         this.onclick = function () {
            var config22 = {};
            $.extend(config22, hs_config1)
            config22.slideshowGroup = 'group1';
            config22.slideshowGroup = group;
            //config22.thumbnailId = tid;
            return hs.expand(this, config22);
            //return hs.expand(this, hs_config1);
         };
      });
   });
};

$(function () {
   $(".pxs_slider > li").addClass("containers");
   $(window).resize(function () {
      //$(".pxs_slider > li > .holder").css('height', $(window).height()+"px");
	    $(".left-box").css({
	         'height'  : $(window).height()+"px",
	      });
   }).trigger("resize");
});

$(function () {
   if (!menu_is_clickable)
   {
      $("ul.pxs_thumbnails > li > a").each(function () {
         if ( $(this).parent().children("div").length > 0 )
         {
            $(this).css('cursor', 'default');
            $(this).addClass("not_clickable");
            $(this).click(function () {
               
               return false;
            });
         }
      });
   }
   
   /*
   $(".hash_go").click(function () {
      var h = $(this).attr("href");
      h = h.replace('/^.\#/', '');
      alert(h);
      if (h)
      {
         var e = $(".hash_"+h, $(this).parents(".post"));
         alert(e.length);
         if (e.length)
         {
            alert("ok");
            return false;
         }
      }
   });
   */
});



$(document).ready(function() {
	$(".toggle a.question").click(function (event) {
		event.preventDefault(); 
		$(this).toggleClass("act");
		$(this).next("div.answer").slideToggle("fast", function () {
		   upd_ev();
		});
	});
	
	//setTimeout(upd_ev, 1000);
});




