(function($) {
	
	$.fn.menu = function() {
		
		return $(this).each(function(index, value) {
			var currentElem = $(value);
			var currentSubmenu = currentElem.find('.dropdown');
				
			
			currentElem.hover(function(e){ 
				var t = $(this);
				var tNext = t.next().children('a:first-child');
			
				t.addClass('hover');
				tNext.css({'border':'none'});	
				
			
				
			},function(e){ 
				var t = $(this);
				var tNext = t.next().children('a:first-child');
				
				t.removeClass('hover');
				//tNext.removeAttr('style');
				
				
			});

			
		});

	}

})(jQuery);	



$(document).ready(function() {
	
				//*adujst Top Menu elements
				var menu = $('#menuTop'),
					  menuLi = menu.find('ul:first > li'),
					  menuLink = menu.find('a'),
					  nMenu = menuLi.size(),
					  tMenu = 0,
					  wMenu = 781,
					  liArr = [],
					  pads = [],
					  pos = [],
					  imageCount = 0;
					  
					  
				   menuLi.each(function(index) {
						   liArr[index] =  $(this).width();
						   tMenu += $(this).width();
				   });
				   
				    
				   
				  // if(tMenu + nMenu > wMenu){
					   menu.find('ul').attr('style', 'padding-top:0px;');
					   menuLi.width(parseInt(wMenu/nMenu-1));
					   menuLi.height(30);
					   
					   menuLink.each(function(index) {
						  if($(this).height() < 22 ){ 
							 $(this).attr('style','padding-top: 10px;')
						  }
					   });  
					   
					   menu.find('ul li.category:last').width(parseInt((wMenu/nMenu)+1));         
				   //}
				   
				   
				   menuLi.each(function(index) {
								   
								   var that = $(this);		
			   
								   $(this).find('.dropdown').find('ul:first').each(function(index) {
									  $(this).css('padding-left', that.offset().left - menuLi.first().offset().left);
									  pads.push(that.offset().left - menuLi.first().offset().left);
								   });
								   
								   if(index == nMenu-1)
								   {
									  var dropDownW = $(this).find('.dropdown').width();
									  
									  $(this).find('.dropdown').find('ul').css('float','right')
									  
									  $(this).find('.dropdown').find('ul:first').each(function(index) {
									 	  $(this).css('padding-left', '0');
								 	  });
									  
								   }
									
									//$.get($(this).find('.push').find('img'), function (data) {  
									//console.log($(this).find('.push').find('img').attr({src: $(this).find('.push').find('img').attr('src') + '?random=' + (new Date()).getTime()}));
									var src = $(this).find('.push').find('img').attr('src');
									$(this).find('.push').find('img').attr({src: src + '?random=' + (new Date()).getTime()}).load(function() {
										
										//console.log($(this).parent().parent().find('img').attr('width'));
										
										$('#menuTop ul li .dropdown').css('display', 'block');
										
										 if(pads[index] < $(this).attr('width'))
										  {
											  //console.log($(this).parent().parent());
												$(this).parent().parent().css('right', '15px')
										  } else {
												$(this).parent().parent().css('left', '15px')
										  }
										  
										 $('#menuTop ul li .dropdown').css('display', 'none'); 
										  
									}); 
									
				   });

	(function() {
		var o = 'opened',
			c = 'closed';
			
		$('.arrow').each(function(k,v) {	
			var arrow = $(v),
				ele = $(arrow.attr('href'));
				
			if (ele.length) {				
				var	h = ele.height();
				
				if (arrow.hasClass('closed')) {
					ele.css({height: '0px'});
				}
						
				arrow.bind('click.arrows',function(e) {
					var newh = 0;
					
					if (arrow.hasClass(o)) {
						arrow.removeClass(o).addClass(c);						
					}
					else {
						arrow.removeClass(c).addClass(o);
						newh = h;
					}
					
					ele.animate({height: newh + 'px'});
					
					return false;
				});				
			}
		});
	})();
	
	/*
	//adjust Boxes
	var headrBlock = $('#home-header'),
		blockLeft = headrBlock.find('.title'),
		blockRight = headrBlock.find('#PdvNews');
			
	//adjust height
	if(blockRight.height() != blockLeft.height()){
		blockRight.height(blockLeft.height());
	} else {
		blockLeft.height(blockRight.height());
	}
	
	//adjust position
	if ($('#home-header').find('.container-right-plan > p').length >0) {
		
		var blockRightPos = headrBlock.find('.container-right-plan > p').position().top;
		console.log(blockRightPos);
		
		if (blockRightPos <= 55) {
			$('.blockServices').css ('top', 0);
			$('.container-right-plan').css ('top', 15);
		} else {
			$('.blockServices').css ('top', (blockRightPos-70));
		};		
	};*/
	//end adjusts
	
})

/*
var url = "http://www.webpelican.com/internet-programming-3/"; 
var pageName = "Client side programming"; 
function bookmark() { 
	if (window.external) { 
		window.external.AddFavorite(url, pageName)   
	} 
	else { 
		alert("Sorry! Your browser doesn't support function. Use the bookmark option in your browser instead."); 
	} 
}*/

