﻿/// <reference path="ref/jquery-1.3.2.js" />
$(document).ready
       (function() {

           if ($.fn.media) { //only do this if the media plugin is loaded

               //set up the media plugin defaults
               $.fn.media.defaults.expressInstaller = SiteSettings.swfobjectExpressInstallUrl;
               $.fn.media.defaults.flashVersion = '9';
               $.fn.media.defaults.autoplay = true;

               //$("#member-home-promo img[src$='swf']").media({ width: 680, height: 90, type: 'swf', bgColor: '#ffffff', allowScriptAccess: 'always' });

               $("#member-home-promo img[src$='swf']").media(function(el, options) {
               options.width = 680;
               options.height = 90;
               options.type = 'swf';
               options.params = { allowScriptAccess: 'always', wmode: 'transparent' };
               });

               $('#current-avatar .avatar').media(function(el, options) {
                   options.width = 200;
                   options.height = 290;
                   options.type = 'swf';
                   options.src = SiteSettings.avatarViewerUrl;
                   options.params = { allowScriptAccess: 'always', wmode: 'transparent' };
                   options.flashvars = { swfPath: $(el).attr('href') };
               });

               $('.avatar-list-item .avatar').media(function(el, options) {
                   options.width = 97;
                   options.height = 140;
                   options.type = 'swf';
                   options.src = SiteSettings.avatarViewerUrl;
                   options.params = { allowScriptAccess: 'always', wmode: 'transparent' };
                   options.flashvars = { swfPath: $(el).attr('href'), myLink: $(el).siblings('a').attr('href') };
               });

               $('#current-avatars li a').media(function(el, options) {
                   options.width = 90;
                   options.height = 130;
                   options.type = 'swf';
                   options.src = SiteSettings.avatarViewerUrl;
                   options.params = { allowScriptAccess: 'always', wmode: 'transparent' };
                   options.flashvars = { swfPath: $(el).attr('href'), myLink: $(el).siblings('a').attr('href') };
               });

               $('.banner728x90').media({ width: 720, height: 90, type: 'swf', bgColor: '#ffffff' });
               $('#home_promo').media({ width: 680, height: 400, type: 'swf', bgColor: '#ffffff', quality: 'medium', params : { allowScriptAccess: 'always', wmode: 'transparent' } },
                    function(el, opt) { /*$.log(el); $.log(opt);*/ },
                    function(el, newdiv, opt, playerName) {
                        $('#home_content h3').hide();
                        if ($(newdiv).children().length > 0) {
                            $('#home_content').hide();
                        } else {
                            $('#home_content .notice').show();
                        }
                        $.log($(newdiv).children().length);
                    }
                );
               //$('#home_promo div').hide();
               //$('#home_content').hide();

               $('.banner300x250').media({ width: 300, height: 250, type: 'swf', bgColor: '#ffffff' });

               $("#sample_content a[href$='swf']").media({ width: 96, height: 118, type: 'swf', bgColor: '#ffffff' });


//               $('#news_media').media(function(el, options) {
//                   options.width = 300;
//                   options.height = 238;
//                   options.type = 'swf';
//                   /*options.src = $(el).attr('href');*/
//                   options.params = { allowScriptAccess: 'always', wmode: 'transparent' };
//                   options.bgColor = '#CDCD9B';
//                   options.flashvars = { wallpaperLink: $(el).attr('link') };
//               });

               $('#free_content_links').media(function(el, options) {

                   var emoLink = $('#free_emoticon');
                   var ddpicLink = $('#free_display_picture');

                   emoLink.hide();
                   ddpicLink.hide();

                   options.width = 300;
                   options.height = 50;
                   options.type = 'swf';
                   options.src = $(el).attr('href');
                   options.params = { allowScriptAccess: 'always' };
                   options.bgColor = '#F1F3CD';
                   options.flashvars = { ddpLink: ddpicLink.attr('href'), emoLink: emoLink.attr('href') };
               });



               $('.member-card .avatar').media(function(el, options) {
                   options.width = 120;
                   options.height = 170;
                   options.type = 'swf';
                   options.src = SiteSettings.avatarViewerUrl;  //$(el).attr('href');
                   options.params = { quality: 'medium', allowScriptAccess: 'always', wmode: 'transparent' };
                   options.flashvars = { swfPath: $(el).attr('href'), myLink: $(el).siblings('a').attr('href') };
                   options.bgColor = '#ffffff';
               });
               $('#member-home .member-primary-avatar .avatar').media(function(el, options) {
                   options.width = 200;
                   options.height = 290;
                   options.type = 'swf';
                   options.src = SiteSettings.avatarViewerUrl;
                   options.params = { quality: 'high', allowScriptAccess: 'always', wmode: 'transparent' };
                   options.flashvars = { swfPath: $(el).attr('href'), myLink: $(el).siblings('a').attr('href') };
               });
               $('#member-avatars li a.avatar').media(function(el, options) {
                   options.width = 90;
                   options.height = 140;
                   options.type = 'swf';
                   options.src = SiteSettings.avatarViewerUrl;
                   options.params = { quality: 'high', allowScriptAccess: 'always', wmode: 'transparent' };
                   options.flashvars = { swfPath: $(el).attr('href'), myLink: $(el).siblings('a').attr('href') };
                   options.bgColor = '#D6E1D0';
               });
           }

           //           if ($(document).pngFix) {
           //               $(document).pngFix();
           //           }

           $("#btnMctDownload").click(function(e) {
               $("#download-message").show();
               $(this).hide();
               return true;
           });

           $(".help_link").click(function() {
               $("#help").toggle();
           });

           $("#help .help_close").click(function() {
               $("#help").toggle();
           });

           if (!$.browser.msie) {
               $('#msninstall_ieOnly').addClass('error');
           } else {
               $('#msninstall_ieOnly').hide();
           }

       });


/******** COMMON STUFF  *******/
//logging function
// usage $.log('sds')
(function($) {
    $.extend({
        log: function(msg) {
            var name;
            if (typeof console != 'undefined') {
                if (typeof msg !== 'string') {
                    for (name in msg) {
                        if(msg.hasOwnProperty(name)){
                            console.log("%s: %s: %o", name, msg[name], this);
                        }
                    }
                } else {
                    console.log("%s: %o", msg, this);
                }
            }
        }
    });
})(jQuery);






/** BUILDER Functions **/

//Builder calls this when user clicks 'done'
function OnBuilderCreateClicked(AvatarXML) {
    try {
        ShowName(AvatarXML);
        $.log(unescape(AvatarXML));
    }
    catch (e) {
        $.log(e);
    }
}


function UpdateViewer(AvatarXml) {

    $('#avatar-viewer').empty().media(function(el, options) {
        options.width = 175;
        options.height = 255;
        options.type = 'swf';
        options.bgColor = '#ffffff';
        options.flashvars = { AvatarXmlData: AvatarXml };
        options.params = { base: '.', allowScriptAccess: 'always' };
        options.src = BuilderSettings.AvatarViewer
    });
}


(function($) {
    $.fn.emptySelect = function() {
        return this.each(function() {
            if (this.tagName == 'SELECT') this.options.length = 0;
        });
    }

    $.fn.loadSelect = function(optionsDataArray) {
        return this.emptySelect().each(function() {
            if (this.tagName == 'SELECT') {
                var selectElement = this;
                $.each(optionsDataArray, function(index, optionData) {
                    var option = new Option(optionData.caption,
                                  optionData.value);
                    if ($.browser.msie) {
                        selectElement.add(option);
                    }
                    else {
                        selectElement.add(option, null);
                    }
                });
            }
        });
    }
})(jQuery);


///Region code


function adjustRegionDropDown() {
    var countryValue = $("#Country").val();
    var regionDropDown = $("#Region");

    if (countryValue.length == 0) {
        regionDropDown.attr("disabled", true);
        regionDropDown.parent().hide();
        regionDropDown.emptySelect();
        $.log("countryValue.length == 0");
    }
    else {
        $.getJSON(
                '/regions/index.aspx',
                { countryid: countryValue },
                function(data) {
                    if (data.HasRegions) {
                        regionDropDown.loadSelect(data.Regions);
                        regionDropDown.parent().show();
                        regionDropDown.attr("disabled", false);
                        regionDropDown.focus();
                    }
                    else {
                        regionDropDown.attr("disabled", true);
                        regionDropDown.parent().hide();
                    }
                }
             );
    }
}






    