jQuery(document).ready(function() {
    $('.needlogin').click(function() {
        if ($.trim($(".spnuser").text()) == '') {
            $('#needloginerror').css('display', 'block');
            loadPopup('.popupContact');
            return false;
        }
        return true;
    });

    var tabContainers = $('div.tabs > div');
    $(".preview").hide();

    var selectedevt = $(".selectedevent .selevhidden").val();
    if (selectedevt != undefined) {
        $(selectedevt).show();
        var selind = $(".selectedevent").attr("id");
        var carousallen = $("#mycarousel li").children().length;
        if (selind == 1) {
            $("#mycarousel").prepend("<li></li>");
        } else {
            var indexs = selind - 2;
            if (selind == carousallen) {
                for (var l = 0; l < indexs; l++) {
                    $(".evtsliderbackul").prepend($("#mycarousel li:first-child").clone());
                    $("#mycarousel li:first-child").remove();
                }
                $("#mycarousel").append("<li></li>");
                $("#mycarousel").append("<li></li>");
            } else {
                for (var l = 0; l < indexs; l++) {
                    $(".evtsliderbackul").prepend($("#mycarousel li:first-child").clone());
                    $("#mycarousel li:first-child").remove();
                }
                centerselected();
            }
        }
    } else {
        $("#auction_id_1").show();
        $('div.tabs ul.tabNavigation a').removeClass('selectedevent');
        $("#1").addClass('selectedevent');
        $("#mycarousel").prepend("<li></li>");
    }

    regdtchange();
    if ($('input[id$="txtCompanynm"]').val() != undefined) {
        if ($('input[id$="txtCompanynm"]').val() != "") {
            $('#searchnmchk').css({ display: 'inline' });
        } else {
            $('#searchnmchk').css({ display: 'none' });
            $(".chkcompnm input").removeAttr("CHECKED");
            $('input[id$="matcheschk"]').attr("checked", "checked");
        }
    }

    $(".chkcompnm").click(function() {
        $(".chkcompnm input").removeAttr("CHECKED");
        $(this).children("input").attr("checked", "checked");
    });
});

$(function() {
    $(".locationitem").each(function() {
        $(this).css({ display: 'block' });
    });

    $(".pr_sub").hide();

    $(".newscontent").hide();
    if ($(".newsclass").val() != undefined) {
        if ($('input[id$="maxpageitems"]').val() != "" && $('input[id$="maxpageitems"]').val() != undefined) {
            pagination();
        } else {
            if ($(".newsclass").val() != "" && $(".newsclass").val() != undefined) {
                var showhmnews = $(".newsclass").val();
                expandpressmenu(showhmnews);
            } else {
                var showhmnews1 = $(".pressexpand").attr("id");
                if (showhmnews1.indexOf("press-") != -1)
                    showhmnews1 = showhmnews1.substring(6, showhmnews1.length);
                expandpressmenu(showhmnews1);
            }
        }
    }

    var windowurl = window.location.pathname.split("/");
    $("#nav li a").click(function() {
        $("#nav li a").css({ color: '#ffffff' });
        $(this).css({ color: '#9999cc' });
    });

    if ($(".selected").attr("href") != undefined) {
        $("#nav li a").each(function() {
            var menuurl = $(".selected").attr("href").split("/");
            var navhref = $(this).attr("href");
            if (navhref.indexOf(menuurl[2]) != -1)
                $(this).css({ color: '#9999cc' });
        });
    }

    if ($(".team").text() != "") {
        if ($("#hiddentelno").val() != undefined) {
            if ($("#hiddentelno").val() != "")
                $("#telno").css({ display: "block", margin: "0px" });
            else
                $("#telno").css({ display: "none" });

            if ($("#hiddenmobno").val() != "")
                $("#mobno").css({ display: "block", margin: "0px" });
            else
                $("#mobno").css({ display: "none" });
        }
        var url = window.location;
        $(".pr_sub li a").each(function() {
            if (this.href == url) {
                this.style.fontWeight = "bold";
                this.style.backgroundColor = "#9999cc";
                $(this).parent().parent().slideToggle("normal");
            }
        });
        expandteammenu('1');
    }

    $('input[id$="txtfromDate"]').click(function() {
        $(".dterror").css({ display: 'none' });
    });

    $('input[id$="txtToDate"]').click(function() {
        $(".dterror").css({ display: 'none' });
    });

    $('input[id$="txtCompanynm"]').click(function() {
        $('#searchnmchk').css({ display: 'inline' });
    });
});

(function($) {
    // VERTICALLY ALIGN FUNCTION
    $.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);

$('.title').vAlign();

$(function() {
    if ($('#collapser').jqcollapse) {
        $('#collapser').jqcollapse({
            slide: true,
            speed: 500,
            easing: 'easeOutCubic'
        });
    }
});

function expandmenu(name) {
    $(".pr_sub").slideUp("normal");
    $("#" + name + " .pr_sub:hidden").slideToggle("normal");
}

function expandteammenu(name) {
    $(".teamexpand").hide();
    $(".team span").show();
    if ($.browser.msie) {
        $(".team span img").each(function() {
            var imgsrc = $(this).attr("src");
            var imgcomplete = $(this).attr("complete");
            if (imgsrc == "/templates/public/images/blank.gif" || imgcomplete == false)
                $(this).parent().hide();
        });
        $("#team-" + name + " .locationitem div span img").each(function() {
            var imgsrc = $(this).attr("src");
            var imgcomplete = $(this).attr("complete");
            if (imgsrc == "/templates/public/images/blank.gif" || imgcomplete == false) {
                $(this).parent().hide();
                $(this).parent().siblings(".teampos").css({ position: "relative" });
            }
        });
    }

    $(".tmarrow img").not("#tmarrow-" + name + " img").attr("src", "/templates/public/images/sbdown_arrow.png");
    if ($("#tmarrow-" + name + " img").attr("src") == "/templates/public/images/sbup_arrow.png") {
        $("#tmarrow-" + name + " img").attr("src", "/templates/public/images/sbdown_arrow.png");
        $("#team-" + name).slideUp("fast");
    } else {
        $("#tmarrow-" + name + " img").attr("src", "/templates/public/images/sbup_arrow.png");
        $("#tmimg-" + name).hide();
        $("#tmocc-" + name).hide();
        $("#team-" + name).slideDown("fast");
    }
}

function expandlocationmenu(name) {
    if ($.browser.msie) {
        $("#team-" + name + " .locationitem span img").each(function() {
            var imgsrc = $(this).attr("src");
            var imgcomplete = $(this).attr("complete");
            if (imgsrc == "/templates/public/images/blank.gif" || imgcomplete == false) {
                $(this).parent().hide();
                $(this).parent().siblings(".width450").css({ position: "relative" });
            }
        });
    }
    $(".teamexpand").hide();
    $(".tmarrow img").not("#tmarrow-" + name + " img").attr("src", "/templates/public/images/sbdown_arrow.png");
    if ($("#tmarrow-" + name + " img").attr("src") == "/templates/public/images/sbup_arrow.png") {
        $("#tmarrow-" + name + " img").attr("src", "/templates/public/images/sbdown_arrow.png");
        $("#team-" + name).slideUp("fast");
    } else {
        $("#tmarrow-" + name + " img").attr("src", "/templates/public/images/sbup_arrow.png");
        $("#team-" + name).slideDown("fast");
    }
}

function expandpressmenu(name) {
    if ($.browser.msie) {
        $("#press-" + name + " div .mediadiv div a img").each(function() {
            var imgsrc = $(this).attr("src");
            var imgcomplete = $(this).attr("complete");
            if (imgsrc == "/templates/public/images/blank.gif" || imgcomplete == false) {
                $(this).parent().hide();
                $(this).parent().parent().parent().css({ display: "none" });
            }
        });
    }
    $(".pressexpand").hide();
    $(".pressarrow img").not("#pressarrow-" + name + " img").attr("src", "/templates/public/images/sbdown_arrow.png");
    if ($("#pressarrow-" + name + " img").attr("src") == "/templates/public/images/sbup_arrow.png") {
        $("#pressarrow-" + name + " img").attr("src", "/templates/public/images/sbdown_arrow.png");
        $("#press-" + name).slideUp("fast");
    } else {
        $("#pressarrow-" + name + " img").attr("src", "/templates/public/images/sbup_arrow.png");
        $("#press-" + name).slideDown("fast");
    }
    $(".newsclass").val(name);
}

function pagination() {
    var pgpaginationcnt = $('input[id$="maxpageitems"]').val();
    var pgtotalcnt = $(".press").length;
    var pgnum = "";
    if (pgpaginationcnt != "") {
        var nopgs = pgtotalcnt / pgpaginationcnt;
        var pagingControls = '';
        if (nopgs > 1) {
            for (var i = 1; i < nopgs + 1; i++) {
                pagingControls += '<a href="#" onclick="showPage(' + i + '); return false;" class="pagination" id="page' + i + '">' + i + '</a> ';
            }
            
            $(".pagepagination").html(pagingControls);
            if ($(".newsclass").val() != "" && $(".newsclass").val() != undefined) {
                var newsclassval = "press-" + $(".newsclass").val();
                for (var k = 1; k < nopgs + 1; k++) {
                    var maxnum = k * pgpaginationcnt;
                    var minnum = maxnum - pgpaginationcnt;
                    for (var j = minnum; j < maxnum; j++) {
                        if ($(".pressexpand").eq(j).attr("id") == newsclassval) {
                            pgnum = k;
                            break;
                        }
                    }
                } 
            }else {
                pgnum = $('input[id$="whichpageitems"]').val();
            }
        }
        if (pgnum == "")
            showPage('1');
        else
            showPage(pgnum);
    }
}

function showPage(num) {
    var pgpaginationcnt = $('input[id$="maxpageitems"]').val();
    $('input[id$="whichpageitems"]').val(num);
    if (pgpaginationcnt != "") {
        $(".press").hide();
        $(".press1").hide();
        $(".pressexpand").hide();
        var maxshow = pgpaginationcnt * num;
        var minshow = maxshow - pgpaginationcnt;
        for (var i = minshow; i < maxshow; i++) {
            $(".press").eq(i).show();
            $(".press1").eq(i).show();
            if ($(".newsclass").val() != "" && $(".newsclass").val() != undefined) {
                var newsclassval = "press-" + $(".newsclass").val();
                if ($(".pressexpand").eq(i).attr("id") == newsclassval) {
                    $("#pressarrow-" + $(".newsclass").val() + " img").attr("src", "/templates/public/images/sbdown_arrow.png");
                    expandpressmenu($(".newsclass").val());
                }
            }
        }
        if ($(".newsclass").val() == "" || $(".newsclass").val() == undefined) {
            var pgnum = minshow + 1;
            $("#pressarrow-" + pgnum + " img").attr("src", "/templates/public/images/sbdown_arrow.png");
            expandpressmenu(pgnum);
        }
        $(".pagination").css({ fontWeight: "normal" });
        $(".pagepagination #page" + num).css({ fontWeight: "bold" });
    }
}

function regdtchange() {
    if ($('select[id$="regdate"]').val() != undefined) {
        var regdatecss = $('#trfromdt').css("display");
        if ($('select[id$="regdate"]').val() == "A" && regdatecss == "none") {
            $('#trfromdt').css({ display: 'inline' });
            $('#trtodt').css({ display: 'inline' });
        } else {
            $('#trfromdt').css({ display: 'none' });
            $('#trtodt').css({ display: 'none' });
            $(".dterror").css({ display: 'none' });
        }
    }

    if ($('input[id$="selcntry"]').val() != undefined) {
        if ($('input[id$="selcntry"]').val() != "") {
            var selcntry = $('input[id$="selcntry"]').val().split(",");
            for (var i = 0; i < selcntry.length - 1; i++) {
                $('select[id$="searchLocation"] option').each(function() {
                    if ($(this).val() == selcntry[i])
                        $(this).attr("selected", "selected");
                });
            }
        } else {
            if ($('input[id$="selloc"]').val() == "Select")
                $('select[id$="searchLocation"] option:first-child').attr("selected", "selected");
        }
    }
}

function Addselloc() {
    var selcntry = "";
    $("#selectedcountries").css({ display: 'inline' });
    if ($('select[id$="searchLocation"] option').is(":first-child:selected")) {
        $("#selectedcountries").empty();
        $('input[id$="selloc"]').val("Select");
    } else {
        var str = "Selected Locations: <br><select multiple='true' size='4'>";
        $('select[id$="searchLocation"] option:selected').each(function() {
            str += "<option value='" + $(this).val() + "'> - " + $(this).text() + "</option>";
            selcntry += $(this).val() + ",";
        });
        str += "</select>";
        $("#selectedcountries").html(str);
        $("#selectedcountries select").css({ border: "0px" });
        $('input[id$="selloc"]').val("");
    }
    $('input[id$="selcntry"]').val(selcntry);
}

function hdnverstatus() {
    var selverstatus = "";
    if (!$('select[id$="searchVerStatus"] option').is(":first-child:selected")) {
        $('select[id$="searchVerStatus"] option:selected').each(function() {
            selverstatus += $(this).text() + ",";
        });
    }
    $('input[id$="hdnverstatus"]').val(selverstatus);
}

function defaultSearchFields() {
    if ($('select[id$="regdate"]').val() == "A" && $('input[id$="txtfromDate"]').val() == "" && $('input[id$="txtToDate"]').val() == "") {
        $(".dterror").css({ display: 'inline' });
    } else {
        $('select[id$="regdate"] option:selected').removeAttr("selected");
        $('select[id$="regdate"] option:first').attr("selected", "selected");
        $('#trfromdt').css({ display: 'none' });
        $('#trtodt').css({ display: 'none' });
        $('input[id$="txtfromDate"]').val("");
        $('input[id$="txtToDate"]').val("");
        $(".dterror").css({ display: 'none' });
        $('select[id$="searchLocation"] option:selected').removeAttr("selected");
        $('select[id$="searchVerStatus"] option:selected').removeAttr("selected");
        $("#searchnmchk").css({ display: 'none' });
        $('input[id$="incompletereg"]').removeAttr("CHECKED");
        $('input[id$="deactivatedreg"]').removeAttr("CHECKED");
        $('input[id$="selcntry"]').val("");
        $('input[id$="hdnverstatus"]').val("");
        $('input[id$="txtCompanynm"]').val("");
        $(".chkcompnm input").removeAttr("CHECKED");
        $('input[id$="matcheschk"]').attr("checked", "checked");
        $("#selectedcountries").css({ display: 'none' });
    }
    return false;
}
