window.addEvent('domready',function()
{

    
    //Global -------------------------------------------------------------------

    var loc = window.location.toString();

    //Menu ---------------------------------------------------------------------
    $('nav').MooDropMenu(
    {
        onOpen: function(el)
        {
                if(Browser.ie)
                {
                    el.fade('show');
                }
                else
                {
                    el.fade('in')
                }
        },
        onClose: function(el)
        {
                if(Browser.ie)
                {
                    el.fade('hide');
                }
                else
                {
                    el.fade('out');
                }
                
        },
        onInitialize: function(el)
        {
            el.fade('hide').set('tween',{duration:'short'});
        }
    });

    $$('#nav ul').forEach(function(item)
    {
        item.setStyle('display','block')
    })


    function setNavOver()
    {
        if(loc.contains('about-us') || loc.contains('news'))
        {
            $('nav-about').removeClass('first');
            $('nav-about').getFirst('a').addClass('nav-over');
        }
        else if (loc.contains('our-services'))
        {
            $('nav-services').removeClass('first');
            $('nav-services').getFirst('a').addClass('nav-over');
        }
        else if(loc.contains('our-practices'))
        {
            $('nav-practices').removeClass('first');
            $('nav-practices').getFirst('a').addClass('nav-over');
        }
        else if(loc.contains('contact-us'))
        {
            $('nav-contact').removeClass('first');
            $('nav-contact').getFirst('a').addClass('nav-over');
            if($('general'))
            {
//                alert('hey')
                var check   =   new FormCheck('general',
                                {
                                        submitByAjax:true,
                                        ajaxResponseDiv:'responder',
                                        onAjaxRequest:function(response)
                                        {
                                                $('responder').set('html','sending your message');
                                        },
                                        onAjaxFailure:function()
                                        {
                                                $('responder').set('html','sorry there was an error sending your message');
                                        },
                                        onAjaxSuccess:function(response)
                                        {
                                                $('general').reset();
                                        },
                                        display:
                                        {
                                                scrollToFirst:false,
                                                tipsOffsetX:5,
                                                tipsOffsetY:40
                                        },
                                        alerts:
                                        {
                                                email:'Please enter a valid email'
                                        }
                                });
            }
        }
        else
        {
            $('nav-home').removeClass('first');
            $('nav-home').getFirst('a').addClass('nav-over');
        }
    }

    setNavOver();

    $$('#nav .first').addEvents(
    {
        'mouseover':function()
        {
            var anchor = this.getFirst('a')
            anchor.addClass('nav-over')
        },
        'mouseleave':function()
        {
            var anchor = this.getFirst('a')
            anchor.removeClass('nav-over')
        }
    })    

    //Index slideshow ----------------------------------------------------------

    if($('srg-ss'))
    {
        var indexSS = new SlideShow('srg-ss',
        {
                duration: 750,
                link:'cancel',
                autoplay: true,
                delay:9000,
                transition:'expoLeft'
       });

       var controls = $$('.btn')
       var morphOptions	= {link:'cancel',duration:'normal',transition:Fx.Transitions.Sine.easeOut};
       var grey = "#CCCCCC";
       var blue = "#44b1da";

       controls.set('morph',morphOptions)
       controls[0].morph({'background-color':[grey, blue]});

       controls.each(function(item,index)
       {
           item.addEvent('click',function()
           {
                var curIn = indexSS.slides.indexOf(indexSS.current);
                var transition = (curIn < index) ? 'expoLeft' : 'expoRight';
                indexSS.show(index, { transition: transition });
                controls.morph({'background-color':grey});
                item.morph({'background-color':blue});
                indexSS.pause();
           })
            
       })

       indexSS.addEvent('showComplete',function()
       {
           var currentIndex = indexSS.slides.indexOf(indexSS.current);
           if(currentIndex == 0)
           {
               controls[4].morph({'background-color':grey});
           }
           else
           {
               controls[currentIndex-1].morph({'background-color':grey});
           }
           
           controls[currentIndex].morph({'background-color':blue});
       })
        


        $$('.xfader').addEvents(
        {
            'mouseenter':function()
            {
                $$('.img-grey').fade('out');
            },
            'mouseleave':function()
            {
                $$('.img-grey').fade('in');
            }
        })
    }

    if($('news-ss'))
    {
        var newsSS = new SlideShow('news-ss',
        {
                duration: 750,
                link:'cancel',
                autoplay: true,
                delay:5000,
                transition:'fadeThroughBackground'
        })


    }

    //Index info arrows --------------------------------------------------------

    if($$('.arrow'))
    {
        $$('.arrow').forEach(function(item)
        {
            item.addEvents(
            {
                'mouseenter':function()
                {
                    this.addClass('arrow-on');
                },
                'mouseleave':function()
                {
                    this.removeClass('arrow-on');
                }
            })
        })
        
    }

    //Map replacement ----------------------------------------------------------

    if($('pic'))
    {
        var pic = $('pic').getElement('img')
        $('pic').empty()
        $('pic-holder').adopt(pic);
    }

    if($('address'))
    {
        var add = $('address').get('html')
        $('address').empty()
        $('add-holder').set('html',add);
    }

    //iOS iFrame/img swap ------------------------------------------------------


    //TIPS ---------------------------------------------------------------------

    if($$('.practiceTips'))
    {
        var srgTips = new Tips('.practiceTips',
        {
           // offset:{x:20,y:-130}
        });

       srgTips.addEvents(
       {
           'show':function(tip)
           {
               tip.fade('in');
           },
           'hide':function(tip)
           {
               tip.fade('out');
           }
       })
    }

    //SEARCH INPUT -------------------------------------------------------------
    
    if($('s'))
    {
       var searchText = $('s'),
	   searchVal = 'Search this site';

	searchText.set('value',searchVal);

	searchText.addEvent('focus',function()
	{
		if(this.get('value') == searchVal)
		{
			this.set('value','');
		}
		else
		{
			return;
		}
	})

	searchText.addEvent('blur',function()
	{
		if(this.get('value') == '')
		{
			this.set('value',searchVal);
		}
		else
		{
			return;
		}
	})
    }
	

})

