﻿
jQuery('document').ready(function(){
    // rollovers
    jQuery('a img').each(function(){
        var $img = jQuery(this);
        jQuery($img).parents('a').hover(function(){
            swap_over($img);
        }, function(event){
            swap_up($img);
        });
    });
   
    //preload
    jQuery('a img').each(function(){
        var rollSrc = jQuery(this).attr('src');
        var rollOver = rollSrc.replace(/_up/i, '_over');
        jQuery('<img>').attr('src', rollOver);
    });
    
    //search
    jQuery('#txt_search').focus(function(){
        $this = jQuery(this);
        if(this.value == 'search'){
            this.value = '';
            $this.removeClass('placeholder').addClass('search_term');
        }
        $this.keypress(function(event){
            if(event.keyCode == 13){
                event.preventDefault();
                search(this.value);
            }
        }).blur(function(){
            if( ! this.value){
                this.value = 'search';
                jQuery(this).removeClass('search_term').addClass('placeholder');
            }
        });
    });
    
    jQuery('#btn_search').click(function(){
        search(jQuery('#txt_search').val());
    }).keypress(function(event){
        if(event.keyCode == 13){
            search(jQuery('#txt_search').val());
        }
    });
    
    //check for font-size preference
    var font_size_preference = readCookie('font_size_preference');
    if(font_size_preference != null){
        jQuery('body').css('font-size', font_size_preference);
    }
    
    //top and footer navigation active state
    var regex = new RegExp('tabid[/=](\\d*)', 'i'); 
    var window_location = window.location.href;
    jQuery('#top_nav a').each(function(){
        var $this = jQuery(this);         
        location_match = regex.exec(window_location);
        nav_match = regex.exec($this.attr('href'));
        if(nav_match != null && location_match != null){
            var location_tabid = location_match[1];
            var nav_tabid = nav_match[1];
            if(nav_tabid == location_tabid){
                $this.replaceWith("<img src='/portals/1/skins/planbien/assets/images/bullet_small_over.gif' style='vertical-align:text-top'  /><span style='color:#C10538;'>" + $this.text() + "</span>");
            }
        }
        //home button
        if($this.attr('href') == '/'){
            var urlMinusProtocol = window_location.substr(7);
            var urlAfterDomain = urlMinusProtocol.substr((urlMinusProtocol.indexOf('/') + 1));
            if(/^(\w?)$|^(Default.aspx)$|^(Default.aspx\?tabid=212)$/i.test(urlAfterDomain)){
                 $this.replaceWith("<img src='/portals/1/skins/planbien/assets/images/bullet_small_over.gif' style='vertical-align:text-top'  /><span style='color:#C10538;'>" + $this.text() + "</span>");
            }
        }
         
    });   
    jQuery('#footer_left_col a').each(function(){
        var $this = jQuery(this);         
        location_match = regex.exec(window_location);
        nav_match = regex.exec($this.attr('href'));
        if(nav_match != null && location_match != null){
            var location_tabid = location_match[1];
            var nav_tabid = nav_match[1];
            if(nav_tabid == location_tabid){
                $this.css({color: '#C10538'});
            }
        }
    });     
});

function search(term){
    url = '/default.aspx?tabid=96&search=' + term;
    document.location.href = url;
}

function font_size(isIncrease){
    $body = jQuery('body');
    current_size = $body.css('font-size');
    var num = parseFloat(current_size);
    if(isIncrease){
        if(num < 22){++num;}
    } 
    else
    {
        if(num > 9){--num;}
    }
    $body.css('font-size', num + 'px');
    createCookie('font_size_preference', num + 'px', 365);
}

function font_reset(){
    jQuery('body').css('font-size', '12px');
    eraseCookie('font_size_preference');
}

function swap_over($img){
    var imgsrc = $img.attr('src');
    var imgsrcOver = imgsrc.replace(/_up/i, '_over');
    $img.attr('src', imgsrcOver);
}

function swap_up($img){
    if( ! $img.attr('landing')){//don't run on landing page naivigation
        var imgsrc = $img.attr('src');
        var imgsrcUp = imgsrc.replace(/_over/i, '_up');
        $img.attr('src', imgsrcUp);
    }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function pausecomp(millis)
{
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < millis);
} 

//open page content in new window for print
function printContent(){
    printWin = window.open('/print.html','mywin','left=20,top=20,width=700,height=700,toolbar=1,resizable=1,scrollbars=yes');
    printWin.focus();
    src_title = document.title;
    src_value = "<img src='/portals/1/skins/planbien/assets/images/logo.gif'/>";
    src_value += jQuery("div[id$='ContentPane']", '#dnn_ContentPane').html();
    counter = 10;
    waitfortarget();
}
function waitfortarget(){
    counter--;
    if ( ! printWin.document.getElementById('print_content')){
        //do nothing
        if (counter > 0){
            timer=setTimeout("waitfortarget()", 1000);
        }
        else{
            alert('Unable to load print version. Please try again later.');
        }
    }
    else{
        donetarget();
    }
}
function donetarget(){
    printWin.document.title = src_title;
    printWin.document.getElementById('print_content').innerHTML = src_value;
    printWin.print();
}


