var is_ie6= jQuery.browser.msie && jQuery.browser.version.major == 6;
var is_ie8= jQuery.browser.msie && jQuery.browser.version.major == 8;
var time = 250; // tabs crossfade time in milliseconds

// hover combo
(function(jQuery) {
	jQuery.fn.h = function(classname) {
		return this.hover(
				function(){
					jQuery(this).addClass(classname);
				},
				function(){
					jQuery(this).removeClass(classname);
				}
			);
	}
})(jQuery);

(function(jQuery) {
jQuery.fn.f = function(classname) {
 return this.focus(function(){jQuery(this).addClass(classname)}).blur(function(){jQuery(this).removeClass(classname)});
}
})(jQuery);



(function(jQuery) {
// clear on focus
jQuery.fn.clear_on_focus = function(word){
 return this.each(function(){
    jQuerythis = jQuery(this);
    var by_title = !jQuerythis.attr("title").blank();
    var title = word || jQuerythis.attr("title") || jQuerythis.attr("defaultValue");
    if(by_title){if(jQuery(this).attr("value").blank() || jQuerythis.attr("value") == null) jQuerythis.attr("value", title)}
   jQuerythis
      .focus(function(){
        if(jQuerythis.attr("value") == title) jQuerythis.attr("value", "");
      })
      .blur(function(){
        if(jQuerythis.attr("value").blank() || jQuerythis.attr("value") == null) jQuerythis.attr("value", title);
      });
  });
}
})(jQuery);

(function(jQuery) {
//Custom select box_title1
jQuery.fn.select_box = function(options){
  return this.each(function(){
    var o = {};
    o.stop_event = true;
    o.update_current = true;
    jQuery.extend(o, options);

    var me = jQuery(this);
    var current = me.find('.current-in');
    var last = null;


    var items  = me.find('.items').hide();

    var active = false;
    var selected = null;


    jQuery(document).click(function(e){
      if(active){
        deactivate();
      }
    });

    current.click(function(e){
      e.preventDefault();
      activate();
    });

     function activate(){
      if(!active){
        me.addClass("select-active");
        items.fadeIn("fast");
        setTimeout(function(){
          active = true;
        },250);
      }
    }

     function deactivate(){
      me.removeClass("select-active");
      items.fadeOut("fast");
      active = false;
    }

    function afterChange(selected){
       if (jQuery.isFunction(o.afterChange)) o.afterChange(selected);
    }

    me.find(o.options).click(function(e){
      if(o.stop_event) e.preventDefault();
      if(o.update_current) current.html(jQuery(this).html());
      selected = jQuery(this);
      if(last != this) afterChange(selected);
      last = this;
      deactivate();
    })
  })
}
})(jQuery);


// random number
Number.prototype.rnd = function(){return Math.round(Math.random()*this)}

// String blank
String.prototype.blank = function(){return /^\s*jQuery/.test(this || ' ');}


// Vip
var vip ={
  init: function(){


   this.login_box();
   this.set_font_size();
   this.sitemap();



   jQuery('.scroll-pane').jScrollPane({scrollbarWidth:11, scrollbarMargin:10,showArrows:true});
   jQuery('.tabs .tab').hide(); //IE6 fix
   jQuery('.tabs .tab.only-tab').show();

   this.set_teasers();

   if(is_ie8){
	     jQuery('#toplogin input').keypress(function(e) {
	       var key = e.which ? e.which : e.keyCode;
	       if(key == 13) {
	         var f = jQuery(this).blur().parents("form:first").submit();
	         e.preventDefault();
	       }
	     });
	   }


  //  this.set_container_height();

    //clear on focus
    jQuery("input.txt, .shoutform textarea").clear_on_focus();


   jQuery(".selectable-stars").stars({
     inputType: "select",
     cancelShow: false
   });


    if (is_ie6) {
     jQuery('#mainmenu li').h('hover');
     jQuery('#mainmenu .sub').h('sub_hover');
     jQuery.each(['orange', 'brown','light_brown', 'purple', 'pink', 'red', 'light_orange'], function(){
           jQuery('#mainmenu .'+this).h(this+'_hover');
         });
     jQuery('span.btn').h('btn_hover');
     jQuery('.img img, .title_icon, .table2 .status, .expand').ifixpng();
     jQuery('.stars, .stars-fill, .af-alpha, .fotodesc').ifixpng({method: "crop"});
    // DD_roundies.addRule('.login,#toplogin form input');
    }


    this.text_counters();

    this.set_selects();

    this.comments();

  },


  text_counters: function(){
    jQuery(".shoutform textarea").charCounter(1000,{
      format: "%2/%0 characters used"
    });
  },

  set_font_size:function(){
   jQueryview_full = jQuery('.vlc, .vlc2');
   var current_size = 100;
    jQuery('.letter_size a').click(function(e){
      e.preventDefault();
      jQuerythis = jQuery(this);

      switch(this.className){
      case "smaller":
        if(current_size <= 60) break;
        current_size -= 10;
        jQueryview_full.css({fontSize: current_size + "%" });
        break;
      case "bigger":
        if(current_size >= 140) break;
        current_size += 10;
        jQueryview_full.css({fontSize: current_size + "%"});
        break;
      default:
       current_size = 100;
       jQueryview_full.css({fontSize: "100%"});
      }
    })
  },

  login_box: function(){

    /* LOGIN BOX */

	  jQuery("#toplogin a.do_login").bind("click", function()
	  {
		  jQuery("#toplogin").addClass("open_login").animate({"height" : "143px"}, time);

		  //if(jQuery.browser.msie && 6.0 >= jQuery.browser["version"])
		  if(is_ie6) jQuery("#toplogin").css({"backgroundPosition" : "-306px bottom"});

		  jQuery("#toplogin .closed, #toplogin .temp").hide();
		  jQuery("#toplogin .open").fadeIn();

		  return false;
	  });

	  jQuery("#toplogin a.do_login_temp").bind("click", function()
	  {
		  jQuery("#toplogin").addClass("open_temporary").removeClass("open_login").animate({"height" : "115px"}, time);

		  if(jQuery.browser.msie && 6.0 >= jQuery.browser["version"]) {
			  jQuery("#toplogin").css({"backgroundPosition" : "-306px bottom"});
		  }
		  jQuery("#toplogin .closed, #toplogin .open").hide();
		  jQuery("#toplogin .temp").fadeIn();

		  return false;
	  });

	  jQuery("#toplogin a.close").bind("click", function()
	  {
		  jQuery("#toplogin").animate({"height" : "43px"}, time);

		  if(jQuery.browser.msie && 6.0 >= jQuery.browser["version"]) {
			  jQuery("#toplogin").css({"backgroundPosition" : "left top"});
		  }
		  jQuery("#toplogin .open, #toplogin .temp").hide();
		  jQuery("#toplogin .closed").fadeIn();

		  setTimeout('jQuery("#toplogin").removeClass("open_temporary").removeClass("open_login")', time - 50);

		  return false;
	  });

  },

  set_container_height: function(){
   var main_bg = jQuery('#main_bg');
   var main_bg_height = main_bg.height();
   var document_height = window.innerHeight ? window.innerHeight : jQuery(window).height();
   if(document_height > main_bg_height) main_bg.height(document_height);
  },

  sitemap:function(){
    jQuery('.sitemap .lvl2').hide();
    jQuery('.sitemap li:not(.open) .title').click(function(e){
      e.preventDefault();
      jQuery(this).parent().find('> ul').toggle("fast");
      jQuery(this).parent().toggleClass('open');
    })

    function open(){
      jQuery('.sitemap .lvl2').show().parent('li').addClass('open');
    }

    function close(){
      jQuery('.sitemap .lvl2').hide().parent('li').removeClass('open');
    }

    jQuery('.sitemap-trigger').click(function(e){
      e.preventDefault();
      jQuerythis = jQuery(this);
      jQuerythis.hasClass('sitemap-open') ? close() : open();
      jQuerythis.toggleClass('sitemap-open');
    });



  },

  comments: function(){
    jQuery('.close_comments').click(function(e){
      e.preventDefault();
      jQuerythis = jQuery(this);
      jQuerythis.toggleClass("comments-open").parents(".post").find(".comments").toggle();
      var rel = jQuery(this).attr("rel");
      jQuerythis.attr("rel", jQuerythis.html());
      jQuerythis.html(rel);
      jQuerythis.parents('.scroll-pane:first').jScrollPane({scrollbarWidth:11, scrollbarMargin:10,showArrows:true});
    });
  },

set_selects: function(){
    jQuery('.usercorner-select').select_box({
      options: "a",
      afterChange: function(selected){
        jQuery('.user_corner-current').html(selected.html());
      }
    });

    jQuery('.select_box').select_box({
      options: "a",
      stop_event: false,
      update_current: false
    });

    jQuery('.select_box2').select_box({
      options: "a"
    });



     jQuery('.cc').select_box({
      options: "a",
      afterChange: function(selected){
       var cname  = jQuery("body").get(0).className.match(/pc_\w+/)[0] || '';
       jQuery("body").removeClass(cname).addClass(selected.attr("rel"));
      }
    });
},

  set_teasers: function(){


     // Teaser type 2
     jQuery('.js_teaser2').each(function(i,e){
       jQuery(e).find('.pages:first a').each(function(index,element){
         jQuery(element).addClass('handle_'+index);
       });
       jQuery(e).find('.slides:first ').cycle({
        fx:'scrollHorz',
        speed: 800,
        timeout: 0,
        cleartypeNoBg: true,
        pager:  jQuery(e).find('.pages:first'),
        prev: jQuery(e).find('.prev2:first'),
        next: jQuery(e).find('.next2:first'),
        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
           return jQuery(e).find('.pages:first a.handle_' + idx);
        }
      });
     });

     // Teaser type 2 - Not animated
     jQuery('.js_teaser2-na').each(function(i,e){
     //fix sc 23.12.2009. u ie6 se single tab u nested portletu ne prikazuje
    	 var b = true;
    	 if (is_ie6) {
    		 if (jQuery(e).find('.pages:first a').size() == 1) {
    			 b = false;
    		 }
    	 }
       jQuery(e).find('.pages:first a').each(function(index,element){
    	   if (b)
    		   jQuery(element).addClass('handle_'+index);
       })
       jQuery(e).find('.slides:first ').cycle({
        fx:'scrollHorz',
        speed: 0,
        timeout: 0,
        cleartypeNoBg: true,
        pager:  jQuery(e).find('.pages:first'),
        prev: jQuery(e).find('.prev2:first'),
        next: jQuery(e).find('.next2:first'),
        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
           return jQuery(e).find('.pages:first a.handle_' + idx);
        }
      });
     });


     // Teaser type 2 - Not animated, fluid
     jQuery('.js_teaser2-na-f').each(function(i,e){
       jQuery(e).find('.pages:first a').each(function(index,element){
         jQuery(element).addClass('handle_'+index);
       });
       jQuery(e).find('.slides:first ').cycle({
//        fx:'scrollHorz',
        speed: 0,
        timeout: 0,
        cleartypeNoBg: true,
        containerResize:0,
        slide_position: "static",
        pager:  jQuery(e).find('.pages:first'),
        prev: jQuery(e).find('.prev2:first'),
        next: jQuery(e).find('.next2:first'),
        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
           return jQuery(e).find('.pages:first a.handle_' + idx);
        }
      });
     });



     // Teaser type 3
     jQuery('.js_teaser3').each(function(i,e){
       jQuery(e).find('.pages:first a').each(function(index,element){
         jQuery(element).addClass('handle_'+index);
         if (index == 0) {
        	 jQuery(element).addClass('activeSlide');
         }
       });
       jQuery(e).find('.slides:first ').cycle({
        fx:'scrollVert',
        speed: "normal",
        timeout: 0,
        cleartypeNoBg: true,
        pager:  jQuery(e).find('.pages:first'),
        prev: jQuery(e).find('.prev2:first'),
        next: jQuery(e).find('.next2:first'),
        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
           return jQuery(e).find('.pages:first a.handle_' + idx);
        }
      });
     });


     // TABS
     jQuery('.js_tabs1').each(function(i,e){
    	 
       jQuery(e).find('ul.horizontal:first a').each(function(index,element){
         jQuery(element).addClass('handle_'+index);
       });
       
       jQuery(e).find('.tabs:first').cycle({
        fx:'scrollLeft',
        speed: 0,
        timeout: 0,
        cleartypeNoBg: true,
        pager:  jQuery(e).find('.horizontal:first'),
        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
           return jQuery(e).find('.horizontal:first a.handle_' + idx);
        }
      });
     });

     // TABS
     jQuery('.js_tabs1_1').each(function(i,e){
       jQuery(e).find('ul.horizontal:first a').each(function(index,element){
         jQuery(element).addClass('handle_'+index);
       });
       
       jQuery(e).find('.tabs2:first').cycle({
        fx:'scrollLeft',
        speed: 0,
        timeout: 0,
        cleartypeNoBg: true,
        pager:  jQuery(e).find('.horizontal:first'),
        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
           return jQuery(e).find('.horizontal:first a.handle_' + idx);
        }
      });
     });


      jQuery('.photogallery_pager a').each(function(index,element){
         jQuery(element).addClass('handle_'+index);
       });

       jQuery('.photogallery_image .slide:not(.loaded) .bi').append('<div class="loader">uÄitavam sliku ... </div>');

 var photo_info = {
   title: jQuery('.photo-details .title:first'),
   subtitle: jQuery('.photo-details .subtitle:first'),
   add_link: jQuery('.photo-info .flexi-btn:first'),
   your_grade: jQuery('.photo-details .your_grade_num:first'),
   our_grade: jQuery('.photo-details .our_grade_num:first'),
   img_num : jQuery('.photo-info .img_num strong:first')
 }


 function set_stars(el, num){
  el.find('span').html(num);
  domel = el.get(0);
  if(domel){
    domel.className = domel.className.replace(/stars\d/, 'stars'+num );
  }
 }

 jQuery('.js_gallery1 .slides').after('<div class="pager"><a href="#" class="prev">&lt;</a><a href="#" class="next">&gt;</a></div>').cycle({
        fx:'scrollHorz',
//        speed: 0,
        timeout: 0,
        cleartypeNoBg: true,
        pager: jQuery('.photogallery_pager .slides2'),
        prev: jQuery('.js_gallery1 .prev:first'),
        next: jQuery('.js_gallery1 .next:first'),

        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
           return jQuery('.photogallery_pager a.handle_' + idx);
        },
        pagerClick: function(zeroBasedSlideIndex, slideElement){
          photo_info.img_num.html(++zeroBasedSlideIndex);
        },
        prevNextClick:function(isNext, zeroBasedSlideIndex, slideElement){
          photo_info.img_num.html(++zeroBasedSlideIndex);
        },

        after:function(currSlideElement, nextSlideElement, options, forwardFlag){
           var jQuerynext = jQuery(nextSlideElement);

           var info = jQuerynext.find('input');
           info = info && info.val();

           if(info){
            info = eval('(' + info + ')');
            photo_info.title && photo_info.title.html(info.title);
            photo_info.subtitle && photo_info.subtitle.html(info.subtitle);
            photo_info.add_link && photo_info.add_link.attr('href', info.add_link);

            if(photo_info.your_grade){
              set_stars(photo_info.your_grade, info.your_grade);
              set_stars(photo_info.our_grade, info.our_grade);
            }
           }


         if(!jQuerynext.hasClass("loaded")){

           var loader = jQuerynext.find(".loader:first");
           var bg_img = jQuerynext.find('.bg_img');
           var img = new Image();

           jQuery(img).load(function(){
//             console.log("loaded")
             loader.fadeOut();
             //jQuery(this).ifixpng();
             bg_img.css('background', 'url(' + img.src + ') no-repeat 0 0');
           })
//            console.log(info.img_src)
           img.src = info.img_src;
           jQuerynext.addClass('loaded');
          }



        }
      });



  // Teaser type 1
    jQuery('.js_teaser1').each(function(i,e){
       jQuery(e).find('.slides2:first').after('<div class="pager" style="display:none;"><a href="#" class="prev">&lt;</a><a href="#" class="next">&gt;</a></div>').cycle({
          fx: 'scrollHorz',
//          speed: 1200,
          timeout: 8000,
          cleartypeNoBg: true,
          prev: jQuery(e).find('.prev:first'),
          next: jQuery(e).find('.next:first'),
          pagerEvent: 'click'
        });
      if(jQuery(e).find('.slides2 .slide2, .slides2 .slide').length > 1) jQuery(e).find(".pager:first").show();
    });



     // TABS 2
     jQuery('.js_tabs2').each(function(i,e){
       jQuery(e).find('.tabs:first').after('<div class="pager"><div class="pages pages2"></div></div>').cycle({
        fx:'scrollHorz',
//        speed: 800,
        timeout: 8000,
        cleartypeNoBg: is_ie6,
        pager:  jQuery(e).find('.pages:first'),
        pagerEvent: 'click'
      });
     });


 }
}



jQuery(document).ready(function() {

 // add js to body for css
 jQuery("body").addClass('js');

 //vip
 vip.init();



})

jQuery(document).ready(function() {
	jQuery("a.share")
		.bind("mouseover",  function(){ return addthis_open(this, '', '[URL]', '[TITLE]'); })
		.bind("onmouseout", function(){ addthis_close(); })
		.bind("onclick",    function(){ return addthis_sendto(); });
});


