
jQuery(document).ready(function($) 
{

    /* start Tooltip */
     
    $(function() {
    $('#tooltiper a').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        fade: 200
    });
    });
    /* end Tooltip */
	 
    $('.centerColumn ul').attr('type','square');
    
    $('.searchRecipeBut').click(function(){
        $('.searchForm').remove();    
        $('body').append('<div style="position: absolute; top: 20%; z-index: 999999; left: 42%; color:#fff; font-weight:bold; text-align:center "><img width="150px" src="http://img.oede.by/new/img/loading.gif" alt="Идёт поиск рецепта"><br>Пожалуйста, подождите...</div>');    
    })
    
    $('#search').focus(function(){
        $(this).val('');
    
    })    

    $('#receiptName').click(function(){
        $(this).val("");  
    }) 

    $('.searchIcon').click(function(){
        $('#cse-search-box1').submit();
    
    })
    
    
    
    /* print version */
    initPrintVersion();
   
    /* favorite */
    initFavorites();
	 			
	// setup the drop downs
	$('#navbar ul > li').hoverIntent(
		// mouseover so show the drop down
		function()
		{
			// make the nav item have the over state
			$('> .sub-menu', this).toggle();
			$('.open', this).css('background','url(http://img.oede.by/new/img/menuUp.jpg) no-repeat');
			if($(this).hasClass('shops')){
                $(this).css('background','url(http://img.oede.by/new/img/menuBackgroundShops.png) no-repeat 0px 3px');
			} else if($(this).hasClass('publications')){
                $(this).css('background','url(http://img.oede.by/new/img/menuBacjgroundPublications.png) no-repeat 0px 3px');
			} else if(!$(this).hasClass('forumMenu') && !$(this).hasClass('about')){
                $(this).css('background','url(http://img.oede.by/new/img/menuBackgroundRecipes.png) no-repeat 0px 3px');
            }
            $('.menuTitle',this).css('border','none');
			
		},
		// mouseout so hide the drop down
		function()
		{
            //remove the over state on the nav item
			$('> .sub-menu', this).toggle();
			$('.open', this).css('background','url(http://img.oede.by/new/img/menuDown.jpg) no-repeat');
			$(this).css('background','none');
			$('.menuTitle',this).css('border-bottom','1px solid #A6A6A6')
		}
	);
	
	
	// IE fun
	if(isIE())
	{
		$('#navbar li li').hover(
			function()
			{
				$(this).addClass('hover_ie');
			},
			function()
			{
				$(this).removeClass('hover_ie');
			}
		);	
	}
	
	
	zIndexWorkaround();

    /* Newsletter */
    
    $("img#subscribe").click(function(){
     
     var name = $('input#name').val();
     
     var email = $('input#email').val();
     
     //Валидация формы
     
        if((name == "")&(email == "")) $('.subscribe_message').html('Пожалуйста, введите имя и email.').css('color','red').show();
        
        else if(name == "") $('.subscribe_message').html('Пожалуйста, введите имя.').css('color','red').show();
       
        else if(email == "") $('.subscribe_message').html('Пожалуйста, введите email.').css('color','red').show();
        
        else if(!(/^([a-zA-Z0-9_  .\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email))) $('.subscribe_message').html('Неверный email!').css('color','red').show();
        
        else if((name != "")&(email != "")&(/^([a-zA-Z0-9_  .\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email))) {
        
            $('.subscribe_message').html('Подождите..').css('color','#8CA019').show(); 
            
            $.ajax({
                    type:'GET',
                    url:'http://oede.by/sendmail/sendmailAjax.php',
                    data: "name=" + name + "&email=" + email,
                    complete: function(data){ if (data.getResponseHeader('Content-Length') != 31) $('.subscribe_message').css('color','#8CA019').html('Спасибо, за подписку.<br>Вам выслано письмо.').show();
                                               else $('.subscribe_message').html('Вы уже подписаны.').css('color','red').show();;
                                              }
                  });  
              
        $('input#name').attr("value","");  
        
        $('input#email').attr("value","");                           

    return false;

        }
    });	

function isIE()
	{
    	if(navigator.userAgent.match(/MSIE \d\.\d+/))
        return true;
    	return false;
	}

	function zIndexWorkaround()
	{
	    // If the browser is IE,
	    if(isIE())
	    {
	        /*
	        ** For each div with class menu (i.e.,
	        ** the thing we want to be on top),
	        */
	        $(".sub-menu").parents().each(function() {
	            var p = $(this);
	            var pos = p.css("position");
	
	            // If it's positioned,
	            if(pos == "relative" ||
	               pos == "absolute" ||
	               pos == "fixed")
	            {
	                /*
	                ** Add the "on-top" class name when the
	                ** mouse is hovering over it, and remove
	                ** it when the mouse leaves.
	                */
	                p.hover(function() {
	                        $(this).addClass("on-top");
	                    },
	                    function() {
	                        $(this).removeClass("on-top");
	                    });
	            }
	        });
	    }
	}

    /* print version*/
    function initPrintVersion() 
    {
        jQuery('.print-version a.showPrint').click(function () {

            window.open('/print.html','_blank','scrollbars=yes,width=800,height=600,left=0,top=0, toolbar=yes, titlebar = yes, status = yes, resizeable = no, menubar = yes,location = yes, directories = yes, fullscreen = yes');

            return false;
        });
    }
    
    function getPrintContent() 
    {
            var szResult = '';
            szResult += '<h1>' + jQuery('.contentNamePublication').html() + '</h1>';
            szResult += '<div>';
            szResult += '<div style="float:left;margin:5px"><img src="' + jQuery('.contentImg img').attr('src') +'"/></div>';
            szResult += '<div> ' + jQuery('.textContent').html() + ' </div>';
            szResult += '</div>';
            return szResult;
    }
 
    /* favorites */
    function initFavorites() 
    {

        jQuery('.favorites').click(function() {
            var szName = jQuery('.contentNamePublication').html();
            var szUrl = window.location;
            jQuery.post('/stuff/favorites_rpc.php','name=' + szName + '&url=' + szUrl,function (result) {
                if (result == 'Ok') {
                    alert('Ссылка добавлена в избранное')
                }
                else if (result == 'Already') {
                    alert('Ссылка добавлена в избранное')
                }
                else 
                {
                    alert('Эта функция доступна только для зарегистрированных пользователей');
                }
            })
            return false;
        });
        jQuery('.deleteFavorites').click(function () {
            var nResult = confirm('Вы уверены, что хотите удалить эту ссылку?');
            if (!nResult) {
                return false;
            }
            var nId = this.getAttribute('rel');
            jQuery.post('/stuff/favorites_rpc.php','id=' + nId,function (result) {
                var nResult = window.parseInt(result);
                if (nResult > 0) {
                    jQuery('a.deleteFavorites[rel=' + nResult +']').each(function () {
                        jQuery(this.parentNode).remove();
                    });
                }
                else {
                    alert('Удаление ссылки не удалось, обновите страницу')
                }

            });
            return false;
        })
    }    
    

	
})

	
