﻿(function($) {
    // VERTICALLY CENTER ELEMENT
    $.fn.vAlign = function() {
        return this.each(function(i) {
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = (ph - ah) / 2;
            $(this).css('margin-top', mh);
        });
    };
})(jQuery);

function FormActionChange() {
    var action = $("#aspnetForm").attr("action");
    if (action.indexOf('bing/') != 0)
        $("#aspnetForm").attr("action", "bing/" + action);

}


(function($) {
    // VERTICALLY ALIGN TO CENTER OF BROWSER WINDOW FUNCTION (MAKE POSITION:ABSOLUTE)
    $.fn.vAlignWin = function() {
        return this.each(function(i) {
            var ah = $(this).height();
            var ph = $(window).height();
            var mh = (ph - ah) / 2;
            mh = mh + $(window).scrollTop();
            $(this).css('top', mh);
        });
    };
})(jQuery);

(function($) {
    // HORIZONTALLY CENTER ELEMENT
    $.fn.hAlign = function() {
        return this.each(function(i) {
            var ah = $(this).width();
            var ph = $(this).parent().width();
            var mh = (ph - ah) / 2;
            $(this).css('margin-left', mh);
        });
    };
})(jQuery);

(function($) {
// HORIZONTALLY CENTER ELEMENT OF BROWSER WINDOW FUNCTION (MAKE POSITION:ABSOLUTE)
    $.fn.hAlignWin = function() {
        return this.each(function(i) {
            var ah = $(this).width();
            var ph = $(window).width();
            var mh = (ph - ah) / 2;
            mh = mh + $(window).scrollLeft();
            $(this).css('left', mh);
        });
    };
})(jQuery);

/* COOKIE FUNCTIONS */
function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}


// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/* END COOKIE FUNCTION */

function Show_Popup(id, type, tab) {
    document.getElementById('popwrap').innerHTML='<div style=\'width:220px;margin-right:auto;margin-left:auto;padding-top:100px;color:white;text-align:center\'>Loading...<br><img src=\'/images/Generic/ajax-loader.gif\'></div>';
    $('#popup_bkg').css('height',$(document).height()).css('opacity',0.25).fadeIn('fast');
    //$('#popup_window').hAlign().css('top',$(window).scrollTop() + 200).fadeIn('fast');
    $('#popup_window').hAlignWin().vAlignWin().fadeIn('fast');
    
    UpdatePopPanel(id, type, tab);
    //Following fixes 'quick look' not going away:
    $('.quick_rapper .q').css('display', 'none');
    setTimeout("$('.quick_rapper .q').css('display', '')",1250);
}

function Show_Filter_Popup(what, id, type, catid, btnUniqueKey) {
    document.getElementById('popwrap1').innerHTML='<div style=\'width:220px;margin-right:auto;margin-left:auto;padding-top:100px;color:white;text-align:center\'>Loading...<br><img src=\'/images/Generic/ajax-loader.gif\'></div>';
    $('#popup_bkg1').css('height',$(document).height()).css('opacity',0.25).fadeIn('fast');
    $('#popup_window1').hAlignWin().vAlignWin().fadeIn('fast');
    
    UpdateFilterPopPanel(what, id, type, catid, btnUniqueKey);
}

function Close_Popup() {
    $('#popup_bkg').fadeOut('fast');
    $('#popup_window').fadeOut('fast');
}
function Close_Filter_Popup() {
    $('#popup_bkg1').fadeOut('fast');
    $('#popup_window1').fadeOut('fast');
}
function InitProdPopUp(tab2ID, tabb2ID, tab3ID, tabb3ID) {
    
    $('#tab1').live('click', function() {
      $('#tab1').attr('class', 'sel');
      $(tab2ID).attr('class', 'reg');
      $(tab3ID).attr('class', 'reg');
      $('#tabb1').css('display', 'block');
      $(tabb2ID).css('display', 'none');
      $(tabb3ID).css('display', 'none');
    });

    $(tab2ID).live('click', function() {
      $('#tab1').attr('class', 'reg');
      $(tab2ID).attr('class', 'sel');
      $(tab3ID).attr('class', 'reg');
      $('#tabb1').css('display', 'none');
      $(tabb2ID).css('display', 'block');
      $(tabb3ID).css('display', 'none');
    });

    $(tab3ID).live('click', function() {
      $('#tab1').attr('class', 'reg');
      $(tab2ID).attr('class', 'reg');
      $(tab3ID).attr('class', 'sel');
      $('#tabb1').css('display', 'none');
      $(tabb2ID).css('display', 'none');
      $(tabb3ID).css('display', 'block');
    });
}

function showOnRight(index) {
    if ($('#bt_' + index).css('display') == 'block') {
        return;
    }
    for (i = 1; i < 5; i++) {
        $('#bt_' + i).css('display', 'none');
        $('#t_' + i).attr('class', 't');
    }
    //$('#bt_' + index).css('display', 'block');
    
    $('#bt_' + index).fadeIn();
    $('#t_' + index).attr('class', 't t_sel');
}

function showFeaturedItem(index) {

    if (index == '0') {
        var selected_index = 1;
        for (i = 1; i < 6; i++) {
            if ($('#div_link_img_' + i).css('display') == 'block') {
                selected_index = i;
            }
        }
        if (selected_index + 1 > 5) {
            selected_index = 1;
        }
        else {
            selected_index += 1;
        }
        index = selected_index;
        //alert(index);
    }

    if ($('#div_link_img_' + index).css('display') == 'block') {
        return;
    }
    for (i = 1; i < 6; i++) {
        $('#div_link_img_' + i).css('display', 'none');
        $('#div_featured_content_' + i).css('display', 'none');
        $('#t_' + i).attr('class', 't');
    }
    $('#div_link_img_' + index).fadeIn();
    $('#div_featured_content_' + index).fadeIn();
    $('#t_' + index).attr('class', 't t_sel');
}

//retrieve querystring from client side
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}


function FormActionChange(wl) {
    var action = $("#aspnetForm").attr("action");
    if (action.indexOf(wl + '/') != 0)
        $("#aspnetForm").attr("action", wl + "/" + action);
}
