﻿var ColoradoHarmony =
{
    BaseUrl:  'http://www.snowatfirstsight.com/',
    ImageDiv: null,
    VoteBTN:  null,
    Load:     function() 
    {
        var img = document.createElement('img')

        img.style.display    = 'none'
        img.style.visibility = 'hidden'

        ColoradoHarmony.ImageDiv = img

        $$("li a").each(function(el) 
        {
            el.title = ''

            if (el.innerHTML == "Visit Colorado") 
            {
                el.onclick = function() 
                {
                    pageTracker._trackEvent('Visit Colorado', 'Click');
                }
            }
        })
        $$("a").each(function(el) 
        {
            if (el.href.indexOf("www.snowatfirstsight.com") != -1) 
            {
                el.onclick = function() 
                {
                    pageTracker._link(this.href);
                    return false;
                }
            }
        })
        $$("div.visitcolorado a").each(function(el) 
        {
            el.onclick = function() 
            {
                pageTracker._trackEvent('Visit Colorado', 'Click - ' + this.innerHTML, this.href)
            }
        })
        if (ColoradoHarmony.OnVideoPage()) 
        {
            $$("div.widget").each(function(el) 
            {
                var TheColumn = el.parentNode.getAttribute('columnno')

                if (TheColumn == '1') 
                {
                    ColoradoHarmony.CreateSocialWidget(ColoradoHarmony.GetMediaKey(document.URL), el)
                    //ColoradoHarmony.CreateWidget(ColoradoHarmony.GetMediaKey(document.URL), el)
                    throw $break;
                }
            })
        }
        else if (ColoradoHarmony.OnVideosPage()) 
        {
//            $$("div.box-wrap center h3").each(function(el) 
//            {
//                el.innerHTML = '<div style="margin-top: 10px; font-weight: bold;">Shh! The artists are at work. Check back tomorrow to see their videos.</div><div style="margin: 10px 180px 10px 180px; font-size: 12px;">In the meantime, why not sketch out a few video ideas of your own? Who knows, maybe you’ll be chosen to experience the joys of falling in love with Snow at First Sight.</div>'
//            })
//            $$("li.location").each(function(el) 
//            {
//                try 
//                {
//                    var TheUL = el.parentNode
//                    var TheELs = Element.getElementsBySelector(TheUL.parentNode.parentNode, "div ul li a.title")

//                    if (TheELs.length > 0) 
//                    {
//                        var TheKey = ColoradoHarmony.GetMediaKey(TheELs[0].href)
//                        var TheLI  = document.createElement('li')
//                        TheLI.innerHTML = '<img src="http://www.snowatfirstsight.com/handlers/voting.ashx?f=imgs&key=' + TheKey + '" />'
//                        TheUL.appendChild(TheLI)
//                    }
//                }
//                catch (e) { }
//            })
        }
        else if (ColoradoHarmony.OnDefaultPage()) 
        {           
            ColoradoHarmony.CreateFacebookWidget()
            ColoradoHarmony.CreateTwitterWidget ()
            ColoradoHarmony.CreateFlickrWidget  ()
        }
        else if (ColoradoHarmony.OnRegisterPage()) 
        {
            //<!-- Start of DoubleClick Spotlight Tag: Please do not remove-->
            //<!-- Activity Name for this tag is:Snow at First Sight Login -->
            //<!-- Web site URL where tag should be placed: http://www.coloradosnowatfirstsight.com/Register.aspx -->
            //<!-- This tag must be placed within the opening <body> tag, as close to the beginning of it as possible-->
            //<!-- Creation Date:10/23/2009 -->
            var el = document.createElement('div')
            el.style.position = 'absolute'
            el.style.top = '0px'
            el.style.left = '0px'
            var axel = Math.random() + ""
            var a = axel * 10000000000000
            el.innerHTML = '<IFRAME SRC="http://fls.doubleclick.net/activityi;src=1320480;type=gener280;cat=snowa699;ord=1;num=' + a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>'
            document.body.appendChild(el)
            //<!-- End of DoubleClick Spotlight Tag: Please do not remove-->
        }
        else if (ColoradoHarmony.OnUploadDonePage()) 
        {
            //<!-- Start of DoubleClick Spotlight Tag: Please do not remove-->
            //<!-- Activity Name for this tag is:Snow at First Sight Upload Confirm/Thank You -->
            //<!-- Web site URL where tag should be placed: http://www.coloradosnowatfirstsight.com/MyMediaList.aspx?pagetype=myclips -->
            //<!-- This tag must be placed within the opening <body> tag, as close to the beginning of it as possible-->
            //<!-- Creation Date:10/23/2009 -->
            var el = document.createElement('div')
            el.style.position = 'absolute'
            el.style.top = '0px'
            el.style.left = '0px'
            var axel = Math.random() + ""
            var a = axel * 10000000000000
            el.innerHTML = '<IFRAME SRC="http://fls.doubleclick.net/activityi;src=1320480;type=gener280;cat=snowa155;ord=1;num=' + a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>'
            document.body.appendChild(el)
            //<!-- End of DoubleClick Spotlight Tag: Please do not remove-->
        }
    },
    CreateFacebookWidget: function()
    {

    },
    CreateTwitterWidget:  function()
    {

    },
    CreateFlickrWidget:   function() 
    {

    },
    GetMediaKey: function(url) 
    {
        var qs = url.split('?')[1]
        var items = qs.split('&')

        for (var i = 0; i < items.length; i++) 
        {
            var keyvalue = items[i].split('=')

            if (keyvalue[0].toLowerCase() == 'key') 
            {
                return keyvalue[1].toLowerCase()
            }
        }

        return null
    },
    SetClass: function(el, classname) 
    {
        el.className = classname
        el.setAttribute('class', classname)
    },
    CreateWidget: function(key, el) 
    {
        var box = document.createElement('div')
        ColoradoHarmony.SetClass(box, 'box')

        var boxwrap = document.createElement('div')
        ColoradoHarmony.SetClass(boxwrap, 'box-wrap')

        var boxbottom = document.createElement('div')
        ColoradoHarmony.SetClass(boxbottom, 'box-bottom')
        boxbottom.innerHTML = '<div> </div>'

        var boxtop = document.createElement('div')
        ColoradoHarmony.SetClass(boxtop, 'box-top')
        boxtop.innerHTML = ' '

        var boxcontent = document.createElement('div')
        ColoradoHarmony.SetClass(boxcontent, 'box-content clearfix')

        var widgetheader = document.createElement('div')
        ColoradoHarmony.SetClass(widgetheader, 'widget_header')
        widgetheader.innerHTML = '<div><table class="widget_header_table" width="100%"><tr><td class="widget_title" width="90%" align="left"><h3><span>Vote &amp; Share</span></h3><h3> </h3></td></tr></table></div>'

        var widgetdiv = document.createElement('div')
        widgetdiv.innerHTML = '<div class="widget_body"><table style="border-collapse: collapse;" border="0" cellspacing="0"><tr><td colspan="2"><div class="w-assetownerwidget"><div class="block"><div style="text-align: left; padding-left: 10px;"><img src="http://www.snowatfirstsight.com/handlers/voting.ashx?f=imgs&key=' + key + '" /></div><br/><div id="votebuttondiv"></div></div></div></td></tr></table></div></div>'

        box.appendChild(boxwrap)
        box.appendChild(boxbottom)

        boxwrap.appendChild(boxtop)
        boxwrap.appendChild(boxcontent)

        boxcontent.appendChild(widgetheader)
        boxcontent.appendChild(widgetdiv)

        el.parentNode.insertBefore(box, el.nextSibling)

        ColoradoHarmony.CreateButtons(key, document.getElementById('votebuttondiv'))
    },
    CreateSocialWidget: function(key, el) 
    {
        var box = document.createElement('div')
        ColoradoHarmony.SetClass(box, 'box')

        var boxwrap = document.createElement('div')
        ColoradoHarmony.SetClass(boxwrap, 'box-wrap')

        var boxbottom = document.createElement('div')
        ColoradoHarmony.SetClass(boxbottom, 'box-bottom')
        boxbottom.innerHTML = '<div> </div>'

        var boxtop = document.createElement('div')
        ColoradoHarmony.SetClass(boxtop, 'box-top')
        boxtop.innerHTML = ' '

        var boxcontent = document.createElement('div')
        ColoradoHarmony.SetClass(boxcontent, 'box-content clearfix')

        var widgetheader = document.createElement('div')
        ColoradoHarmony.SetClass(widgetheader, 'widget_header')
        widgetheader.innerHTML = '<div><table class="widget_header_table" width="100%"><tr><td class="widget_title" width="90%" align="left"><h3><span>Check us out on...</span></h3><h3> </h3></td></tr></table></div>'

        var widgetdiv = document.createElement('div')
        widgetdiv.innerHTML = '<div class="widget_body"><table style="border-collapse: collapse;" border="0" cellspacing="0"><tr><td colspan="2"><div class="w-assetownerwidget"><div class="block"><div id="socialbuttondiv"></div></div></div></td></tr></table></div></div>'

        box.appendChild(boxwrap)
        box.appendChild(boxbottom)

        boxwrap.appendChild(boxtop)
        boxwrap.appendChild(boxcontent)

        boxcontent.appendChild(widgetheader)
        boxcontent.appendChild(widgetdiv)

        el.parentNode.insertBefore(box, el.nextSibling)

        var fbbtn = document.createElement('div')
        var twbtn = document.createElement('div')

        fbbtn.key = key
        fbbtn.style.margin = '0px 0px 2px 10px'
        fbbtn.style.width = '263px'
        fbbtn.style.height = '34px'
        fbbtn.style.background = 'url(' + ColoradoHarmony.BaseUrl + 'images/fbProfile.jpg)'
        fbbtn.style.cursor = 'pointer'
        fbbtn.innerHTML = '&nbsp;'

        twbtn.key = key
        twbtn.style.margin = '0px 0px 2px 10px'
        twbtn.style.width = '263px'
        twbtn.style.height = '34px'
        twbtn.style.background = 'url(' + ColoradoHarmony.BaseUrl + 'images/twProfile.jpg)'
        twbtn.style.cursor = 'pointer'
        twbtn.innerHTML = '&nbsp;'

        var cont = document.getElementById('socialbuttondiv')

        cont.parentNode.insertBefore(twbtn, cont.nextSibling)
        cont.parentNode.insertBefore(fbbtn, cont.nextSibling)

        fbbtn.onclick = function() {
            pageTracker._trackEvent('Colorado Facebook', 'Fan Page View')
            window.open('http://www.facebook.com/visitcolorado')
        }

        twbtn.onclick = function() {
            pageTracker._trackEvent('Colorado Twitter', 'Profile View')
            window.open('http://www.twitter.com/Colorado')
        }
    },
    CreateButtons: function(key, el, addli) 
    {
        var btn   = document.createElement('div')
        var fbbtn = document.createElement('div')
        var twbtn = document.createElement('div')

        btn.key = key
        btn.style.margin = '0px 0px 2px 10px'
        btn.style.width = '263px'
        btn.style.height = '38px'
        btn.style.background = ColoradoHarmony.HasVoted() ? 'url(' + ColoradoHarmony.BaseUrl + 'images/voteButtonDeactive.jpg)' :
                                                            'url(' + ColoradoHarmony.BaseUrl + 'images/voteButtonActive.jpg)'
        btn.style.cursor = ColoradoHarmony.HasVoted() ? 'default' : 'pointer'
        btn.innerHTML = '&nbsp;'

        ColoradoHarmony.VoteBTN = btn

        fbbtn.key = key
        fbbtn.style.margin = '8px 0px 2px 10px'
        fbbtn.style.width = '263px'
        fbbtn.style.height = '38px'
        fbbtn.style.background = 'url(' + ColoradoHarmony.BaseUrl + 'images/shareFacebook.jpg)'
        fbbtn.style.cursor = 'pointer'
        fbbtn.innerHTML = '&nbsp;'

        twbtn.key = key
        twbtn.style.margin     = '8px 0px 2px 10px'
        twbtn.style.width      = '263px'
        twbtn.style.height     = '38px'
        twbtn.style.background = 'url(' + ColoradoHarmony.BaseUrl + 'images/shareTwitter.jpg)'
        twbtn.style.cursor     = 'pointer'
        twbtn.innerHTML        = '&nbsp;'

        if (addli) 
        {
            var liel = document.createElement('li')
            liel.appendChild(btn)
            liel.appendChild(fbbtn)
            liel.appendChild(twbtn)
            el.parentNode.insertBefore(liel, el.nextSibling)
        }
        else 
        {
            el.parentNode.insertBefore(twbtn, el.nextSibling)
            el.parentNode.insertBefore(fbbtn, el.nextSibling)
            el.parentNode.insertBefore(btn,   el.nextSibling)
        }

        if (!ColoradoHarmony.HasVoted()) 
        {
            btn.onclick = function() 
            {
                if (ColoradoHarmony.HasVoted()) 
                {
                    alert("You've already Voted!")
                    ColoradoHarmony.VoteBTN.style.background = 'url(' + ColoradoHarmony.BaseUrl + 'images/voteButtonDeactive.jpg)'
                    ColoradoHarmony.VoteBTN.style.cursor = 'default'
                }
                else 
                {
                    pageTracker._trackEvent('Video Vote', 'Vote Click')
                    ColoradoHarmony.Vote(this.key)
                    ColoradoHarmony.VoteBTN.style.background = 'url(' + ColoradoHarmony.BaseUrl + 'images/voteButtonDeactive.jpg)'
                    ColoradoHarmony.VoteBTN.style.cursor = 'default'
                    ColoradoHarmony.VoteBTN.onclick = null
                    alert('Thank you for voting!\n\nIf you really want to see this video win the Colorado Snow at First Sight contest, share this video with your friends and family on Facebook and Twitter.')
                }
            }
        }

        fbbtn.onclick = function() 
        {
            pageTracker._trackEvent('Facebook Video Share', 'Share Click')
            window.open('http://www.facebook.com/sharer.php?u=' + escape(document.URL) + '&t=' + escape(document.title))
        }

        twbtn.onclick = function() 
        {
            BitlyCB.alertResponse = function(data) 
            {
                var s = '';
                var first_result;

                for (var r in data.results) 
                {
                    first_result = data.results[r];
                    break;
                }
                for (var key in first_result) 
                {
                    if (key == 'shortUrl') 
                    {
                        window.open('http://twitter.com/home?status=' + escape('Vote for this video to win the Colorado Snow at First Sight contest! ' + first_result[key].toString() + ' #SAFS'))
                    }
                }
            }

            BitlyClient.call('shorten', { 'longUrl': document.URL }, 'BitlyCB.alertResponse');
            pageTracker._trackEvent('Twitter Video Share', 'Share Click')
        }
    },
    Vote: function(key) 
    {
        ColoradoHarmony.ImageDiv.src = ColoradoHarmony.BaseUrl + 'handlers/voting.ashx?key=' + key
        ColoradoHarmony.SetCookie('voted', key, 1)
    },
    HasVoted: function() 
    {
        return ColoradoHarmony.GetCookie('voted') != null
    },
    OnDefaultPage: function() 
    {
        if (document.URL.toLowerCase().indexOf('/default.aspx') != -1) 
        {
            return true
        }
        return false
    },
    OnRegisterPage: function() 
    {
        return (document.URL.toLowerCase().indexOf('register.aspx') != -1)
    },
    OnUploadDonePage: function() 
    {
        return (document.URL.toLowerCase().indexOf('clipedit2.aspx') != -1)
    },
    OnVideosPage: function() 
    {
        if (document.URL.toLowerCase().indexOf('medialist.aspx') != -1) 
        {
            if (document.URL.toLowerCase().indexOf('type=video')) 
            {
                return true
            }
        }
        return false
    },
    OnVideoPage: function() 
    {
        if (document.URL.toLowerCase().indexOf('media.aspx') != -1) 
        {
            if (document.URL.toLowerCase().indexOf('type=video')) 
            {
                return true
            }
        }
        return false
    },
    SetCookie: function(key, value, days) 
    {
        var setValue = key + '=' + escape(new String(value));

        if (days) 
        {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var setExpiration = "; expires=" + date.toGMTString();
        }
        else var setExpiration = "";

        var cookieString = setValue + setExpiration

        document.cookie = cookieString;
    },
    GetCookie: function(key) 
    {
        var keyEquals = key + "=";
        var value = null;

        document.cookie.split(';').invoke('strip').each(function(s) 
        {
            if (s.startsWith(keyEquals)) 
            {
                value = unescape(s.substring(keyEquals.length, s.length));
                throw $break;
            }
        });
        
        return value;
    }
}

Event.observe(window, "load", ColoradoHarmony.Load)


