/**
* Put any style specific JavaScript code in here.
*
* @author Victor Sumner <vsumner@wecreate.com>
* @version $Id: style.js 16 2008-01-15 15:06:34Z vsumner $
* @package semantic_theme
*/
var cl_lightboxes;

var cl_locationSelect;

function cl_closeLocationBox( id ){
    if( typeof cl_lightboxes != 'undefined' ){
        cl_lightboxes.close();
        cl_lightboxes = false;
    }
}

function cl_openWindow( str ){

    if( !$('cl_location_window') ){
        var element = new Element('div',{'id': 'cl_location_window', 'styles': {'display': 'none'}}).setHTML( str );
        element.injectInside(document.body);
    } else {
        $('cl_location_window').setHTML( str );
    }

    cl_lightboxes = ModalBox.init( 'cl_location_window', {initialWidth:560,initialHeight:860} );

    var url = window.location;
    if( $('page_from') ){
        $('page_from').value = url;
    }

    // setup the quick select
    var quickLinks = $$('a.quicklocationLink');

    if( quickLinks.length > 0 ){
        for( var i = 0; i < quickLinks.length; i++ ){
            quickLinks[i].addEvent('click', function( event ){
                new Event( event ).stop();
                var city_id = this.id;
                // make sure we have the right id
                if( city_id.test('location_city_') ){
                    city_id = city_id.substring(14).toInt();
                    $('location_city').value = this.getText();
                    $('location_city_id_box').value = city_id;
                    $('container_location_city').empty();
                    $('location_city_form').submit();
                    // show ajax
                    if( $('locationPickerContainer') ){
                        var ajaxProgressContainer = new Element('div',{'class':'ajaxProgressContainer'});
                        new Element('h4').setText('Please wait while we change your city.').injectInside( ajaxProgressContainer );
                        new Element('img',{'src':'/images/ajax-loader.gif'}).injectInside( ajaxProgressContainer );
                        $('locationPickerContainer').empty();
                        ajaxProgressContainer.injectInside( $('locationPickerContainer') );
                    }
                }
            } );
        }
    }

    var picker = new ConnectorLocationPicker( 'location_q', {'debug':false, 'lightbox':cl_lightboxes} );
    picker.openWindow();
}

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

    cl_locationSelect = new ConnectorLocationSelect();

    if ($('header-logout-button')) {
        $('header-logout-button').onclick = function(){
            if(confirm('You are about to logout. Click OK to continue.')){
                document.location='/settings/logout/';
                return false;
            }
            return false;
        } // end onclick listener
    } // end if

    if( $( 'cl_content_by_hidden' ) && $( 'sponsor' ) ){
        // get the inner html
        var contentBy = $( 'cl_content_by_hidden' ).innerHTML;
        $( 'sponsor' ).empty();
        $( 'sponsor' ).innerHTML = contentBy;

    }
    if( $('sbCurrentLocationLink') ){
        $('sbCurrentLocationLink').addEvent('click',function( event ){
            new Event( event ).stop();
            cl_locationSelect.changePanel( 'location', true );
        });
    }
    if( $('cl_changeLocationButton') ){
        $('cl_changeLocationButton').addEvent('click',function( event ){
            new Event( event ).stop();
            cl_locationSelect.changePanel( 'location', true );
        });
    }
});

var clMenuHover = function() {
    // get the current active link
    var activeMenuLinks = $$('a.clMenuActive');
    if( activeMenuLinks && activeMenuLinks.length > 0 ){
        var activeMenuLink = activeMenuLinks[0];
        activeMenuLink.parentNode.onmouseover = function(){
            this.className+=" clmenuhover";
        };
        activeMenuLink.parentNode.onmouseout=function() {
            this.className=this.className.replace(new RegExp(" clmenuhover\\b"), "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", clMenuHover);