var modded = false;
var xp_new_window_class = "new-window";
var wWith = "760";
var wHeight = "480";
var windowName = 'aWindow';
var windowOptions = 'resizable=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes';
var pos = 0;
var height = 0;

$(document).ready(function(){



    try {
        $('table.press-review a').tooltip({
            delay: 0,
            showURL: false,
            bodyHandler: function(){
                var src = $(this).attr('rel');
                if (src != null && src != '' && src != undefined && src.length > 0) 
                    return $("<img/>").attr("src", src);
                return;
            }
        });
    } 
    catch (e) {
       /* alert(e); */
    }
    
    
    $('body.ViewNews div.news-section div.content').hide();
    $('body.viewTable div.xt-content-wrapper').hide();
    $.expr[":"].containsNoCase = function(el, i, m){
        var search = m[3];
        if (!search) 
            return false;
        var reg = eval("/" + search + "/i");
        return reg.test($(el).text());
    };
    
    
    $('#accordion table tbody td.data, #accordion table tbody td.icon').hover(function(e){
        var col = $(this).parent().children().index($(this));
        //var row = $(this).parent().parent().children().index($(this).parent());
        var title = $(this).parent().parent().parent().find('tr.product-name th').eq(parseInt(col) - 1);
        title = title.find('a');
        var div = $("<div class=\"tooltip\" style=\"position:absolute; top:" + ($(this).position().top - 20) + "px; left:" + ($(this).position().left) + "px;\">" + title.text() + "</div>").appendTo(this);
        //$(this).append(div);
    
        //alert(title.text());
    }, function(e){
        $(this).find('div.tooltip').remove();
    });
    
    
    
    var url = window.location.toString();
    var queryString = url.replace(/^[^\?]+\??/, '');
    var params = xp_parseQuery(queryString);
    var highlightedWord = params['highlightedWord'];
    if (highlightedWord != undefined && highlightedWord != null) {
        var reg = eval("/" + highlightedWord + "/i");
        var h = $("#content td div.xt-content-wrapper").html().replace(reg, '<span class="highlight">' + highlightedWord.toUpperCase() + '</span>');
        $("#content td div.xt-content-wrapper").html(h);
        /*
        
         $("div.xt-content-wrapper:containsNoCase("+highlightedWord+")").each(function(){
        
         var h = $(this).html().replace(highlightedWord, '<span class="highlight">'+highlightedWord+'</span>');
        
         $(this).html(h);
        
         });
        
         */
        
    }
    
    $('#left-cycle .xt-multimediacontents-wrapper').hide();
    $('#bookmark').show();
    $('#bookmark').css({
        width: '231',
        height: '180'
    });
    $('#line-right-content-bookmark').css({
        width: '231',
        height: '223'
    });
    $('#endorsers-play').hide();
    $('#line-right-content').hide();
    
    $('.toValidate').validate();
    $('table.easy-reading tbody tr:even').addClass('even');
    
    
    $('a.new-window').click(function(){
        var url = $(this).attr('href');
        if (url) 
            newWindowWithURL(url);
        return false;
    });
    
    $('a.internal-call').click(function(){
        opener.location = this.href;
        opener.focus();
    });
    $('.news-headline').click(function(){
        var link = $(this).find('a:first');
        window.location = link.attr('href');
    });
    
    
    $('a.disabled-link').click(function(){
        return false;
    });
    
    
    
    $("ul.sf-menu").superfish({
        delay: 800,
        animation: {
            opacity: 'show',
            height: 'show'
        },
        speed: 'fast',
        autoArrows: false,
        dropShadows: false
    });
});

var xURL = '/Apps/WebObjects/Xtremeaudio.woa/wa/viewMultimediaContent?name=x';

function stopCycle(){
    //$('.cycle').hide();
    $('.cycle-placeholder').show();
    //$('#video-player .play').hide();
   	$('#video-player .start').bind('click',function(){ return startCycle(); });
	$('#video-player .stop').bind('click',function(){ return false; });
    $('#video-player .stop').hide();
    $('.vp .cycle').hide();
	return false;
}

function startCycle(){
    //$('#video-player .play').toggle();
    $('#video-player .start').bind('click',function(){ return false; });
   	$('#video-player .stop').bind('click',function(){ return stopCycle(); });
    
	$('#video-player .stop').show();
    $('.cycle').fadeIn();
    $('.cycle').cycle({
        speed: 2000,
        timeout: 4000,
        delay: -2000,
        autostop: 1,
        end: function(){
            /*$('.cycle-placeholder').show();
            $('#video-player .start').toggle();
            $('#video-player .stop').hide();
            $('.vp .cycle').hide();*/
			stopCycle();
        }
    });
	return false;
}

$(window).load(function(){
    $('body.viewTable div.xt-content-wrapper').show();
    
    $('div#accordion').accordion({
        header: 'div.xt-title-wrapper',
        navigation: true,
        alwaysOpen: false,
        active: false,
        autoheight: false
    });
    
    $('#bookmark').hide();
    
    
    $('#video-player').css('cursor', 'hand');
    $('#left-cycle .xt-multimediacontents-wrapper').fadeIn();
    //  
    
    /**/
     
     /* $('#video-player').click(function(){
     $('#left-cycle .xt-multimediacontents-wrapper').cycle({
     fx: 'fade',
     timeout: 1500,
     speed: 3000
     });
     return false;
     });*/
    $('#video-player .start').click(function(){
        $('.cycle-placeholder').fadeOut(500, startCycle);
        return false;
    });
    
    /*$('#video-player .play').click(function(){
        $('.cycle').cycle('resume', true);
        return false;
    });*/
    /*$('#video-player .stop').click(function(){
        $('.cycle').cycle('pause');
        $('.cycle').fadeOut(500, stopCycle);
        return false;
    });*/
    
    
    
    $('#left-cycle2 .xt-multimediacontents-wrapper').cycle({
        fx: 'fade',
        timeout: 500,
        speed: 1000
    });
    
    
    
    $('body.ViewNews div.news-section div.content').show();
    $('div.news-section').accordion({
        header: 'div.title',
        navigation: true,
        alwaysOpen: false,
        active: false,
        autoheight: false
    });
    
    
    $('#NEWS .line-1-menu ul a').each(function(i){
        $(this).click(function(){
            var id = "#the-news-menu div.div-level-3:eq(" + i + ")";
            var q = "p.p-level-2:eq(" + i + ")";
            if ($(q).hasClass('selected')) {
                $(q).removeClass('selected');
                $(id).slideUp();
            }
            else {
                $(q).addClass('selected');
                $(id).slideDown();
            }
            return false;
        });
    });
    
    
    
    $('#endorsers-placeholder').hide();
    $('#endorsers-play').show();
    $('#line-right-content-bookmark').hide();
    $('#line-right-content').show();
    
    var h = $('#product-text').height();
    var newh = h + 223;
    var h2 = 500;
    var h3 = 278;
    /* if (h < 278) {
     //alert ($('#product-text').height());
     $('#left-cycle-padding').css('height', h2);
     $('#product-text').css('height', h3);
     //alert ($('#product-text').height());
     }
     else {
     //alert ($('#product-text').height());
     $('#left-cycle-padding').css('height', newh);
     $('#product-text').css('height',h);
     }*/
    function replaceThisWithX(selector){
    
    }
    
    //alert ($('#left-cycle-padding').height());
});





function newWindowWithURL(anHref){
    var queryString = anHref.replace(/^[^\?]+\??/, '');
    var params = xp_parseQuery(queryString);
    if (params['width'] != null && params['width'] != '') 
        wWith = params['width'];
    if (params['height'] != null && params['height'] != '') 
        wHeight = params['height'];
    if (anHref != null && anHref != '' && anHref != '#') {
        //$('input, textarea, select').attr('disabled','true');
        var newWindow = window.open(anHref, windowName, windowOptions + ',width=' + wWith + ',height=' + wHeight);
        newWindow.focus();
    }
}

function xp_parseQuery(query){
    var Params = {};
    if (!query) {
        return Params;
    }// return empty object
    var Pairs = query.split(/[;&]/);
    for (var i = 0; i < Pairs.length; i++) {
        var KeyVal = Pairs[i].split('=');
        if (!KeyVal || KeyVal.length != 2) {
            continue;
        }
        var key = unescape(KeyVal[0]);
        var val = unescape(KeyVal[1]);
        val = val.replace(/\+/g, ' ');
        Params[key] = val;
    }
    return Params;
}

function flashButtonToRed(){
    $('#comparison a').animate({
        color: 'rgb(129,27,33)'
    }, 500);
    /*$('#comparison a').animate({opacity:'0'},500);*/
    setTimeout('flashButtonToWhite()', 750);
}

function flashButtonToWhite(){
    $('#comparison a').animate({
        color: '#fff'
    }, 500);
/*$('#comparison a').animate({opacity:'1'},500);*/
}
