$(document).ready(function(){
	
	$('div').ifixpng();
	$('td#news-panel img').ifixpng();
	
    var zeppelinBtn = $( "div#zeppelin" );
    var logoBtn = $( "div#NHK-logo" );

    zeppelinBtn.css( 'cursor', 'pointer' );
    zeppelinBtn.click(
	    function(){
		    window.location.href = "/";
	    }
	);
	
	logoBtn.css( 'cursor', 'pointer' );
	logoBtn.click(
	    function(){
		    window.location.href = "/";
	    }
	);
	
	var mainMenu = $( "div#main-menu > span:has( div )" );	
	for( var i=0; i<mainMenu.length; i++ ){
		new downMenu( $( mainMenu[i] ) );
	}
	
	new findForm( $("form#search-form") );
	
	if(Math.round(Math.random()) > 0) {
		new balloon( $( "div#antizasor" ) ).StartAnimation();
		new balloon( $( "div#chistulya" ) ).StartAnimation();
		new balloon( $( "div#domingo" ) ).StartAnimation();
	} else {
		new balloon( $( "div#antipyatin" ) ).StartAnimation();
		new balloon( $( "div#ikeep" ) ).StartAnimation();
		new balloon( $( "div#thrifty-housewife" ) ).StartAnimation();
	}
	
    new curbstoneBtn( $( "div#domingo-btn" ), "/brand.sdf/domingo" );
    new curbstoneBtn( $( "div#antipyatin-btn" ), "/brand.sdf/antipyatin" );
    new curbstoneBtn( $( "div#chistulya-btn" ), "/brand.sdf/chistulya" );
    
    if( $( "p#message-send" ).size() != 0 )
        setTimeout ( "clearSendMailForm()", 5000 );
        
    if( $( "div#hide-catalog-description > *" ).size() != 0  ){  
        $( "div.page-title" ).hover( 
	        function(){
	            $( "div#hide-catalog-description" ).show();
	        },
	        function(){
	            $( "div#hide-catalog-description" ).hide();
	        }
	    );
    } 
    
    $("a#map_trigger").click(function () {
        $("p#map").slideToggle();
    });
});

/* Clear sendmail form */
function clearSendMailForm( form, message ){
    $( "form#sendmail-form" )[0].reset();
    $( "p#message-send" ).remove();
};

/* class Balloon */
var balloon = function( element ){

	function random( size ) {
		return Math.round( ( Math.random()*( size - 1 ) ) + 1 );
	};
	
	function getX() {
		var leftVal = element.css("left");
		return strPropertyToInt( leftVal );		
	};
	
	function getY () {
		var topVal = element.css("top");
		return strPropertyToInt( topVal );
	};
	
	function strPropertyToInt( string ) {
		var end = string.indexOf( "px" );
		var number = string.substring( 0, end );
		return parseInt( number );
	};
	
	element.show();
	
	element.bind("click",
	    function( eventObject ){
	        var url = '#';
	        switch ( this.id ) {
				case "chistulya":
					url = '/brand.sdf/chistulya';
					break;    
				case "antipyatin":
					url = '/brand.sdf/antipyatin';
					break;
				case 'domingo':
					url = '/brand.sdf/domingo';
					break
				case 'antizasor':
					url = '/brand.sdf/antizasor';
					break;
				case 'ikeep':
					url = '/brand.sdf/ikeep';
					break;
				case 'thrifty-housewife ':
					url = '/brand.sdf/thriftyhousewife ';
					break;					
			};
			window.location.href = url;
	    }
	);
	
	element.css({"cursor":"pointer"});
	
	return {
	    Element: element,
		X: getX(),
		Y: getY(),
		
		StartAnimation: function() {		
		    var curX = getX();
			var curY = getY();
			var self = this;
			var step = random( 10 ) + 10;
			var duration = 2500;
			var easing = "swing";
			var animation;
			
			var vector = random( 2 );
			
			switch ( vector ) {
				case 3: // moving left
					if( this.X - 20 < curX - step )
						animation = { left: '-=' + step };
					break    
				case 4: // moving right				    
					if( this.X + 20 > curX + step )
						animation = { left: '+=' + step };
					break
				case 2: // moving up
					if( this.Y - 50 < curY - step )
						animation = { top: '-=' + step }; 
					break
				case 1: // moving down
					if( this.Y - 5 > curY - step )
						animation = { top: '+=' + step };	
				    break 			  
			};
			
			if( animation ){
				this.Element.animate( animation, duration, easing, 
					function(){
						self.StartAnimation();
					}
				);	
			}else{
			    this.StartAnimation();
			}
		}
	}
};

/* class FindForm */
var findForm = function( form ){
    var clBlack = '#000000';
    var clGray = '#AAAAAA';
    var formId = form.attr('id');
    var srchText = $("form#" + formId + " > input[@name = keyword]");
    var submitBtn = $("form#" + formId + " > button[@name = findBtn]");
    var text = srchText.val();
    
    srchText.attr({ value: text });
    srchText.css( 'color', clGray );
    
    srchText.blur(
	    function(){
	        if( !srchText.val() ){
	            srchText.css( 'color', clGray );
	            srchText.attr( {value:text} );
	        }
	    }
	);
		
	srchText.focus(
	    function(){
	        if( srchText.val() == text ){
	            srchText.css( 'color', clBlack );
	            srchText.attr( {value:''} );
	        }
	    }
	);
	
	submitBtn.bind( "click", 
	    function(){
            if( srchText.val() != text ){
	            form.submit();
	        }
	    }
	);
    
    return {
        submit: function(){
            
        }
    }
}

/* class curbstoneButtons */
var curbstoneBtn = function( element, url ){
	var self = this;
	
	this.Btn = element.find( "img:first" );
	this.BtnOver = element.find( "img:last" );
	this.Href = url;
	this.Active = true;
	
	this.BtnOver.css({ "display": "none" });
	element.css({ "cursor": "pointer" });
	
	if( this.Btn.css("display") == "none" ){
	    this.Active = false;
	    this.BtnOver.show();
	};
	
	this.Btn.hover( 
	    function(){
	        if( self.Active ){
	            self.Btn.hide();
	            self.BtnOver.show();
	        }
	    },
	    function(){}
	);
	
	this.BtnOver.hover( 
	    function(){},
	    function(){
	    	if( self.Active ){
	            self.BtnOver.hide(); 
	            self.Btn.show(); 
	        }     
	    }
	);
	
	this.BtnOver.click(
	    function(){
		    window.location.href = self.Href;
		}
	);
	
	return {
	
	}	
};

/* class DownMenu */
var downMenu = function( menu ){	
	var self = this;
		
	this.Menu = menu;
	this.SubMenu = menu.find("div");
	this.Links = this.SubMenu.find("a");
	this.LinksHeight = 0;
	this.Fade = 500;
	this.XSep = jQuery.browser.msie ? 0:12;
	this.YSep = jQuery.browser.msie ? 8:12;
	
    this.MenuOffset = this.Menu.offset();    
    this.SubMenuWidth = this.SubMenu.css( "width" );
    this.SubMenuHeight = this.SubMenu.height();
    
    this.YPos = this.MenuOffset.top - ( this.SubMenu.height() + this.YSep );
    this.XPos = this.MenuOffset.left;
    
    if( this.YPos < 0 ){
        this.YPos = 0;
        this.XPos = this.XPos + this.Menu.width() + this.XSep;
    }
    
    this.SubMenu.css({ "top": self.YPos });
	this.SubMenu.css({ "left": self.XPos });
	this.SubMenu.css({ "display": "none" });
	//this.SubMenu.css({ "width": "0px" });
	///this.SubMenu.css({ "height": ( this.Links.length * 10 ) + "px" });
	
	this.inprocess = false;
	
	/* ----------------------------------------------- */
	/*
	this.Menu.mouseout(function(){
        self.inprocess = self.inprocess ? false : true; 
	    
	    if( self.inprocess ){		
		    self.SubMenu.animate({
		        width: "0px",
                opacity: 0                   
            },
            self.Fade,
            function(){
                self.SubMenu.css({ 'display':'none' });
                self.inprocess = false;
            }
            );
        }
    }).mouseover(function(){
        self.inprocess = self.inprocess ? false : true;
	    
	    if( self.inprocess ){
	        self.SubMenu.css({ 'display':'block' });
		    self.SubMenu.animate({
		        opacity: 0.9,
		        width: self.SubMenuWidth,
		        height: self.SubMenuHeight      
            },
            self.Fade,
            function(){
                self.inprocess = false;
            }
            );
        }
    });*/
    /* ----------------------------------------------- */    

	this.Hover = function() {/*
	    self.inprocess = self.inprocess ? false : true;
	    
	    if( self.inprocess ){
	        self.SubMenu.css({ 'display':'block' });
		    self.SubMenu.animate({
		        opacity: 0.9,
		        width: self.SubMenuWidth,
		        height: self.SubMenuHeight      
            },
            self.Fade,
            function(){
                self.inprocess = false;
            }
            );
        }*/
		self.SubMenu.show();
	};
		
	this.Over = function( elName ) {/*
	    self.inprocess = self.inprocess ? false : true; 
	    
	    if( self.inprocess ){		
		    self.SubMenu.animate({
		        width: "0px",
                opacity: 0                   
            },
            self.Fade,
            function(){
                self.SubMenu.css({ 'display':'none' });
                self.inprocess = false;
            }
            );
        }*/
		self.SubMenu.hide();
	};
	
	this.Menu.hover( this.Hover, this.Over );
	
	return {}
};
