﻿var $j = jQuery.noConflict();
/*#########################################################################################
Name: FixNewsHeader
Description: JQuery Plugin to make sure news heading on listing remains on the same
             line based on the height specified.
#########################################################################################*/
(function($){
    $.fn.FixNewsHeader = function(options){
        var o = $.extend({},$.fn.FixNewsHeader.defaults,options);
        
        return this.each(function(i){
            if($(this).height() > o.height){
                var t,a,s,r;
                t = $.trim($(o.subjectCss,this).text());
                a = t.split(' ');
                s = a[a.length-1];
                r = s;
                r = r.substring(0,r.length-3) + '..'
                t = t.replace(s,r);
                $(o.subjectCss,this).hide().text(t).fadeIn();
            }
        });
    }
    $.fn.FixNewsHeader.defaults = {subjectCss:'a.GreenBold',height:15}
    $.fn.FixNewsHeader.TrimOne = function(w){}
})(jQuery);
/*#########################################################################################
Name: SimlpeClickTrhough
Description: JQuery Plugin to register Tab Clickthroughs
#########################################################################################*/
(function($){
    $.fn.SimpleClickThrough = function(options){
        var o = $.extend({},$.fn.SimpleClickThrough.defaults, options);
        
        return this.each(function(idx){
            $(this).click(function(){
                var tabName = ''                
                if($('.tab-arg',this).length > 0){
                    tabName = $('.tab-arg',this).text();
                }else{                
                    tabName= $(this).text();
                }
                
                if($.trim(tabName) != ''){
                    $.getJSON(o.webservice,{a:o.action,p:o.pageName,t:tabName});
                }                
            });
        });
    }
    
    $.fn.SimpleClickThrough.defaults = {webservice: '/genericasync.ashx', action: 'click', pageName: 'Home'}
})(jQuery);

/*#########################################################################################
Name: Tabify
Description: JQuery Plugin Switch tabs on the home page
#########################################################################################*/
(function($){
    $.fn.Tabify = function(o){
        o = $.extend({},{startCount:0},o);        
        var self = this;
        return this.each(function(){
            var count = o.startCount;
            var contentPane = $(this).next();
            
            $('li',this).each(function(i){
                if($.trim($('.TabFeatured',this).html()).length == 0){
                    $(this).addClass('Tab_HomePage_Empty');
                }
                $(this).mouseenter(function(){                    
                    if($(this).hasClass('Tab_HomePage')){
                        $(this).removeClass('Tab_HomePage')
                            .addClass('Mouseover_Tab_Home');
                    }
                }).mouseleave(function(){
                    if($(this).hasClass('Mouseover_Tab_Home')){
                        $(this).removeClass('Mouseover_Tab_Home')
                            .addClass('Tab_HomePage');
                    }
                }).click(function(){
                    if(!$(this).hasClass('Tab_HomePage_Empty')){
                        getTabData(contentPane, i, count + i, function(){
                            doSelectTab(self, contentPane, i);
                        });
                    }
                });                
            });
        });
    }
})(jQuery);

/*#########################################################################################
Name: TabifySub
Description: JQuery Plugin to switch tabs on sub pages
#########################################################################################*/
(function($){
    $.fn.TabifySub = function(o){
        o = $.extend({},{
                startCount:0,
                enableajax: false,
                groupId: 0,
                categoryName: '',
                container: 'tab-sub-containers',
                cssnormal: 'Tab_HomePage',
                cssactive: 'Tab_HomePage_Active',
                cssover: 'Mouseover_Tab_Home',
                cssempty: 'Tab_HomePage_Empty'
            },o);        
        var self = this;
        return this.each(function(){
            var count = o.startCount;
            var contentPane = $(this).next();
            
            //Hide all content except the one that is active
            $('.' + o.container,contentPane).hide();
            $('.' + o.container +':eq(0)',contentPane).show();            
            
            $('li',this).each(function(i){
                var arg = $('.tab-arg',this).text();
                var disp = arg;
                if(arg.length > 10){
                    disp = arg.substring(0,9) + '..';
                }
            
                if($.trim($('.tab-arg',this).text()).length == 0){
                    //Apply empty css to empty tab
                    $(this).removeClass(o.cssnormal).addClass(o.cssempty);
                }else{
                    $('.tab-arg',this).html(disp);
                    
                    $(this).mouseenter(function(){
                        //If this is not the active tab, apply mouse over css
                        if($(this).hasClass(o.cssnormal)){
                            $(this).removeClass(o.cssnormal)
                                .addClass(o.cssover);
                        }
                    }).mouseleave(function(){
                        //If there is a mouse over css on the tab, remove it and apply normal css
                        if($(this).hasClass(o.cssover)){
                            $(this).removeClass(o.cssover)
                                .addClass(o.cssnormal);
                        }
                    }).click(function(){
                        showSpinner(contentPane);
                        //Goto parent and deactivate all tabs except current
                        $(this).parent().find('li').each(function(j){
                            if(i == j){
                                $(this)
                                    .removeClass(o.cssnormal)
                                    .removeClass(o.cssover)
                                    .addClass(o.cssactive);
                            }else{
                                if($(this).hasClass(o.cssactive)){
                                    $(this).removeClass(o.cssactive).addClass(o.cssnormal);
                                }
                            }
                        });
                        
                        //Show respective content holder
                        $('.'+ o.container,contentPane).hide();
                            
                        if(o.enableajax && i != 0){
                            //if content is not already loaded then make an ajax call                            
                            var targetContent = $('.' + o.container + ':eq(' + i + ')',contentPane).text();
                            
                            if($.trim(targetContent).length == 0 || targetContent == 'Web Service is temporarily down.'){
                                var tabArg = arg;
                                var tabArgRss = '';
                                var resp = '';
                                //if($('.tab-arg',this).length > 0){
                                    //tabArg = $('.tab-arg',this).text();
                                    if($('.tab-rss-arg',this).length > 0){
                                        tabArgRss = $('.tab-rss-arg',this).html();
                                    }                                    
                                    var a = '';
                                    if($.trim(tabArgRss) == ''){
                                        a=tabArg;
                                    }else{
                                        a=tabArgRss;
                                    }                                
                                    resp=retrieveSubPageTabItemProp(a,o.groupId,o.categoryName);                                    
                                //}
                                $('.' + o.container +':eq(' + i + ')',contentPane).html(resp).fadeIn();
                            }else{
                                $('.' + o.container +':eq(' + i + ')',contentPane).fadeIn();
                            }
                        }else{
                            $('.' + o.container +':eq(' + i + ')',contentPane).fadeIn();
                        }
                        hideSpinner(contentPane);                        
                    });                
                }
            });
        });
    }
})(jQuery);
/*#########################################################################################*/

function doSelectTab(self,content,i){
    //Set the earlier selected li inactive
    $j('li.Tab_HomePage_Active', self)
        .removeClass('Tab_HomePage_Active')
        .addClass('Tab_HomePage');
    
    //Set current li active
    $j('li:eq(' + i +')',self)        
        .removeClass('Tab_HomePage')
        .addClass('Tab_HomePage_Active');
            
    //Hide other content
    $j('.tab-containers',content).hide();
    //Show respective content
    $j('.tab-containers:eq(' + i + ')',content).fadeIn('slow');
}

function getTabData(content, tabId, callbackArg, callback){
    try{    
        showSpinner(content); 
    }catch(e)
    {
    }
    var data = $j('.tab-containers',content).eq(tabId).html()
    data = $j.trim(data);    
    if(((data == "") || (data == "Web Service is temporarily down.")) && (data != "Empty Tab")){
        $j.ajax({
            type: 'POST',
            url: '/XMLResponse.aspx',
            data: 'command=retrieveview&TabPosition=' + (callbackArg-1),
            success: function(msg){
                $j('.tab-containers',content).eq(tabId).html(msg);
                hideSpinner(content);
            }
        });
    }else{
        try{
            hideSpinner(content);
        }
        catch(e){
        }
    }
    callback();
}

function showSpinnerById(elemId){
    var subjectElement = jQuery('#'+elemId);
    showSpinner(subjectElement);
}
function showSpinner(subjectElement){
    var elemId = jQuery(subjectElement).attr('id');
    if(subjectElement.length > 0){
        //check if its hidden
        var vis = subjectElement.is(":visible");                
        if (!vis)
            subjectElement.show();  // must be visible to get .position
            
        var pos = subjectElement.position();
        
        if (!vis) 
            subjectElement.hide();
                    
        var wt = subjectElement.width()/2;
        var ht = subjectElement.height()/2;
        
        jQuery('.tools').append(jQuery('<div id="' + elemId + '_spinner" class="spinner">&nbsp;</div>').css({
            'position':'absolute',
            'left':pos.left + wt - 20,
            'top':pos.top + ht - 20
        }));
    }
}
function hideSpinnerById(elemId){
    var subjectElement = jQuery('#'+elemId);
    hideSpinner(subjectElement);
}
function hideSpinner(subjectElement){
    var elemId = jQuery(subjectElement).attr('id');
    if(subjectElement.length > 0){
        jQuery('#' + elemId + '_spinner').remove();
    }
}

//<!--
var dc_tile=0;
var axel = Math.random() + "";
var ord = axel * 1000000000000000000;        
//-->    

 function MM_findObj(n, d) { //v4.01
    var p,i,x;  if(!d) d=window.document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }   
}

function MM_showHideLayersView() { //v6.0
  var i,p,v,obj,args=MM_showHideLayersView.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null){ 
    v=args[i+2];
    if (obj.style){
        obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; 
    }  
    obj.visibility=v;              
  }    
  ChangeColor(args[args.length-1]);
}

function showdescription(objid,TotalCount){
    var i,p,v,obj,args=MM_showHideLayers1.arguments;
    for (i=0; i<TotalCount; i+=1){
        window.document.getElementById('lbl'+i).style.visibility='hidden';
        window.document.getElementById('lbl'+i).style.height='0px';
        window.document.getElementById(objid).style.overflow ='hidden';
    }
    window.document.getElementById(objid).style.visibility='visible';
    window.document.getElementById(objid).style.height='200px';
    window.document.getElementById(objid).style.overflow='auto';
}

function hidedescription(objid){
    window.document.getElementById(objid).style.visibility='visible';
    window.document.getElementById(objid).style.overflow='auto';
}
