/* custom  global nav nav */
  // reloads page with new URL (to be used in support of Rails I18n functionality)
   function set_language(local_pref, available_locales) {
       
         var current_url = window.location.href
       
       var new_url = swap_locale(current_url, local_pref, available_locales);
       window.parent.location.replace(new_url);
   };
    // returns a new string replacing any discovered locales with the requested one
    function swap_locale(url, locale_pref, available_locales) {
        var url_path = url.split("/");
        var available_locales = available_locales.split(",");
        var language_found = false;

        // for each supported locale detect if its present in the URI
        jQuery.each(url_path, function(index,value) {
            // if the locale is discovered swap it out
            if($.inArray(value, available_locales) >= 0){
                url_path[index] = locale_pref;
                language_found = true;
            }
        });
        // rewrite the new URL using new locale or add locale if none was discovered
        if( language_found == true) {
            return url_path.join("/");
        } else {
            url_path[2] = url_path[2].concat("/", locale_pref);
        };
        return url_path.join("/");
    };

// ============================================== //
// slideanim.js
// Written by Sampath Lakshmanan
// Trion Worlds
// ============================================== //



//
// If any of the increment doesn't work, then need to check whether the variable 
// is still a string if so need to be converted to number using "parseInt"
//

var curr_el = '';
var allThumb = new Array();
var frame = '';
var curr_frame = '';
var inview_frame;
var curr_img = '';
var curr_img_num = '';
var container_width = 220;
var loop = false; // Loop option are 'true' or 'false'

// DOM onload

 $(document).ready(function() {
   
   //
   // Initialize
   //
   
   // stop the default 
   $('.thumb-sct a').click(function(e) { e.preventDefault(); });
   $('#scrn-controls a').click(function(e) { e.preventDefault(); });
   
   // iterate the thumbnail array
   $('.thumb-sct a').each(function(index) {
   // var index = index+1; // this works with the highlight & scroll, but it gives wrong array count
    allThumb[index] = $(this);
    $(this).click(function(e) {
        show_image(index); // set the corresponding index number (img num) to the show_image funct
    });
   }); // each
   
    img_count = parseInt(allThumb.length);
    
    // pagination
    var num_of_container = $('.thumb-sct').length;
    if(num_of_container > 1) {
        for (var j=0; j < num_of_container; j++) {
            var curr_j = j;
                $('#pagination').append('<a href="#" onclick="scroll_frame(this,'+curr_j+')">'+(j+1)+'</a>');   
        }
    }
    $('#pagination a').click(function(e) { e.preventDefault(); });  // stop the default
    $('#news-arch-accordion a').click(function(e) { e.preventDefault(); }); // stop the default
    
    // thumb-container width
    $('#thumb-container').css('width',container_width*num_of_container);
    
    //
    // Onload
    //
    /**************  show the IMG according to the URL # value  ************/       
    call_onload_bind();
    
    
}); // DOM

 // this is called when Browser BACK & FORWARD arrows are clicked
 $(window).bind('hashchange', function () { 
    /**************  show the IMG according to the URL # value  ************/   
    call_onload_bind();
});
 
 
 var call_onload_bind = function() {
    var curr_url = window.location.href+'';
    var hash_position = parseInt(curr_url.indexOf('#'));
    var show_img = 0; // show the first img as default

    // first check for #
    if(hash_position > 0) {
        // there s a hash in URL, now check whether there s anything after #
        if(curr_url.length == hash_position+1) {
            show_img = 0; // nothing after # so show first img
        } else {
            show_img = curr_url.substr(curr_url.indexOf('#')+1, curr_url.length); // crop the img value
            if(show_img >= img_count || show_img < 0) {
                // out of array number, possible user entry default to first img also check for negative num
                show_img = 0;
            }
        }
    } else {
        // no hash
        show_img = 0;
    }
    show_image(show_img);   
 };

    $('#scrn-previous').mouseenter(function(e){ 
        if(loop){
            $(this).addClass('previous-rollover');
        } else {
            if(parseInt(whats_in_view()) == 0) {
                // $(this).removeClass('previous-rollover');
            } else {
                $(this).addClass('previous-rollover');
            }       
        }
    });
    $('#scrn-previous').mouseout(function(e){ 
        // $(this).css('background',"url([system-asset]/img/leftArrowStatic.png[/system-asset]) no-repeat");
        $(this).removeClass('previous-rollover');
    });
    
    
    $('#scrn-next').mouseenter(function(e){ 
    
        if(loop) {
            $(this).addClass('next-rollover');
        } else {
            if(parseInt(whats_in_view()) == parseInt(img_count-1)) {
                // $(this).removeClass('next-rollover');
            } else {
                $(this).addClass('next-rollover');
            }       
        }
    });
    $('#scrn-next').mouseout(function(e){ 
        $(this).removeClass('next-rollover');
    }); 
    
    // wallpapers mouseover
    $('#size-list-area, #scrn-img').mouseover(function(e){
        $('#size-list-area').addClass('res-show');
    });
    $('#size-list-area, #scrn-img').mouseout(function(e){
        $('#size-list-area').removeClass('res-show');
    });

    $('#size-list-area').find('*').mouseover(function(e){
        $('#size-list-area').addClass('res-show');
    });
    $('#size-list-area').find('*').mouseout(function(e){
        $('#size-list-area').removeClass('res-show');
    });
    
    
 
// 
// COMMON funct
// 
var show_image = function(img_num) {
    // alert ('img_num:'+img_num+' allThumb len:'+allThumb.length);
    if((img_num < allThumb.length) && img_num >= 0) {
        for ( var i in allThumb ) {
            var el = allThumb[i];
            el.removeClass('in-view'); // reset all
        }
        var curr_img_num = parseInt(img_num);
        // alert ('incr:'+incr+' i:'+i);
        var curr_el = allThumb[curr_img_num];
        var curr_frame = Math.ceil(parseInt(curr_img_num+1)/8)-1;
        // alert (curr_frame);

        // check frame
        // alert ('inview_frame:'+ inview_frame +' curr_frame:'+curr_frame);    
        if(inview_frame!=curr_frame) {
            inview_frame = curr_frame;
            scroll_frame(this,curr_frame);
        }
        
        // hightlight the current one
        curr_el.addClass('in-view');
        curr_img = $(curr_el).attr('href');
        
        
        $('#scrn-img img').fadeOut(); // hide the existing image
        $('#scrn-img').addClass('loading');
        
        var img = new Image();
        
        $(img).load(function () {       // once the image has loaded, execute this code
          $(this).hide();   // set the image hidden by default    
          $('#scrn-img').removeClass('loading');          
          $('#scrn-img').empty().append(this);  // then insert our image
          $(this).fadeIn(); // fade our image in to create a nice effect
        })    
        .error(function () {
          // notify the user that the image could not be loaded
        }).attr('src', curr_img);   // *finally*, set the src attribute of the new image to our image
        
        // change the full size img link
        var full_name = curr_el.attr('rel');
        var fldr_name = full_name.substring(0,full_name.indexOf('-'));
        var sys_link = curr_el.attr('href');
        sys_link = sys_link.substring(0, sys_link.lastIndexOf('/')+1); // crop the img value        
        
        if($('#size-list-area').length) { // if true means the code is called from Wallpapers pg
            $('#size1024x768').attr('href',sys_link+fldr_name+'-1024x768.jpg');
            // $('#size1152x864').attr('href',sys_link+fldr_name+'-1152x864.jpg');
            // $('#size1280x800').attr('href',sys_link+fldr_name+'-1280x800.jpg');
            $('#size1280x1024').attr('href',sys_link+fldr_name+'-1280x1024.jpg');
            $('#size1280x720').attr('href',sys_link+fldr_name+'-1280x720.jpg');         
            
            // $('#size1440x900').attr('href',sys_link+fldr_name+'-1440x900.jpg');
            $('#size1600x1200').attr('href',sys_link+fldr_name+'-1600x1200.jpg');
            $('#size1680x1050').attr('href',sys_link+fldr_name+'-1680x1050.jpg');
            $('#size1920x1200').attr('href',sys_link+fldr_name+'-1920x1200.jpg');

        } else { // this is called in concept art and screenshots
            // alert (sys_link+full_name);
            $('#full_link').attr('href',sys_link+full_name);
        }
        
        // update URL for sharing purpose
        if(curr_img_num!=0) { // don't rewrite the URL for the first img, if you do it messes up the back button
            curr_url = window.location+'';
            curr_url = curr_url.substr(0,curr_url.indexOf('#'));
            window.location.href=curr_url+"#"+curr_img_num;
        }
            
    } // if img_num < thumb.len
    else {
        // The Img number is out of limits
        // $('#btn_next').attr('disabled','true');
    }

} // show_image

//
// COMMON funct
//
var scroll_frame = function(thisel,val) {
    inview_frame = val;
    var val_negative = -val;
    var container = $("#thumb-container");
    // curr_p_left = parseInt(p.css("left"));
    // var goto_p_left = curr_p_left - 220;
    
    var goto_p_left = parseInt(val_negative) * container_width;
    // alert ('curr left:'+curr_p_left+' goto left:'+goto_p_left);
    $(container).animate({
        left: goto_p_left
  }, 300, function() {
        // Animation complete.
  });
    // hightlight the current frame and reset the rest
    $('#pagination a').each(function(index) {
        // alert (val);
        if(index==val) {
            // $(this).css('color','#959697');
            // $(this).css('cursor','default');
            $(this).addClass('frame-on-view');
        }else {
            // $(this).css('color','#f99c5e');
            // $(this).css('cursor','pointer');
            $(this).removeClass('frame-on-view');           
        }
    }); // pagination a
}; // scroll_frame

var super_next = function() {
    for ( var i in allThumb ){
        var el = allThumb[i];
        var incr = parseInt(i)+1;
        
        // find the thumb which is in-view
        if(el.hasClass('in-view')) {
            var curr_img_num = incr; 
        } // if
    } // for
    
    // loop
    if(loop) {
        if(curr_img_num==allThumb.length){ curr_img_num=0; }
    }

    // call COMMON funct
    show_image(curr_img_num);
}; // super_next

var super_previous = function() {
    for ( var i in allThumb ){
        var el = allThumb[i];
        var decr = parseInt(i)-1;
        
        // find the thumb which is in-view
        if(el.hasClass('in-view')) {
            var curr_img_num = decr; 
        } // if
    } // for

    // loop
    if(loop) {
        if(curr_img_num==-1){ curr_img_num=allThumb.length-1; }
    }
    
    // call COMMON funct
    show_image(curr_img_num);
}; // super_previous



var whats_in_view = function() {
    for ( var i in allThumb ){
        var el = allThumb[i];       
        // find the thumb which is in-view
        if(el.hasClass('in-view')) {
            var img_in_view = parseInt(i); 
        } // if
    } // for
    return img_in_view;
}; 




/************ Thumb ************/
(function($){$.fn.Zoomer=function(b){var c=$.extend({speedView:200,speedRemove:400,altAnim:false,speedTitle:400,debug:false},b);var d=$.extend(c,b);function e(s){if(typeof console!="undefined"&&typeof console.debug!="undefined"){console.log(s)}else{alert(s)}}if(d.speedView==undefined||d.speedRemove==undefined||d.altAnim==undefined||d.speedTitle==undefined){e('speedView: '+d.speedView);e('speedRemove: '+d.speedRemove);e('altAnim: '+d.altAnim);e('speedTitle: '+d.speedTitle);return false}if(d.debug==undefined){e('speedView: '+d.speedView);e('speedRemove: '+d.speedRemove);e('altAnim: '+d.altAnim);e('speedTitle: '+d.speedTitle);return false}if(typeof d.speedView!="undefined"||typeof d.speedRemove!="undefined"||typeof d.altAnim!="undefined"||typeof d.speedTitle!="undefined"){if(d.debug==true){e('speedView: '+d.speedView);e('speedRemove: '+d.speedRemove);e('altAnim: '+d.altAnim);e('speedTitle: '+d.speedTitle)}
$(this).hover(function(){
// RESET
$('ul.thumb li.need-zoomer').css('z-index','0');
$('ul.thumb > li.need-zoomer').find('img').removeClass('hover').css({ 
    'padding':0,'margin':0,'width':'92px','height':'100px','top':0,'left':0
});
// CODE
$(this).css({'z-index':'100'});
$(this).find('img').addClass("hover").stop().animate({
    marginTop:'-110px',marginLeft:'-110px',top:'50%',left:'50%',width:'175px',height:'190px',padding:'2px'
},d.speedView);
if(d.altAnim==true){var a=$(this).find("img").attr("alt");if(a.length!=0){$(this).prepend('<span class="title">'+a+'</span>');$('.title').animate({marginLeft:'-42px',marginTop:'90px'},d.speedTitle).css({'z-index':'10','position':'absolute','float':'left'})}}},function(){$(this).css({'z-index':'0'});$(this).find('img').removeClass("hover").stop().animate({marginTop:'0',marginLeft:'0',marginBottom:'0',marginRight:'0',top:'0',left:'0',width:'92px',height:'100px',padding:'0'},d.speedRemove);$(this).find('.title').remove()})}}})(jQuery);

$(document).ready(function(){
    // $('ul.thumb-hm li').Zoomer({speedView:200,speedRemove:400,altAnim:true,speedTitle:400,debug:false});
    // $('#home-scrnshots .scrn-hm a').Zoomer({speedView:200,speedRemove:400,altAnim:true,speedTitle:400,debug:false});
    $('ul.thumb li.need-zoomer').Zoomer({speedView:200,speedRemove:400,altAnim:false,speedTitle:400,debug:false});
    
});

/*************** Home Anim  *************/

$(document).ready(function() {
    $('#anim-ctrl a').click(function(e) { e.preventDefault(); });   
    // doanim('2').delay(300);
});
var doanim = function(el){
    muteall();
    $('#anim-img-'+el).fadeIn();
    $('#anim-txt-'+el).css('display','block');
};
var muteall = function() {
    $('.anim-img-all').fadeOut(100);
    $('.anim-txt-all').css('display','none');;
}

/*
Maybe someone did not realize this (I did not):
1. .delay() cannot be stopped by using .stop()
2. The only way to cancel a running delay is using .clearQueue(). 
*/


/***** News Archive START *****/

function showMonth(el,showel) {     

    // el.preventDefault();
    if($('#'+el).hasClass('inview')) {
        $('#'+showel).slideUp();
        $('#'+el).removeClass('inview');        
        $('#'+el).find('a').css('backgroundPosition','8px 11px');       
    } else {
        $('#'+showel).slideDown("fast");
        $('#'+el).addClass('inview');                   
        $('#'+el).find('a').css('backgroundPosition','8px -23px');
    }       
}

function shownews(thisel,cls) {
    $('.news-all').css('display','none');
    // $('.'+cls).css('display','block');
    show_filter_frame_arch(cls,0);
    // reset
    $('.arch-month li').css('color','#b0c5dd');
    $('.arch-month li a').css('color','#b0c5dd');
    $('#news-tabs a').removeClass('highlt');
    $('#news-dflt').addClass('highlt');
    // highlight
    $(thisel).css('color','#3da5fe');
    $(thisel).find('a').css('color','#3da5fe');
    
    var n = $('.'+cls).length;  
    numof_newsframes = Math.ceil(n/news_per_pg);
    // alert ('frames'+numof_newsframes+'news per pg'+news_per_pg);
    $('#news-pgnt').html('');
    if(numof_newsframes > 1){
        for(i=0; i < numof_newsframes; i++) {           
            if(i==0) {
                $('#news-pgnt').append('<a href="#" class="frameon" id="news-dflt-frm" onclick="show_filter_frame(this,\''+cls+'\','+i+')">'+(i+1)+'</a>');
            }else {
                $('#news-pgnt').append('<a href="#" onclick="show_filter_frame(this,\''+cls+'\','+i+')">'+(i+1)+'</a>');
            }
        }
    }   
}


// NEWS Pagination

// var number of news items per page
var news_per_pg = 5;

function show_filter(thisel,el) {
    // alert(el+" : "+$('.'+el).length);
    $('.news-all').css('display','none');
    
    $('.arch-month li').css('color','#b0c5dd');
    $('.arch-month li a').css('color','#b0c5dd');
    
    // $('.'+el).css('display','block');
    show_filter_frame(thisel,el,0);
    $('#news-tabs a').removeClass('highlt');
    $('#news-tabs a').removeClass('frameon');
    $(thisel).addClass('highlt');
    var n = $('.'+el).length;   
    numof_newsframes = Math.ceil(n/news_per_pg);
    
    $('#news-pgnt').html('');
    if(numof_newsframes > 1){
        for(i=0; i < numof_newsframes; i++) {           
            if(i==0) {
                $('#news-pgnt').append('<a href="#" class="frameon" id="news-dflt-frm" onclick="show_filter_frame(this,\''+el+'\','+i+')">'+(i+1)+'</a>');
            }else {
                $('#news-pgnt').append('<a href="#" onclick="show_filter_frame(this,\''+el+'\','+i+')">'+(i+1)+'</a>');
            }
        }
    }
}
function show_filter_frame(thisel,el,frame_num) {
    minitem = frame_num*news_per_pg;
    if(frame_num==0){ maxitem = news_per_pg-1; } else{ maxitem = (minitem+(news_per_pg-1)); }
    // alert ('min:'+minitem+'max:'+maxitem);
    $('#news-pgnt a').removeClass('frameon');
    $(thisel).addClass('frameon');
    $('.news-all').css('display','none');
    $('.'+el).each(function(index,domEl) {
        if(index >= minitem && index <= maxitem){
            $(this).css('display','block'); 
        }
    });
}
function show_filter_onload(thisel,el) {
    $('.news-all').css('display','block');
    // $(thisel).addClass('highlt');
    var n = $('.'+el).length;
    numof_newsframes = Math.ceil(n/news_per_pg);
    
    $('#news-pgnt').html('');
    if(numof_newsframes > 1){
        for(i=0; i < numof_newsframes; i++) {           
            if(i==0) {
                $('#news-pgnt').append('<a href="#" class="frameon" id="news-dflt-frm" onclick="show_filter_frame(this,\''+el+'\','+i+')">'+(i+1)+'</a>');
            }else {
                $('#news-pgnt').append('<a href="#" onclick="show_filter_frame(this,\''+el+'\','+i+')">'+(i+1)+'</a>');
            }
        }
    }
}
function show_filter_frame_onload(el,frame_num) {
    minitem = frame_num*news_per_pg;
    if(frame_num==0){ maxitem = news_per_pg-1; } else{ maxitem = (minitem+(news_per_pg-1)); }
    $('.news-all').css('display','none');
    $('.'+el).each(function(index,domEl) {
        if(index >= minitem && index <= maxitem){
            $(this).css('display','block'); 
        }
    });
}
function show_filter_frame_arch(el,frame_num) {
    minitem = frame_num*news_per_pg;
    if(frame_num==0){ maxitem = news_per_pg-1; } else{ maxitem = (minitem+(news_per_pg-1)); }
    // alert ('min:'+minitem+'max:'+maxitem);
    $('#news-pgnt a').removeClass('frameon');
    // $(thisel).addClass('frameon');
    $('.news-all').css('display','none');
    $('.'+el).each(function(index,domEl) {
        if(index >= minitem && index <= maxitem){
            $(this).css('display','block'); 
        }
    });
}

/***/
/* moving the document.ready code to this block, to avoid constraining the */
/* height of the news wrapper div, which is now used in the awards pg too */
/* /_internal/blocks/media/js/news-landing-onload */
/***/

/*
$(document).ready(function(){
    var ht = (158*news_per_pg)+37+43; // 37-tab 43-pagination
    var ht_px = ht+'px';    
    $('#news-list').css('height',ht_px); // its 827 if we r showing 5 news itsm
    $('#news-list').css('overflow','hidden');
    var this_news_dflt = $('#news-dflt');
    var this_news_frm = $('#news-dflt-frm');
    show_filter_onload(this_news_dflt,'news-all');
    show_filter_frame_onload('news-all',0);
    $('#news-tabs a').click(function(e) { e.preventDefault(); });   // stop the default 
    $('#news-pgnt a').click(function(e) { e.preventDefault(); });   // stop the default                         
    
    // BETA section news exception
    $('.beta-news #news-list').css('height','auto'); 
    $('.beta-news #news-list').css('overflow','visible');
    $('.beta-news .news-all').css('display','block'); 
});
*/

/*********************************/
/*********  Products Pg  *********/
/*********************************/
/* onload from Video-JS: ./LANG/products-onload-vide */
$(document).ready(function() {
    $('.prod-media-thumb a').click(function(e) { e.preventDefault(); });
    $('.prod-media-video a').click(function(e) { e.preventDefault(); });
    
    // Image
    $('.prod-media-thumb a').click(function() {
        $('#scrn-prod-img img').css('display','block');
        swfobject.removeSWF('prod-video');
        $('#scrn-prod-img').html('<div id="prod-video"></div><img src="/img/blank.gif" />');
        $('#prod-video').css('display','none');
        $('#scrn-prod-img img').attr('src',$(this).attr("rel"));
    }); // click
    
    // Video
    $('.prod-media-video a').click(function() {
        $('#scrn-prod-img img').css('display','none');
        $('#prod-video').css('display','block');
        // products_video('en');
    }); // click
    
    
}); // DOM

function products_video(lang_var,auto_start) {
    // auto_start either true or false
    // lang_var en, fr or de
    
    if(lang_var=='fr'){
        var video_src = 'http://assets.triongames.com/rift/video/Features/FR/RIFT_Features_V061_FR_504.flv';     
    } else if(lang_var=='de'){
        var video_src = 'http://assets.triongames.com/rift/video/Features/DE/RIFT_Features_V061_DE_504.flv';
    } else {
        var video_src = 'http://assets.triongames.com/rift/video/Features/EN/RIFT_Features_V061_EN_504.flv';
    }

        $('#prod-video').css('display','block');
        $('#scrn-prod-img img').css('display','none');
        flashvars = { themeColor: '336699', mode: 'overlay', scaleMode: 'fit', frameColor: '000000', fontColor: 'b1b1b1', background: '000000', link: '',embed: ''};
        params = {allowFullScreen: 'true',wmode: 'opaque',mute: 'true'};
        attributes = {id: 'prod-video' ,name: 'prod-video'};
        flashvars.src = video_src;
        flashvars.autostart = auto_start;
        flashvars.loadImage = "/img/products/media/video-features.jpg";
        
        swfobject.embedSWF('/swf/AkamaiFlashPlayer.swf','prod-video','504', '284', '9.0.0', '/swf/expressInstall.swf', flashvars, params, attributes);  
}

