/* Hint */
(function($) {

	var html = '<div class="hint-popup hidden"> \
        <div class="a"> \
		{content} \
        </div> \
      </div> ';

	$(".hint").each(function() {
		popup = html.replace("{content}", this.title);
		this.title = "";
		$(this).after(popup);

		$(this).mouseenter(function() {
			$(this).next().css("left", this.offsetLeft+22+"px").css("top", this.offsetTop+0+"px").show();
		}).mouseleave(function() {
			$(this).next().hide();
		});
	});

})(jQuery);

// show/hide search text
(function($) {
  var original = false;

  $("#searchinput").focus(function() {
    if (!original) {
      original = this.value;
    }
    if (this.value == original) {
      this.value = "";
    }
  });
  $("#searchinput").blur(function() {
    if (this.value == "") {
      this.value = original;
    }
  });

})(jQuery);

function sendPasswordModal() {
    $.fn.colorbox({
      inline:true,
      href:".cboxSendPassword",
      //href:".cboxLogin",
      overlayClose:false,
      close:"",
      opacity:0.3,
      transition:'elastic'
    });
}

// show hide form help
(function($) {
  $('form input').focus(function() {
      if ( jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 8 ) {
        var marginTop = 0;
        var left = 0;
      } else {
        var marginTop = -26;
        var left = this.offsetLeft + $(this).width() + 18;
      }
      $(".note, .noteError", $(this).parent().parent() )
        .parent()
        .css("margin-top", marginTop+"px")
        .css("display", "block");
      $(".note, .noteError", $(this).parent().parent() )
        .css("left", left+"px")
        .slideDown('normal');
      return false;
    });
  $('form input').blur(function() {
    $(".note, .noteError", $(this).parent().parent() ).parent().slideUp('normal');
    $(".note, .noteError", $(this).parent().parent() ).slideUp('normal');
    return false;
  });
})(jQuery);

// show hide form help
(function($) {
  $('.companyList .deleteCompany').click(function() {
    $(this).parent().fadeOut();
  });
})(jQuery);


$("DIV.nav li.adminmenu")
  .mouseenter( function() { $(".popupmenu", this).show(); } )
  .mouseleave( function() { $(".popupmenu", this).hide() } );


$("a[rel='images01']").colorbox({
   current: 	"{current} / {total}",
   previous: "eelmine",
   next:"jĆ¤rgmine",
   close:"",
   opacity:0.3,
   transition:'elastic'
});


(function($) {

  var el = document.getElementById("adminProductList");
  if (el!= null) {
    $(".product", el)
      .mouseenter( function() { $(".manage", this).removeClass("hidden"); } )
      .mouseleave( function() { $(".manage", this).addClass("hidden") } );

    $(".delete", el).click(function() {
      var product = $(this).parent().parent().parent();
      //var height = product.height();
      //product.css("height", height+"px").html("").attr("class", "productDeleted");
      product.css("opacity", 0).css("cursor", "default");
      $(".manage", product).remove();
    });
  }



})(jQuery);

$(document).ready(function() {
	var el = $(".productList .product");
	if (el.length) {
		$(".productList .product").biggerlink({hoverclass:'productHover'});
	}
});

$(document).ready(function() {
  if($('#loginfailure').attr('class') != '')
    login();
});

function google_maps() {
  $.fn.colorbox({
      inline:true,
      href:".cboxGMap",
      overlayClose:true,
      close:"",
      opacity:0.3,
      transition:'elastic'
    });
}

// 1. even heights for frontpage .box01 boxes
// 2. mainmenu hover intro
(function($) {

	var max_height = 0;
	if ( $(".mainContentI").length>0 ) {
		even_heights();
		//frontpage_menu_hover();
	}

	function even_heights() {
		var boxes = $(".mainContentI div.box01");
		var cols =  $(".mainContentI div.col01");
		if ( boxes.length>0 ) {
			boxes.each(function() {
				if (max_height<$(this).height()) {
					max_height = $(this).height();
				}
			});
			boxes.each(function() {
				$(this).css("height", max_height+"px");
			});
		}
	}
/*
	function frontpage_menu_hover() {
    var menu = $(".mainmenu");
    var menuitems = $(".mainmenu a");
    var active = false;

		menuitems.mouseenter(function(){
      if(this.id == 'main_page')
        return;

      var nr = this.id.split("_")[1];

      $('[id^=menu_dropdown_]').fadeOut(0);

      if(active === false)
        $("#menu_dropdown_" + nr).fadeIn(400);
      else
        $("#menu_dropdown_" + nr).fadeIn(0);

      active = true;
		});

    $('#main_page').mouseenter(function(){
			var nr = this.id.split("_")[1]
      $('[id^=menu_dropdown_]').fadeOut(400);
      active = false;
		});

    menu.mouseleave(function(){
			var nr = this.id.split("_")[1]
      $('[id^=menu_dropdown_]').fadeOut(400);
      active = false;
		});
	}*/

})(jQuery);

$("a.top5_tab").click(function() {
    $('a.top5_tab').parent().removeClass('selected');
    $(this).parent().addClass('selected');
    $('div#top5_products').hide();
    $('div#top5_news').hide();
    $('div#top5_companies').hide();
    $('div#top5_' + $(this).attr('id')).show();

    return false;
});

$(".submit-form").click(function() {
    $('form[id=register]').submit();
    return false;
});


function createAccountModal() {
    $.fn.colorbox({
      inline:true,
      href:".cboxChooseRegistrationType",
      overlayClose:false,
      close:"",
      opacity:0.3,
      transition:'elastic'
    /*},
    function(){
      var options = {
        script: "json.php?",
        varname: "find",
        json: true,
        maxresults: 35
      };
      var as = new bsn.AutoSuggest('tag', options);*/
  });
}

function createAccount() {
  $.fn.colorbox({
      inline:true,
      href:".cboxRegistrationSuccess",
      overlayClose:false,
      close:"",
      opacity:0.3,
      transition:'elastic'
    /*},
    function(){
      var options = {
        script: "json.php?",
        varname: "find",
        json: true,
        maxresults: 35
      };
      var as = new bsn.AutoSuggest('tag', options);*/
  });
}

function findCompany() {
  $.fn.colorbox({
      inline:true,
      href:".cboxChooseCompany",
      overlayClose:false,
      close:"",
      opacity:0.3,
      transition:'elastic'
    /*},
    function(){
      var options = {
        script: "json.php?",
        varname: "find",
        json: true,
        maxresults: 35
      };
      var as = new bsn.AutoSuggest('tag', options);*/
  });
}

function login() {
    $.fn.colorbox({
      inline:true,
      href:".cboxLogin",
      overlayClose:false,
      close:"",
      opacity:0.3,
      transition:'elastic'
    });
}

function google_maps() {
    $.fn.colorbox({
        inline:true,
        href:".cboxGMap",
        overlayClose:true,
        close:"",
        opacity:0.3,
        transition:'elastic'
      });
}

function cboxModuleInfo1()
{
    $.fn.colorbox({
      inline:true,
      href:".cboxModuleInfo1",
      overlayClose:true,
      close:"",
      opacity:0.3,
      transition:'elastic'
    });
}

function cboxModuleInfo2()
{
    $.fn.colorbox({
      inline:true,
      href:".cboxModuleInfo2",
      overlayClose:true,
      close:"",
      opacity:0.3,
      transition:'elastic'
    });
}

function cboxModuleInfo3()
{
    $.fn.colorbox({
      inline:true,
      href:".cboxModuleInfo3",
      overlayClose:true,
      close:"",
      opacity:0.3,
      transition:'elastic'
    });
}

function cboxModuleOrder01()
{
  $.fn.colorbox({
    inline:true,
    href:".cboxOrderModule01",
    overlayClose:true,
    close:"",
    opacity:0.3,
    transition:'elastic'
  });
}

function cboxOrderAccount()
{
  $.fn.colorbox({
    inline:true,
    href:".cboxOrderAccount",
    overlayClose:true,
    close:"",
    opacity:0.3,
    transition:'elastic'
  });
}

function cboxYoutube()
{
  $.fn.colorbox({
    inline:true,
    href:".cboxYoutube",
    overlayClose:true,
    close:"",
    opacity:0.3,
    transition:'elastic'
  });
}


function cboxSuccess()
{
  $.fn.colorbox({
    inline:true,
    href:".cboxSuccess",
    overlayClose:true,
    close:"",
    opacity:0.3,
    transition:'elastic'
  });
}
/*
if (document.getElementById("upload_button_id")!= null)
{
  new AjaxUpload('upload_button_id', {action: 'upload.php'});
}

if (document.getElementById("upload_button_id2")!= null)
{
  new AjaxUpload('upload_button_id2', {action: 'upload.php'});
}

if (document.getElementById("upload_button_id3")!= null)
{
  new AjaxUpload('upload_button_id3', {action: 'upload.php'});
}
*/

(function($) {

  $(".newslist2 .delete").click(function() {
	var deletePrompt = confirm("Oled kindel, et soovid uudist kustutada?");
	if (deletePrompt){
		$(this).parent().parent().fadeOut();
	}
  });



})(jQuery);
// Get element coordinates
function getOffsets(el) {
    var o = {x : el.offsetLeft, y : el.offsetTop};
    if (el.offsetParent != null) {
        var po = getOffsets(el.offsetParent);
        o.x += po.x;
        o.y += po.y;
    }
    return o;
}

// sidenav Popup
(function($) {
  var popupHTML = ''+
    '<div id="sideNavPopup">'+
    '  <div class="left"><div class="ml"><!-- --></div><div class="bl"><!-- --></div></div>'+
    '  <div class="mid">'+
    '  </div>'+
    '  <div class="right"><div class="tr"><!-- --></div><div class="mr"><!-- --></div><div class="br"><!-- --></div></div>'+
    '</div>';
  var popup = jQuery(popupHTML);
  var hoveringOnNav = false;
  var hoveringOnNavPopup = false;
  var timer;
  var closeTimeout = 0;
  var currentActiveNav;

  jQuery(".sideNav .hasSubmenu").bind({
    mouseenter: function() {
      var el = this;

      currentActiveNav = $(el);
      hoveringOnNav = true;
      hoveringOnNavPopup = false;

      popup.remove();
      $(".sideNav .hasSubmenu").removeClass("submenuActive");
      currentActiveNav.addClass("submenuActive");

      var offsets = getOffsets( el );
      $(".mid", popup).html("");
      $("a", $(el).children('div')).each(function(i) {
        a = $(this).clone();
        if (i==0) {
          $(a).addClass("first");
        }
        $(".mid", popup).append(a);
      });
      popup.css({"left": offsets.x+$(el).width()+"px", "top": offsets.y+"px"});

      popup.bind({
        mouseenter: function() {
          hoveringOnNavPopup = true;
        },
        mouseleave: function() {
          hoveringOnNavPopup = false;
          timer = setTimeout(__close, closeTimeout);

          function __close() {
            if (!hoveringOnNav && !hoveringOnNavPopup) {
              popup.remove()
              currentActiveNav.removeClass("submenuActive");
            }
          }
        }
      });
      jQuery("body").append(popup);
    },
    mouseleave: function() {
      hoveringOnNav = false;
      timer = setTimeout(__close, closeTimeout);

      function __close() {
        if (!hoveringOnNav && !hoveringOnNavPopup) {
          popup.remove();
          currentActiveNav.removeClass("submenuActive");
        }
      }
    }
  });
})(jQuery);

