 $(document).ready(function() {

    	/* ~~~~~search box~~~~~ */
    	var searchBoxVisible = 0;
    	var searchBoxVisibleLogin = 0;    	
        var myprofileBoxVisible = 0;
    	var mouseOverBox = false;
    	var mouseOverBoxLogin = false;
        var mouseOverBoxMyprofile = false;
    	

    	$('div#search_link a').click(function(){
    		if(searchBoxVisible == 0) {
    			$('div#search_box_wrapper').fadeIn(null, function(){
    				myprofileBoxVisible = 0;
    				searchBoxVisible = 1;
    				searchBoxVisibleLogin = 0;
    			});
    		}
    	});
        
        $('div#myprofile_link a').click(function(){
            if(myprofileBoxVisible == 0) {
    			$('div#myprofile_box_wrapper').fadeIn(null, function(){
    				myprofileBoxVisible = 1;
    				searchBoxVisibleLogin = 0;
                    searchBoxVisible = 0;
    			});
    		}
        });

		$(' a#login_link').click(function(){
        	
    		$('div#box_wrapper_login').fadeIn(null, function(){
    			myprofileBoxVisible = 0;
    			searchBoxVisibleLogin = 1;
    			searchBoxVisible = 0;
			});
    		
    	});

    	
        $('div#search_box_wrapper').hover(function(){
            mouseOverBox=true;
        }, function(){
            mouseOverBox=false;
        });

        $('div#box_wrapper_login').hover(function(){
        	mouseOverBoxLogin=true;
        }, function(){
        	mouseOverBoxLogin=false;
        });
        
        $('div#myprofile_box_wrapper').hover(function(){
        	mouseOverBoxMyprofile=true;
        }, function(){
        	mouseOverBoxMyprofile=false;
        });

         

    	$('form#search_form').submit(function(){
    		if($('input#search_field').val().length <= 3) {
    			alert('<?php echo TXT_SEARCH_PHRASE_TOO_SHORT; ?>');
    			return false;
    		}
    	});

    	

    	$('body').click(function(){
            if(!mouseOverBox && searchBoxVisible == 1 ) {
    			$('div#search_box_wrapper').fadeOut();    			
    			searchBoxVisible = 0;    			
    		}
            if ((!mouseOverBoxLogin && searchBoxVisibleLogin == 1)) {
            	$('div#box_wrapper_login').fadeOut();
    			searchBoxVisibleLogin = 0;            	
            }
            if ((!mouseOverBoxMyprofile && myprofileBoxVisible == 1)) {
            	$('div#myprofile_box_wrapper').fadeOut();
    			myprofileBoxVisible = 0;            	
            }
        });
    });
