
/*make sure the live copy of this file only has a leading slash for image paths*/


//form clear function
$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};


// vwhen the page is ready
$(document).ready(function() {

	//popups for the friend finder
	
	$('.findFriends li.friend ').each(function(){
		$friendClass = $(this).attr('id');
		$friendTip = $('.friendTips li.'+$friendClass).html();
		//alert($friendTip);
		
		$(this).qtip({
		   content: $friendTip, //$friendTip,
		   style: { 
		      width: 200,
		      padding: 5,
		      background: 'white',
		      color: 'black',
		      textAlign: 'center',
		      border: {width: 5,radius: 5,color: '#B5B5B2'},
		      tip: 'leftMiddle',
		      name: 'light' // Inherit the rest of the attributes from the preset dark style
		   },
		   position: {
		   	corner: {
		   		target: 'rightMiddle',
		   		tip: 'leftMiddle'
		   	}
			,adjust: { x: 0, y: -35 }
		   }
		});
	});
	
	//replaces the default browser a link title popup
	//$('a[title]').qtip({ style: { name: 'light', tip: true } })

	
    //resizable textarea
    if ($('textarea.resizable').length) {
        $('textarea.resizable:not(.processed)').TextAreaResizer();
    }



    //my profile - toggle settings block with the jumplink
    $('.userSettings').hide();
    $profileRevealText = $('.profileReveal a').html();
    $('.profileReveal a').click(function() {

        $('.userSettings').toggle();

        if ($('.userSettings').is(':visible')) {
            $('.profileReveal a').html('close');
        } else {
            $('.profileReveal a').html($profileRevealText);
        }
        return false;
    });

    //my profile chooser 
    if ($('.myProfileChooser').length) {
        $toggleMyProfileChooser = function($myIdx) {
            $('.chooserContent .contentChoice').each(function() {
                $('.chooserContent .contentChoice').hide();
            });
            $('.myProfileChooser .choice').addClass('dark').removeClass('white').removeClass('selected');

            if ($myIdx > -1) {
                $('.chooserContent .contentChoice').eq($myIdx).show();
				$('.myProfileChooser .choice').eq($myIdx).removeClass('dark').addClass('selected').addClass('white');
            } else {
                $('.chooserContent .contentChoice').eq(1).show();
				$('.myProfileChooser .choice').eq(1).removeClass('dark').addClass('selected').addClass('white');
            }
        };
        $('.myProfileChooser .choice').click(function() {
            $idx = 0;
            $chosen = $(this).attr('id');
            $('.myProfileChooser .choice').each(function() {
                if ($(this).attr('id') === $chosen) {
                    //alert($idx);
                    $toggleMyProfileChooser($idx);

                }
                $idx++;
            });
            return false;
        });

        $('.myProfileChooser .choice').hover(
	      function() {
	          if (!$(this).hasClass('selected')) {
	       		$(this).removeClass('dark');
	          }
	      },
	      function() {
	          if (!$(this).hasClass('selected')) {
	        	$(this).addClass('dark');
	          }
	      }
	    );

		$('.myProfileChooser .choice .holder').append('<div class="pointer"></div>');

        $toggleMyProfileChooser();
    }
    //end my profile chooser stuff



    //*********Messages************//
    
    //add .last class to appropriate messages to ensure proper spacing
    $('.messageList .contentBox .holder, #messagesBlock .contentBox .holder').each(function(){
		$(this).find('div.message:last').addClass('last');
	});


    //message reply links
    $('.message a.reply').click(function() {
        $(this).parents('.message').find('#messageContent').show().addClass('open');
        $(this).parents('.contentBox').find('div.reply').show();
        //mark as read
        $(this).parents('.message').removeClass('unread'); //should save this state to db too.
        this.blur();
        return false;
    });
    //cancel button
    $('.messageList div.reply button.cancel').click(function() {
        $(this).parents('form').clearForm();
        $(this).parents('div.reply').hide();
        this.blur();
        return false;

    });

    //messages expand
    if ($('.message #messageContent').length) {
        $('.message td.mainColumn, .message td.icon, #messageContent a.hide ').click(function() {
            if ($(this).parents('.message').find('#messageContent').hasClass('open')) {
                $(this).parents('.message').find('#messageContent').hide().removeClass('open');
                $(this).find('.moreLink a').html('view message');
                $('div.reply').hide(); //Hide the reply (textArea) form until the users click in the reply link
            } else {
                $(this).parents('.message').find('#messageContent').show().addClass('open');
                $(this).find('.moreLink a').html('hide message');
                $('div.reply').hide(); //Hide the reply (textArea) form until the users click in the reply link
                //mark as read
                $(this).parents('.message').removeClass('unread'); //should save this state to db too. 
            }
            this.blur();

            var pnlId = $(this).parents('.message unread')[0].id;
            var msgId = (pnlId.substring(pnlId.lastIndexOf('_') + 1, pnlId.length));
            $.ajax({
                type: "GET",
                url: "/handlers/Messages.ashx?id=" + msgId,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                complete: function(result) {
                    UpdateFooter();
                }
            });
            
            return false;
        });
    }




    //*********End Messages************//	


    //search/browse accordian 

    if ($('#searchBrowse').length) {
        $('#searchBrowse').accordion({
            header: 'div.head',
            active: false,
            alwaysOpen: false,
            //animated: false, 
            autoheight: false
        });
    }
    // add the firstChild class for IE6 compat.
    $('.accordion li.heading div.head > a')
		.addClass('firstChild')
	;
    //help results
    if ($('div.help .results .accordion').length) {
        $('div.help .results .accordion').accordion({
            header: 'div.head', 
            active: false,
            alwaysOpen: false,
            //animated: false, 
            autoheight: false
        });
    }

    //Form Field Value Swap
    swapValues = [];
    $(".swapValue").each(function(i) {
        swapValues[i] = $(this).val();
        $(this).focus(function() {
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function() {
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i]);
            }
        });
    });




    //general stuff
    $('a').click(function() {
        this.blur();
    });
    $('a img').focus(function() {
        this.parent.blur();
    });


    //button hover for ie6
    $(function() {
        $('.submitBtn').hover(
        // mouseover
			function() { $(this).addClass('submitBtnHover'); },

        // mouseout
			function() { $(this).removeClass('submitBtnHover'); }
		);
    });


    //	    //facebox popup window
    //	    $('a[rel*=facebox]').facebox({
    //	        loading_image: './js/facebox/loading.gif',
    //	        close_image: './js/facebox/closelabel.gif'
    //	    });


    //
    //place context nav pointer img (the little teardrop dollop thing)
    function setPointer(contentNav) {
        if (contentNav == 'all') {
            var pointerWidth = 0;
            $('.contentNav .pointer ').each(function() {
                pointerWidth = 0;
                $(this).parent('div').find('ul li').each(function(i) {
                    if ($(this).hasClass('selected')) {
                        if (i > 0) pointerWidth = $(this).width() * .25 + pointerWidth;
                        $(this).parent('ul').parent('div').find('.pointer').css('background-position', pointerWidth + 'px bottom');
                    } else {
                        pointerWidth = $(this).width() + pointerWidth;
                    }
                });
            });
        } else if (contentNav != '') {
            //this is the stuff to run on a nav item click
            pointerWidth = 0;
            $(contentNav).find('ul li').each(function(i) {
                if ($(this).hasClass('selected')) {
                    if (i > 0) pointerWidth = $(this).width() * .25 + pointerWidth;
                    $(this).parent('ul').parent('div').find('.pointer').css('background-position', pointerWidth + 'px bottom');
                } else {
                    pointerWidth = $(this).width() + pointerWidth;
                }
            });
        } else {
            return false;
        }
    }
    //if theres a pointer on the page, set up its location
    if ($('.pointer').length) setPointer('all');

    $('.contentNav ul li a').click(function() {
	//do a check to see if there is a pointer
	if (!$(this).parent('li').parent('ul').parent('div').hasClass('noPointer')) {
        
	if ($(this).parent('li').parent('ul').parent('div').attr('id')) {
            var thisId = $(this).parent('li').parent('ul').parent('div').attr('id');
            var thisNav = '#' + thisId;
        }
        if ($(thisNav + ' .pointer').length) {
            $(this).parent('li').parent('ul').find('li').each(function() {
                $(this).removeClass('selected');
            });
            $(this).parent('li').addClass('selected');

            setPointer(thisNav);
        }

        contentToLoad = $(this).attr('href') + ' #' + $(this).attr('rel');
        //alert(contentToLoad);
        $('div .' + thisId + ' .holder').load(contentToLoad);
        return false;
	
	}//end check for pointer
    });
    //end content nav pointer

    //alert(find('div').class('swf'));

    //flash insert
    //only do the flash stuff if there is an element with the .swf class
    if ($('.swf').length) {
		
        //MGC flash
        $('.millionGirlChoir.swf').flash(
			{
			    src: '/Flash/MilllionGirlChoir/million_girl_choir.swf?sid=' + $('#sid').val(),
			    wmode: 'transparent',
			    width: '920',
			    height: '512',
			    menu: false
			},
			{
			    expressInstall: true,
			    version: 9
			}
		);

        //home flash full screen navigation
        $('.homeSplash.swf').flash(
			{
		     	src: '/swf/portal.swf',
			    width: '100%',
		        height: '99%',
			    wmode: 'transparent'
			},
			{
			    expressInstall: true,
			    version: 8
			}
		);

    }
    // end flash inserts	


    //rounded corners
    if ($('.round').length) {

        var roundContent = '';
        var preRound = '<div class="ul"><div class="ur"></div><div class="ll"></div><div class="lr"><div class="content">';
        var postRound = '</div></div></div>';
        $('div.round').each(function() {
            roundContent = $(this).html();
            $(this).addClass("rounded");
            $(this).html(preRound + roundContent + postRound);
        });
    }


	//bread crumb dividers
	if($('.contentNav h1').length){
		$('.contentNav h1').each(function(){
			$(this).html($(this).html().replace(/:/g, '<img class="divider" src="/images/spacer.gif" alt="&raquo;" />')); 	
		});
	}

    //centered links on content nav
    if ($('.contentNav.centered').length) {
        $('.contentNav.centered').each(function() {
            var liWidth = 0;
            var contentNavWidth = $(this).width();
            //add up the li's to get their total width
            $(this).find('ul li').each(function() {
                liWidth = liWidth + $(this).width();
            });
            var contentNavLiMargin = contentNavWidth / 2 - liWidth / 2 + "px";
            $(this).find('li:first').css({
                "margin-left": contentNavLiMargin
            });
        });
    }




    //coming soon tooltip for dummy content
    var $toolTipItems = $("img.dummy, a.dummy, area.dummy");
    if ($toolTipItems.length) {
        $toolTipItems.tooltip({
            bodyHandler: function() {
                return 'coming soon';
            },
            track: true,
            delay: 0,
            showURL: false,
            showBody: " - ",
            fade: 250,
            extraClass: 'comingSoon'
        });
    }

    //cant be floating something thats full width.. can i? this is temp!
    $('.contentBox.grid_12.right').removeClass('right');

    //ie specific tweaks (its late)
    if ($.browser.msie) {
        $('.contentNav.centered ul').css({
            'padding-bottom': '20px'
        }
		);
    }

    //mini-top tab nav
    var inprog = 0;
    $('.mininav span.read').hover(function() {
        //if (inprog == 0) {
        //	inprog = 1;
        $('.mininav span.read').stop().animate({ 'top': '0px' }, 600);
        //}

    }, function() {
        $('.mininav span.read').stop().animate({ 'top': '-192px' }, 20, function() { inprog = 0; });
    });


	//book excerpt page - buy books links toggle panel
	if($('.buyBooksLinks').length && $('.buyBooksToggle').length){
		$('.buyBooksLinks .holder').append('<div class="closeToggle" align="center"><a href="close this">close</a></div>');
		$('.buyBooksLinks').hide();
		$('.buyBooksToggle').click(function(){
			$('.buyBooksLinks').toggle();
			return false;
		});
		$('.buyBooksLinks .closeToggle a').click(function(){ $('.buyBooksLinks').hide(); return false; });
	}
		


	//music play link disable right click
	$('a[href$=.mp3], .PlayIt a, a.ymp-tray-track').bind("contextmenu",function(e){
		return false;
	});



});

