YAHOO.namespace('IEDesign');

YAHOO.IEDesign.Submission = function(){

	function showPopup(targetId, popup, wrapper)
	{
		popup.innerHTML = "<div style='margin-left:230px;margin-bottom:85px;margin-top:85px'><img src='/img/ajax.gif'></div>";

        sUrl = "/submissions/id/" + targetId;

        var postData = "username=anonymous&userid=0";

        var handleSuccess = function(o){

            if(o.responseText !== undefined){

                popup.innerHTML = o.responseText;

                
                var region = $D.getRegion(targetId);

                $D.setStyle(wrapper, 'display', 'block');
                $D.setXY(wrapper, [region.left + 25, region.bottom -300]);
            }
        }

        var callback =
        {
            success:handleSuccess
        };


        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
	}
	
    function init(){

        $E = YAHOO.util.Event;
        $D = YAHOO.util.Dom;
        $ = $D.get;

        var subul = $D.getElementsByClassName('submi');
        var popupwrap = $D.getElementsByClassName('pop-up-wrapper');
        var popup = $D.getElementsByClassName('pop-up');
        var popclose = $D.getElementsByClassName('pop-close');
        
        $E.on(popclose[0],'click',function(e){

            $E.stopEvent(e);

            $D.setStyle(popupwrap[0], 'display', '');
        });
        
        $E.on(popclose[1],'click',function(e){

            $E.stopEvent(e);

            $D.setStyle(popupwrap[0], 'display', '');
        });
        

        for ( var i in subul ){
        	
            $E.on(subul[i],'click',function(e){

                $E.stopEvent(e);
                
                var targetId = e.target.id;
                
                showPopup(targetId, popup[0], popupwrap[0]);

            });
            
            // check href for click through
            var href = location.href;
            
            var exploded = href.split('#');
            
            if (2 == exploded.length)
            {
            	var submission = $D.get(exploded[1]);
            	
            	showPopup(submission.id, popup[0], popupwrap[0]);
            }
            
            
            

        }
    }

    return {
        init: init
    }

}();

YAHOO.IEDesign.Cvs = function(){

    function init(){

        $E = YAHOO.util.Event;
        $D = YAHOO.util.Dom;
        $ = $D.get;

        var whats = $D.get('what-is-this');
        var whatim = $D.get('what-is-this-image');

        $E.on(whats,'click',function(e){

            $E.stopEvent(e);

            $D.setStyle(whatim, 'display', 'block');
        });

    }


    return {
        init: init
    }

}();

YAHOO.IEDesign.Search = function(){

    function init() {

        var $E = YAHOO.util.Event,
        $D = YAHOO.util.Dom,
        $ = YAHOO.util.Dom.get;

        $E.on('mce-EMAIL', 'focus', function(e){

            var v = $('mce-EMAIL').value;

            if ('your email address' == v){
                $('mce-EMAIL').value = '';
            }

        });

        $E.on('mce-EMAIL', 'blur', function(e){

            var v = $('mce-EMAIL').value;

            if ('' == v){
                $('mce-EMAIL').value = 'your email address';
            }

        });
        
       
        
        var extraButton = $D.getElementsByClassName('extra-button')[0];
  
        $E.on(extraButton, 'focus', function(e){

            var v = extraButton.value;

            if ('your email address' == v){
            	extraButton.value = '';
            }

        });

        $E.on(extraButton, 'blur', function(e){

            var v = extraButton.value;

            if ('' == v){
            	extraButton.value = 'your email address';
            }

        });
     

    }

    return {
        init: init
    }

}();

YAHOO.IEDesign.Ticker = function(){

    var $E = YAHOO.util.Event;                  //establish YUI shortcuts
    var $D = YAHOO.util.Dom;                    //
    var $C = YAHOO.util.Cookie;                 //
    var $ = $D.get;                             //

    var animTimeMultiplier = 20;                //establish number of seconds taken for 3 blocks to shift off screen
    var animFastMultiplier = 20;                //establish how many times faster the scrolling takes place when the skip button is held down
    var blockSize  = 210;                       //establish the size in pixels of each block
    var blockSizeTilt = 20;                     //extra pixels to be added to the size of the block container (bug fix)

    var tickerList;                             //initialise common variables
    var scrollAnim;                             //
    var maxAnimTime;                            //
    var maxRightPos;                            //
    var maxLeftPos;                             //
    var currentDirection;                       //

    function animate(from, to, time, bounce) {

        var settings = { marginLeft: {from: from, to: to } }
        scrollAnim = new YAHOO.util.Anim(tickerList, settings, time);

        if (bounce == 1) {
            scrollAnim.onComplete.subscribe(function(){
                    if (currentDirection == 'rtl') {
                        currentDirection = 'ltr';
                        animate(maxLeftPos, maxRightPos, maxAnimTime, 1);
                    } else {
                        currentDirection = 'rtl';
                        animate(maxRightPos, maxLeftPos, maxAnimTime, 1);
                    }
            });
        }

        scrollAnim.animate();

    }

    function reAnimate(e, newDirection, newSpeed, bounce) {
        var animProgress;
        var newAnimTime;
        var toPos;
        var currentPos = parseInt($D.getStyle(tickerList, 'marginLeft').replace(/px/, ""));

        $E.stopEvent(e);
        scrollAnim.onComplete.unsubscribe();
        scrollAnim.stop();

        animProgress = (maxLeftPos - currentPos) / maxLeftPos;

        if (newDirection == 'ltr') {                                    //new direction: right to left
            newAnimTime = ( maxAnimTime * ( 1 - animProgress ) );
            toPos = maxRightPos;
            currentDirection  = 'ltr';
        } else {                                                        //new direction: left to right
            newAnimTime = ( maxAnimTime * animProgress );
            toPos = maxLeftPos;
            currentDirection  = 'rtl';
        }

        if (newSpeed == 'fast') {
            newAnimTime = newAnimTime / animFastMultiplier ;
        }

        animate(currentPos, toPos, newAnimTime, bounce);
    }

    function init() 
    {

        var ticker = $('ticker');                                               //if no ticker list box, cancel
        if(ticker == null){
            return;
        }

        var blockCount = $D.getElementsByClassName('ticker_images_li');         //if no ticker list items, cancel
        if(blockCount == null){
            return;
        }

        blockCount = blockCount.length;                                         //if less than 4 ticker list items, cancel
        if (blockCount < 4) {
            return;
        }

        tickerList    = $D.getElementsByClassName('ticker_images')[0];                          //grab the ticker list element
        maxAnimTime   = animTimeMultiplier * ( blockCount / 3 );                                //determine the length of time the animation should take
        maxRightPos   = 0;                                                                      //determine the number of pixels the entire group of blocks must be pulled left to show FIRST 3 blocks
        maxLeftPos    = ( ( blockSize * blockCount ) - ( 3 * blockSize ) ) + blockSizeTilt;     //determine the number of pixels the entire group of blocks must be pulled left to show LAST 3 blocks
        maxLeftPos    = ( maxLeftPos - ( maxLeftPos * 2 ) );                                    //

        $D.setStyle( 'ticker_images', 'width', ( ( blockSize * blockCount ) + blockSizeTilt ) + 'px' );     //set the width of the block container (must be set dynamically)
        $D.setStyle( 'ticker_images', 'height', 'auto' );                                                   //reset the height of the block container
        $D.setStyle( 'ticker', 'width', '600px' );                                                          //shift ticker box to make space for skipper buttons
        $D.setStyle( 'ticker', 'left', '30px' );                                                            //
        $D.setStyle( 'ticker-skipper-right', 'display', 'block' );                                          //re-enable the skipper buttons
        $D.setStyle( 'ticker-skipper-left', 'display', 'block' );                                           //


        var btnFastRight = $D.get('ticker-skipper-right');
        var btnFastLeft = $D.get('ticker-skipper-left');

        //define handlers for forward / backward buttons
        $E.on(btnFastRight, 'mousedown', function(e){ reAnimate(e, 'rtl', 'fast', 0); });
        $E.on(btnFastRight, 'mouseup', function(e){ reAnimate(e, 'rtl', 'slow', 1); });
        $E.on(btnFastLeft, 'mousedown', function(e){ reAnimate(e, 'ltr', 'fast', 0); });
        $E.on(btnFastLeft, 'mouseup', function(e){ reAnimate(e, 'ltr', 'slow', 1); });

        //start initial animation
        currentDirection  = 'rtl';
        animate(maxRightPos, maxLeftPos, maxAnimTime, 1);

    }

    return {
        init: init
    }

}();

YAHOO.util.Event.onDOMReady(function(){

    $E = YAHOO.util.Event;
    $D = YAHOO.util.Dom;
    $ = $D.get;

    YAHOO.IEDesign.Ticker.init();
    YAHOO.IEDesign.Submission.init();
    YAHOO.IEDesign.Search.init();
    YAHOO.IEDesign.Cvs.init();

});
