﻿var blinktimeout;
var morebtn;
var isinital = true;
$(document).ready(function() {
    var height = $(window).height();
    $('#MainContainer').css('height', $(window).height() + 'px');
    var x = ((height + 395) / 2) - (395);
    if (x < 190)
        x = 190;
    $('#pnlMainContent').css('top', Math.floor(x) + 'px');

    x = ((height + 380) / 2) - (380);
    $('#pnlVideo').css('top', Math.floor(x) + 'px');

    x = ((1004 + 508) / 2) - (508);
    $('#pnlVideo').css('left', Math.floor(x) + 'px');

    $('#pnlNationwide').css('height', (height - 100) + 'px');
    x = ((height + (height - 100)) / 2) - ((height - 100));
    $('#pnlNationwide').css('top', Math.floor(x) + 'px');

    x = ((1004 + 818) / 2) - (818);
    $('#pnlNationwide').css('left', Math.floor(x) + 'px');

    morebtn = $('#btnMore');
    BlinkingMore();
    $('#navmenu').children().children().click(function() {
        $(this).addClass('selected').siblings().removeClass('selected');
        ShowMainContent($(this).html());
    });

    $('#navHome').children().click(function() {
        $(this).addClass('selected').siblings().removeClass('selected');
        var cidx = $(this).index();
        $('#pnlHome').children(':eq(' + cidx + ')').css('display', 'block').siblings().css('display', 'none');
    });

    $('#navNews').children().click(function() {
        $(this).addClass('selected').siblings().removeClass('selected');
        var cidx = $(this).index();
        $('#pnlNews').children(':eq(' + cidx + ')').css('display', 'block').siblings().css('display', 'none');
    });
});


function BlinkingMore() {
    blinktimeout = setInterval(function() {
        if (morebtn.css('color') == 'rgb(255, 255, 255)') {
            morebtn.css('color', 'rgb(255, 0, 0)');
        }
        else if (morebtn.css('color') == '#ffffff') {
            morebtn.css('color', '#ff0000');
        }
        else if (morebtn.css('color') == 'red') {
            morebtn.css('color', 'white');
        }
        else if (morebtn.css('color') == 'Red') {
            morebtn.css('color', 'White');
        }
        else {
            morebtn.css('color', 'rgb(255, 255, 255)');
        }
    }, 500);
}

function ShowMenu() {
    clearInterval(blinktimeout);
    morebtn = null;
    $('#btnMore').css('visibility', 'hidden');
    $('#navmenu').show('slide', { 'direction': 'down' }, 500);
}

function ShowMainContent(ID) {
    if (isinital) {
        isinital = false;
        $('#pnlInitial').css('display', 'none');
        $('#pnlMainContent').find('.' + ID).css('display', 'block').siblings('.window').css('display', 'none');

        $('#pnlMainContent').show('slide', { 'direction': 'right' }, 2000, function() {
            if (ID != 'contact' && ID != 'hello') {
                $('#rightContent').show('slide', { 'direction': 'left' }, 500, function() {
                    if (ID == 'web') {
                        $('#pnlWeb').prepend('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="250" height="250" id="chuziTransition" name="chuziTransition"><param name="movie" value="swf/chuzi.swf"> <param name="quality" value="high"><param name="wmode" value="transparent"><embed src="swf/chuzi.swf" quality=high wmode="transparent" width="250" height="250" id="chuziTransition"  name="chuziTransition" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed> </object>');
                    }
                });
            }
            else {
                $('#rightContent').children().css('display', 'none');
            }
        });

        setTimeout(function() {
            $('#navmenu').hide('slide', { 'direction': 'left' }, 500, function() {
                $(this).css({ 'left': '375px', 'top': '20px' }).show('slide', { 'direction': 'left' }, 500);
            });
        }, 1300);
    }
    else {
        if (ID == 'contact' || ID == 'hello') {
            if ($('#rightContent').css('display') == 'block') {
                $('#rightContent').hide('slide', { 'direction': 'left' }, 500, function() {
                    LoadRightContent(ID);
                });
            }
        }
        else {
            if ($('#rightContent').css('display') == 'none') {
                $('#rightContent').show('slide', { 'direction': 'left' }, 500, function() {
                    LoadRightContent(ID);
                });
            }
            else {
                LoadRightContent(ID);
            }
        }
        LoadLeftContent(ID);
    }
}

function NationWide() {
    $('#navNews').children(':first').next().next().addClass('selected').siblings().removeClass('selected');
    if ($('#frameNationWide').length == 0) {
        $('#pnlNationwide').append('<iframe style="border:none;" id="frameNationWide" width="100%" height="100%" src="http://www.philstar.com/Article.aspx?articleId=704114&publicationSubCategoryId=448"></iframe>');
        $('#pnlNationwide').fadeIn(1000);
    } else {
        $('#pnlNationwide').fadeIn(1000);
    }
}


function HideNationWide() {
    $('#pnlNationwide').fadeOut(1000);
}
function LoadLeftContent(ID) {
    var current = $('#leftContent').children('.window:visible:first');
    var item = $('#leftContent').find('.' + ID + ':first');
    if (item.css('display') == 'none') {
        $('#Forcefield').css('display', 'block');
        current.hide('slide', { 'direction': 'left' }, 1000);
        item.show('slide', { 'direction': 'right' }, 1000, function() {
            $('#Forcefield').css('display', 'none');
            if (ID == 'contact') {
                $('#txtFname').focus();
            }
        });
    }
}

function LoadRightContent(ID) {
    var current = $('#rightContent').children('.window:visible:first');
    var item = $('#rightContent').find('.' + ID + ':first');
    if (item.css('display') == 'none') {
        $('#Forcefield2').css('display', 'block');

        $('#pnlWeb').html('<br /><br /><a href="ChuziAccess.aspx" style="color:#FF6600; text-decoration:underline; cursor:pointer;">Check out Chuzi.com</a>');
        current.hide('slide', { 'direction': 'right' }, 1000);
        item.show('slide', { 'direction': 'left' }, 1000, function() {
            $('#Forcefield2').css('display', 'none');
            if (ID == 'web') {
                $('#pnlWeb').prepend('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="250" height="250" id="chuziTransition" name="chuziTransition"><param name="movie" value="swf/chuzi.swf"> <param name="quality" value="high"><param name="wmode" value="transparent"><embed src="swf/chuzi.swf" quality=high wmode="transparent" width="250" height="250" id="chuziTransition"  name="chuziTransition" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed> </object>');
            }
        });
    }
    else {
        if (ID == 'hello') {
            if ($('#pnlHome').children(':first').css('display') == 'none') {
                $('#navHome').children(':first').click();
            }
        }
    }

}

function ShowContact() {
    $('#liContactPage').click();
}

function SendContact() {
    var firstname = $('#txtFname').val();
    var lname = $('#txtLname').val();
    var email = $('#txtEmail').val();
    var phone = $('#txtPhone').val();
    var company = $('#txtCompany').val();
    var msg = $('#txtMessage').val();
    var recieve = false;
    if ($('#chkReceive').attr('checked') == 'checked')
        recieve = true;
    if (firstname.length == 0 || firstname == 'Enter First Name') {
        $('#txtFname').val('Enter First Name').css('color', 'red');
    }
    else {
        $('#txtFname').css('color', 'black');
        if (lname.length == 0 || lname == 'Enter Last Name') {
            $('#txtLname').val('Enter Last Name').css('color', 'red');
        }
        else {
            $('#txtLname').css('color', 'black');
            if (email.length == 0 || email == 'Enter Email') {
                $('#txtEmail').val('Enter Email').css('color', 'red');
            }
            else {
                $('#txtEmail').css('color', 'black');
                if (validateEmail(email) == false || email == 'Enter Valid Email') {
                    $('#txtEmail').val('Enter Valid Email').css('color', 'red');
                }
                else {
                    $('#txtEmail').css('color', 'black');
                    if (company.length == 0 || company == 'Enter Company') {
                        $('#txtCompany').val('Enter Company').css('color', 'red');
                    }
                    else {
                        $('#txtCompany').css('color', 'black');
                        $('#btnSubmit').css('display', 'none').next().css('display', 'block');
                        $.ajax({ url: '/Services/Mail.asmx/SendContact', data: "{'FN':'{0}','LN':'{1}','P':'{2}','E':'{3}','C':'{4}','R':'{5}','M':'{6}'}".format(firstname, lname, phone, email, company, recieve, msg), success: function(msg) {

                        }, async: true, type: 'POST', dataType: 'json', contentType: 'application/json; charset=utf-8'
                        });
                        setTimeout(function() {
                            $('#txtFname').val('');
                            $('#txtLname').val('');
                            $('#txtEmail').val('');
                            $('#txtPhone').val('');
                            $('#txtCompany').val('');
                            $('#txtMessage').val('');
                            $('#chkReceive').attr('checked', false);
                            $('#btnSubmit').css('display', 'block').next().css('display', 'none');
                            alert('Successfully sent.');
                        }, 2000);
                    }
                }
            }
        }
    }
}

function ShowVideo(ID) {
    $('#pnlVideo').css('display', 'block');
    $('#ctrVideoPlayer').append('<object height="358" width="480" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" id="lightwindow_media_primary" name="lightwindow_media_primary"><param value="video/player.swf?video=DD_Trailer.flv" name="movie"><param value="video/player.swf?video=DD_Trailer.flv" name="src"><param value="true" name="controller"><param value="transparent" name="wmode"><param value="false" name="cache"><param value="high" name="quality"><object height="358" width="480" type="application/x-shockwave-flash" data="video/player.swf?video=DD_Trailer.flv" id="lightwindow_media_secondary" name="lightwindow_media_secondary"><param value="true" name="controller"><param value="transparent" name="wmode"><param value="false" name="cache"><param value="high" name="quality"></object></object>');
    $('#videoOverlay').css('display', 'block');
}
function CloseVideo() {
    $('#pnlVideo,#videoOverlay').css('display', 'none');
    $('#ctrVideoPlayer').empty();
}

function validateEmail(email) {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
    return emailPattern.test(email);
}

String.prototype.format = function() {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}
