﻿$(document).ready(function() {
    //apply hover effect for menus
    ApplyHoverEffect();
    //post load scrolling images
    setTimeout("PostloadImages()", 200);
    //initialize scroller
    window._ScrollTimer = setTimeout("DoScrolling(2000)",2000);
    //initialize in-page popups
    $("a.popup").click(function(){
        var jThis = $(this); 
        OpenModalDialog(jThis, jThis.text(), jThis.attr('href'));
        return false;
    });
});

// Update Menu Highlight
function UpdateMenu(product,text, tooltip)
{
    if($('#MenuItem' + product).length<=0)
        SwapMenu(product, text, tooltip);
    
    window._CurrentProduct = text;
    $('#MenuItem' + product).addClass('MenuItemSelected');
    $('#MenuItemText' + product).addClass('MenuItemTextSelected');
}

/*****************************************************
        Apply hover effect on tabs
******************************************************/
function ApplyHoverEffect()
{
    $("div.MenuItem").click(function(){
        $("a", this).trigger('click');
    });
    
    $("a","div.MenuItem").click(function(event){
        window.location = this.href;
        event.stopPropagation();
        return true;
    });
    
    $("div.MenuItem").mouseenter(function(){
        $(this).addClass('MenuItemHover');
        $('.MenuItemText',this).addClass('MenuItemTextHover');
    });
    
    $("div.MenuItem").mouseleave(function(){
        $(this).removeClass('MenuItemHover');
        $('.MenuItemText',this).removeClass('MenuItemTextHover');
    });
    
    $("#MenuItemmore").mouseenter(function(){
        var oMenu = $('div.MoreMenuItems',this);
        var oCur = $(this);
        oMenu.css('right', (983 - oCur.position().left - oCur.width()) + 'px');
        oMenu.show();
    });
    
    $("#MenuItemmore").mouseleave(function(){
        $('div.MoreMenuItems',this).hide();
    });
}

/*****************************************************
    Load the Request a Call widget thingy
******************************************************/
function ToggleCallBack()
{
    if($('#divRequest_Call_Back:visible').length<=0)
    {
        if($('#divRequest_Call_Back').children().length<=0)
        {
            $('#divRequest_Call_Back').load('RequestCallBack.aspx', function() {
               $('#divRequest_Call_Back').slideDown('slow');
            });
        }
        else
            $('#divRequest_Call_Back').slideDown('slow');
    }
    else
        $('#divRequest_Call_Back').slideUp('slow');
}

/*****************************************************
    Submit the Call back form using Ajax
******************************************************/
function DoSubmitCallback()
{
    if($('#txtReqCallBack_Name').val().length<=0)
    {
        alert('Please enter your name');
        $('#txtReqCallBack_Name').focus();
        return;
    }
    
    if($('#txtReqCallBack_ContactNo').val().length<=0)
    {
        alert('Please enter your contact number');
        $('#txtReqCallBack_ContactNo').focus();
        return;
    }
    
    var sUrl = ('RequestCallBack.aspx?ref=' + $('#hdnIntroducerRef').val() + 
                '&ContactName=' + escape($('#txtReqCallBack_Name').val()) +
                '&ContactNumber=' + escape($('#txtReqCallBack_ContactNo').val()) + 
                '&ContactNotes=' + escape($('#txtReqCallBack_Notes').val()) + 
                '&key=' + window._CurrentProduct);
    $('#divRequest_CallBack_Button').hide();
    $('#divRequest_CallBack_Progress').show();
    $('#divRequest_Call_Back').load(sUrl, function() {
        $('#divRequest_CallBack_Progress').hide();
    });
}

/*****************************************************
    Post Load Images
******************************************************/
function PostloadImages()
{
    //post loading images
    var postImgs=$("img.postload_src");
    for(var i=0;i<postImgs.length;i++)
    {
        var indimg = $(postImgs[i]);
        indimg.attr("src",indimg.attr("alt"));
        indimg.show();
    }
}

/*****************************************************
  Function to scroll images on the right hand side
******************************************************/
function DoScrolling(iSpeed)
{
    var oParent = $('#divScroller');
    if(oParent.length<=0) return;
    var oUl = $("ul",oParent);
    var iMax = -1 * (oUl.height() - oParent.height());
    if(!oUl[0].style.marginTop) 
        oUl[0].style.marginTop='0px';
    var iPos = parseInt(oUl[0].style.marginTop) + oUl.position().top;
    if(iPos>(iMax + oUl.position().top))
    {
        window._CurrentScrollTop += 1;
        var oLi = $('li', oUl[0]).eq(window._CurrentScrollTop);
        if (oLi.length>0) 
            iPos -= (oLi.position().top);
        else
        {
            iPos = 0;
            window._CurrentScrollTop = 0;
        }
    }
    else
    {
        iPos = 0;
        window._CurrentScrollTop = 0;
    }

	oUl.animate(
		{ marginTop: iPos}, 
		iSpeed);

    clearTimeout(window._ScrollTimer);
    window._ScrollTimer = setTimeout("DoScrolling(2000)",3000);
}

/*****************************************************
  Function to manually scroll images on the 
  right hand side
******************************************************/
function GetNextScroll()
{
    clearTimeout(window._ScrollTimer);
    DoScrolling(50);
}


/*********---*-*-***-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-**--**/
/*****************************************************
    Opens up a new window with the product 
    processing URL
******************************************************/
function GotoLink(linkId, params, introducerParamName)
{
    params = params?params:'';
    introducerParamName = introducerParamName?introducerParamName:'IntroducerRef';
    var sVars = '{2}={0}&{1}'.format($('#hdnIntroducerRef').val(), params, introducerParamName);
    var sUrl = "getcontent.aspx?reqtype=ext&key={0}&ref={2}&params={1}".format(linkId, escape(sVars),
                            $('#hdnIntroducerRef').val())
    window.open(sUrl);
}

/*****************************************************
    Opens up a new window with the product 
    processing URL
******************************************************/
function GotoExLink(linkUrl)
{
    linkUrl = linkUrl.replace("[$Ref]", $('#hdnIntroducerRef').val());
    window.open(linkUrl);
}

/*****************************************************
    Opens up Ajaxy window with a url
    (These pages can not have script tags)
******************************************************/
function OpenModalDialog(src, title, url)
{
    url=unescape(url);
    if($('#Qq4u_dialog_content')[0]._CurrentUrl!=url)
    {
        $('#Qq4u_dialog_content')[0]._CurrentUrl=url;
        $('#Qq4u_dialog_content').load(url, function() {
            ShowDialog(title,src);
        });
    }
    else
    {
        ShowDialog(title,src);
    }
}

function ShowDialog(title, src)
{   
    var width = parseInt(src.css('width')); 
    var height = parseInt(src.css('height'));

    if(!width) width=450;
    if(!height) height=400;

    if($('#divFrameResize').length<=0)
    {
        var fm = document.createElement('DIV');
        fm.id='divFrameResize';
        $('body').append(fm);
    }
    $('#divFrameResize').css({
        "position":"absolute",
        "background-color":'white',
        "border":"solid 2px gray",
        "top":src.offset().top + "px",
        "left":src.offset().left + "px",
        "width":src.width() + "px",
        "height":src.height() + "px"
    });
    
    var tar = $("#Qq4u_dialog");
    tar.dialog(
    {
        bgiframe: true,
        modal: true,
        maxHeight: '600px',
        autoOpen:false,
        open: function(event, ui) {
            $(this).css({'max-height': 600, 'overflow-y': 'auto'}); 
        }
    });
    
    tar.dialog('option','title', title);
    tar.dialog('option','width', width + 'px');
    tar.dialog('option','maxHeight', '600px');
    //if(src.attr('height')) tar.dialog('option','height', height);
    
    tar.unbind('dialogclose');
    tar.bind('dialogclose',function(){
        $('#divFrameResize').animate({
            top:src.offset().top + "px",
            left:src.offset().left + "px",
            width:src.width() + "px",
            height:src.height() + "px"
        }, {
            duration: 200, 
            specialEasing: {
              width: 'swing',
              height: 'swing'
            }, 
            complete: function() {
                $(this).hide();
            }
        });
    });
    
    

    var tTop = (($(window).height()-height)/2)+$(window).scrollTop();
    var tLeft = (($(window).width()-width)/2)+$(window).scrollLeft();
    
    $('#divFrameResize').animate({
        top:tTop + "px",
        left:tLeft + "px",
        width:width + "px",
        height:height + "px"
      }, {
        duration: 200, 
        specialEasing: {
          width: 'swing',
          height: 'swing'
        }, 
        complete: function() {
            $(this).hide();
            tar.dialog('open');
        }
      });
}

/*************************************************
    This is to swap the item from more... section
**************************************************/
function SwapMenu(code, text, tooltip)
{
    $('div.MenuItem', '#menuTemp').attr('id','MenuItem' + code);
    $('div.MenuItemText', '#menuTemp').attr('id','MenuItemText' + code);
    $('a', '#menuTemp').text(text);
    $('a', '#menuTemp').attr('href', window.location);
    $('a', '#menuTemp').attr('title', tooltip);
}


/****************************************************
    Launch page in iframe
****************************************************/
function GotoLink(product)
{
    $('#mainSection').html('<iframe width="700px" frameborder="0" height="800px;" src="process.aspx?ProductCode=' + product + '"></iframe>');
}
