$(document).ready(function() {


    $('.sf-menu').superfish({ delay: 0, speed: 'fast' });



    if ($('#contentsection').attr('id')) {
        $('.content_fm h3').eq(0).addClass('title_fm');
    }

    if ($('.title_fm:eq(0)').html())
        if ($('.title_fm:eq(0)').html().length == 0) {
        $('.title_fm').html($('.content_fm .title_fm').html());
    }
    $('.content_fm .title_fm').remove();
    if ($('.content_fm .demo_btn').attr('class')) {
        $('.content_fm .demo_btn').appendTo($('.title_fm'));
    }
    $('.content_fm h3').show();
    $('img[src$="blink.gif"]').remove();

    //InitFormFields
    InitFormFields();
    InitRequestForm();
    InitSwf();
    //imgsearch
    $('#imgsearch').click(function() { Search(true); });
    $('#txtkeyword').change(function() { Search(false); });

    //



    //worldlst
    $('.top_list_btn').click(function() {
        $('.top_list').fadeOut(300, function() {
            $('.top_world').fadeIn(100);
            $('.search_fm').fadeIn(100);
        });
    });

    $('.top_world').click(function() {
        $('.top_world').hide();
        $('.search_fm').hide();
        $('.top_list').fadeIn(500);

    });
    //font 10750
    $('.main_fm font[size]').removeAttr('color');
    //1st 2nd titles

    if (mTrim($('.title_fm').text()) == mTrim($('.content_fm p:first').text())) {

        $('.content_fm p:first').remove();
    }


    //
    $('.top_btn_www').click(function() {
        $('.top_btns_countries_content').slideToggle(200);
    });
    //
    $('.home_slide br').remove();
    $('.home_slide').cycle({ timeoutFn: function(currSlideElement, nextSlideElement, options, forwardFlag) {
        var index = options.currSlide;
        if (index == 1 || index == 0) {
            if (index == 1) { return 7000; } else { return 3000; }
        }
        return 7000;

    }, speed: 1000, before: function() {
        if (this.src.indexOf('/userfiles/image/slide/logo.png') != -1 || this.src.indexOf('/userfiles/image/slide/logo_wtagline.png') != -1) {
            $('.home_slide').css('background', 'url(/userfiles/image/slide/logo.png) no-repeat');
        } else {
            $('.home_slide').css('background', 'none');
        }
    }
    });

    $('.home_story').hide();
    $('.home_story:eq(0)').show();
    setTimeout('SetAllSameHeight()', SetAllSameHeight_t);

    setTimeout('InitHomeStories()', 1000);

    //color="5F8450"
    $('font[color="5F8450"]').attr('color', '#5F8450');




});


 

function InitHomeStories() {
    var x = $('.hm_stories_pages img').length;
	
	if(x>0){
    var w = 0;
    $('.hm_stories_pages img').each(function(i){
        var wp = 82;
        if( $('.hm_stories_pages img').eq(i).width()>5) {
            wp = $('.hm_stories_pages img').eq(i).width();
        }
        w+=wp;
    });
    var y = parseInt((917.0 - w) / (x - 1.0));
	 
    $('.hm_stories_pages a').css('margin-right', y + 'px');
    $('.hm_stories_pages a:last').css('margin-right',  '0px');
	}
     
    $('.home_stories').cycle({ fx: 'fade', timeout: 7000, speed: 1000, cleartype:  1,
        pager: '.hm_stories_pages',
        pagerAnchorBuilder: function(idx, slide) {
            return '.hm_stories_pages a:eq(' + idx + ')';
        },
        before: function(currSlideElement, nextSlideElement, opts, forwardFlag) {
            //alert($(this).find('img').attr('src'));



        },
        after: function(curr, next, opts) {
            var index = opts.currSlide;
            //alert(index)
            //if (index!=0) {index++}
            //  alert(index + 'a');
            if ($('.hm_stories_pages img').length > index) {

                //   alert(pre);
                var not =   $('.hm_stories_pages img').not(  $('.hm_stories_pages img[src$=_gray.png]'));
                if(not.length>0){
                    not.eq(0).attr('src', not.eq(0).attr('src').replace('.png', '_gray.png').replace('_gray_gray', '_gray'));
                }

                
                $('.hm_stories_pages img').eq(index).attr('src', $('.hm_stories_pages img').eq(index).attr('src').replace('_gray.png', '.png'));
            } 

        }
    });


    $('.home_grid_2_left_slide').cycle({ fx: 'fade', timeout: 3500, speed: 1000, cleartype:  1 });

   
    newslide();
}

var SetAllSameHeight_t = 500;

function SetAllSameHeight() {
    SetSameHeight('.sameheight');
    SetAllSameHeight_t = SetAllSameHeight_t * 2;
    setTimeout('SetAllSameHeight()', SetAllSameHeight_t);
}
function SetSameHeight(id) {
    if ($(id).length < 2) {
        return;
    }
    //sameheight
    var sameheight = 0;
    $(id).each(function(i) {
        if ($(id).eq(i).height() > sameheight) sameheight = $(id).eq(i).height();
    });
    if (sameheight > 0) {
        $(id).each(function(i) {
            $(id).eq(i).height(sameheight);
        });
    }
}

function mTrim(str) {
    return $.trim(str).replace('\n', ' ').replace(/  /g, ' ');
}
function InitSwf() {
    $('.swf').each(function(i) {
        var pars = $.trim($(this).html()).split(',');
        if (pars.length > 2) {
            $(this).html('<embed src="' + pars[0] + '" height="' + pars[2] + '" width="' + pars[1] + '" flashvars="" quality="best" wmode="transparent" name="link" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" align="middle"></embed>');
        }
    });
    $('.swf').show();
}
function newslide() {
    try {
        $('.newslide').cycle({cleartype:  1, timeout: 7000});
    } catch (err) { }
}

function Search(foc) {
    if ($('#txtkeyword').val().length > 0) {
        location.href = '/search.asp?keyword=' + $('#txtkeyword').val();
    } else {
        if (foc) {
            $('#txtkeyword').focus();
        }
    }
}

function InitRequestForm() {
    $('.validate').validate();
}



function InitFormFields() {
    if ($('.autofields').html()) {
        if (!$('.autofields input[name=_fieldOrder]').attr('type')) {
            var fields = ',';
            //alert($('.autofields :input').length);
            $('.autofields :input').each(function(i) {
                var fname = $('.autofields :input').eq(i).attr('name');
                if (fname) {
                    if (fname.substr(0, 1) != '_' && fields.indexOf(',' + fname + ',') == -1 && (',form_redirect,submit,submitbutton,FORM_ID,NEXT_PAGE,').indexOf(',' + fname + ',') == -1) {
                        fields = fields + fname + ',';
                    }
                }
            });
            fields = fields.substr(1);
            if (fields.substr(fields.length - 1) == ',') {
                fields = fields.substr(0, fields.length - 1);
            }

            $('.autofields').append('<input type="hidden" name="_fieldOrder" value="' + fields + '" /> ');
        }
    }
}


function swf_include(swfUrl, swfWidth, swfHeight, bgColor, swfName, access, flashVars) {

    var flashStr =
	"<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='" + swfWidth + "' height='" + swfHeight + "' id='" + swfName + "' align='middle' />" +
	"<param name='allowScriptAccess' value='" + access + "' />" +
	"<param name='movie' value='" + swfUrl + "' />" +
	"<param name='FlashVars' value='" + flashVars + "' />" +
	"<param name='loop' value='false' />" +
	"<param name='menu' value='true' />" +
    "<param name='wmode' value='transparent' />" +
	"<param name='quality' value='high' />" +
    "<param name='scale' value='noscale' />" +
	"<param name='bgcolor' value='" + bgColor + "' />" +
	"<embed src='" + swfUrl + "' FlashVars='" + flashVars + "'  quality='best' wmode='transparent'  width='" + swfWidth + "' height='" + swfHeight + "' name='" + swfName + "' align='middle' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />" +
	"</object>";


    document.write(flashStr);
};
