﻿jQuery.fn.fadeToggle = function (speed, easing, callback) { return this.animate({ opacity: 'toggle' }, speed, easing, callback) };
jQuery(document).ready(function () {
    var country = $.cookie('country3');
    if (country != null)
        country = country.replace("country3=", "");

    if (country == null || country == "") {
        country = 'us';
    }

    //    $(".category").mouseover(function (e) {
    //        $(".category").css({ backgroundColor: '#3e90c0' });
    //    });
    //country = country;
    //            $('.category .tab').hide();
    //            $('.' + country).show();
    //            $('#countryPicker').html($('#' + country).html());
    //            $('#countryPicker').fadeIn();
    $('.trendingCountry').attr({ src: 'images/' + country + '.gif' });

    var mouseOverDelete = false;
    $(".deleteButton").mouseover(function () { mouseOverDelete = true; }).mouseout(function () { mouseOverDelete = false; });

    $('.deleteButton').click(function (e) {
        deleteClicked = true;
        $('#ctl00_txtSearch').val('');
        $('#ctl00_txtSearch').focus();
    });

    $(".newsItemSmall").each(function () {
        //alert($(this).parent().height());
        $(this).height($(this).parent().height() - 17);
    });

    //    $(".newsItemSmall").mouseover(function () {
    //        $(this).animate({
    //            opacity: '100'
    //        }, 500, function () {
    //            // Animation complete.
    //        });
    //    });

    $('#ctl00_countryPicker').click(function (e) {
        e.stopPropagation();
        $('#countryDrop').toggle();
        $('.dropBox').hide();
        $('.category').css({ backgroundColor: 'rgb(71, 166, 222)' });
        $('.search').css({ backgroundColor: 'rgb(71, 166, 222)' });
    });

    var isFocused = false;

    $('#ctl00_txtSearch').focus(function (e) {
        $('#searchHelp').fadeOut();
        isFocused = true;
        $('#ctl00_txtSearch').animate({
            backgroundColor: '#ffffff',
            //width: '188px',
            color: 'black'
        }, 250, function () {
            $('#ctl00_txtSearch').css({ backgroundPosition: '5px 3px' });
            $('.deleteButton').show();
        });

        //        $('#ctl00_recent').animate({
        //            opacity: '0'
        //        }, 250, function () {
        //            // Animation complete.
        //        });
    });

    $('#ctl00_txtSearch').blur(function (e) {
        //only run blur function if delete textbox content button not clicked
        isFocused = false;
        if (!mouseOverDelete) {

            $('.deleteButton').hide();
            $('#ctl00_txtSearch').animate({
                backgroundColor: '#327197',
                //width: '145px',
                color: '#fff'
            }, 250, function () {
                $('#ctl00_txtSearch').css({ backgroundPosition: '5px -44px' });
            });

            //            $('#ctl00_recent').animate({
            //                opacity: '100'
            //            }, 500, function () {
            //                // Animation complete.
            //            });

        }
    });

    $('#ctl00_txtSearch').mouseover(function () {
        if (!isFocused) {
            $('#ctl00_txtSearch').animate({
                backgroundColor: '#285b7a'
            }, 500, function () {
                // Animation complete.
            });
        }
    });

    $('#ctl00_txtSearch').mouseout(function () {
        if (!isFocused) {
            $('#ctl00_txtSearch').animate({
                backgroundColor: '#327197'
            }, 500, function () {
                // Animation complete.
            });
        }
    });

    $("#ctl00_txtSearch").result(function (event, data, formatted) {

        window.location.href = 'http://interceder.net/topic/' + formatted.replace('&', '%26').replace(' ', '-');
        
    });

    $("#ctl00_txtSearch").autocomplete('Search_CS.ashx', { minChars: 3, width: 186, max: 20, scrollHeight: 400, selectFirst: false });


    $('#ctl00_recent,#top,#business,#technology,#entertainment,#sports').click(function (e) {
        e.stopPropagation();
        $('.search').css({ backgroundColor: 'rgb(71, 166, 222)' });
        $('.category').css({ backgroundColor: 'rgb(71, 166, 222)' });
        $('.dropBox').hide();

        if ($(this).attr('id') == "ctl00_recent") {
            $('#dropRecent').fadeToggle();
            //alert($('.search').css('backgroundColor'));
            if ($('.search').css('backgroundColor') == 'rgb(71, 166, 222)' || $('.search').css('backgroundColor') == 'rgba(0, 0, 0, 0)')
                $('.search').css({ backgroundColor: 'rgb(62, 146, 195)' });
            else if ($('.search').css('backgroundColor') == 'rgb(62, 146, 195)')
                $('.search').css({ backgroundColor: 'rgb(71, 166, 222)' });
            $('#dropTop').hide();
            $('#dropBusiness').hide();
            $('#dropTechnology').hide();
            $('#dropEntertainment').hide();
            $('#dropSports').hide();
        }
        else if ($(this).attr('id') == "top") {
            $('#dropTop').fadeToggle();
            if ($('#cattop').css('backgroundColor') == 'rgb(71, 166, 222)' || $('#cattop').css('backgroundColor') == 'rgba(0, 0, 0, 0)')
                $('#cattop').css({ backgroundColor: 'rgb(62, 146, 195)' });
            else if ($('#cattop').css('backgroundColor') == 'rgb(62, 146, 195)')
                $('#cattop').css({ backgroundColor: 'rgb(71, 166, 222)' });
        }
        else if ($(this).attr('id') == "business") {
            $('#dropBusiness').fadeToggle();
            if ($('#catbusiness').css('backgroundColor') == 'rgb(71, 166, 222)' || $('#catbusiness').css('backgroundColor') == 'rgba(0, 0, 0, 0)')
                $('#catbusiness').css({ backgroundColor: 'rgb(62, 146, 195)' });
            else if ($('#catbusiness').css('backgroundColor') == 'rgb(62, 146, 195)')
                $('#catbusiness').css({ backgroundColor: 'rgb(71, 166, 222)' });
        }
        else if ($(this).attr('id') == "technology") {
            $('#dropTechnology').fadeToggle();
            if ($('#cattechnology').css('backgroundColor') == 'rgb(71, 166, 222)' || $('#cattechnology').css('backgroundColor') == 'rgba(0, 0, 0, 0)')
                $('#cattechnology').css({ backgroundColor: 'rgb(62, 146, 195)' });
            else if ($('#cattechnology').css('backgroundColor') == 'rgb(62, 146, 195)')
                $('#cattechnology').css({ backgroundColor: 'rgb(71, 166, 222)' });
        }
        else if ($(this).attr('id') == "entertainment") {
            $('#dropEntertainment').fadeToggle();
            if ($('#catentertainment').css('backgroundColor') == 'rgb(71, 166, 222)' || $('#catentertainment').css('backgroundColor') == 'rgba(0, 0, 0, 0)')
                $('#catentertainment').css({ backgroundColor: 'rgb(62, 146, 195)' });
            else if ($('#catentertainment').css('backgroundColor') == 'rgb(62, 146, 195)')
                $('#catentertainment').css({ backgroundColor: 'rgb(71, 166, 222)' });
        }
        else if ($(this).attr('id') == "sports") {
            $('#dropSports').fadeToggle();
            if ($('#catsports').css('backgroundColor') == 'rgb(71, 166, 222)' || $('#catsports').css('backgroundColor') == 'rgba(0, 0, 0, 0)')
                $('#catsports').css({ backgroundColor: 'rgb(62, 146, 195)' });
            else if ($('#catsports').css('backgroundColor') == 'rgb(62, 146, 195)')
                $('#catsports').css({ backgroundColor: 'rgb(71, 166, 222)' });
        }

    });
    $('.dropBox').click(function (e) {
        e.stopPropagation();
    });
    $('#searchHelp').click(function (e) {
        $('#searchHelp').fadeOut("slow");
    });
    $('html').click(function () {
        $('.search').css({ backgroundColor: 'rgb(71, 166, 222)' });
        $('.category').css({ backgroundColor: 'rgb(71, 166, 222)' });
        //$('.recentButton').css({ backgroundColor: 'rgb(71, 166, 222)' })
        $('.dropBox').hide();
        $('#countryDrop').hide();
        $('#searchHelp').fadeOut("slow")
    });

});



function checkEnter(e) { //e is event object passed from function invocation

    var character //Code literal character code will be stored in this variable
    if (e && e.which) { //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which //character code is contained in NN4's which property
    }
    else {
        e = event
        characterCode = e.keyCode //character code is contained in IE's keyCode property
    }

    if (characterCode == 13) { //if character code is equal to ascii 13 (if enter key)
        search();
        return false; //return false to the event handler
    }
    else {
        return true //return true to the event handler
    }
}
function search() {
    if (document.getElementById('ctl00_txtSearch').value != '') {
        window.location.href = 'http://interceder.net/topic/' + document.getElementById('ctl00_txtSearch').value.replace('&', '%26').replace(' ', '-');
    }
}
function deleteTab(param) {
    document.getElementById('tab' + param).style.display = 'none';
    document.getElementById('del' + param).style.display = 'none';
    var ck = $.cookie('recentSearchList');
    ck = ck.replace("recentSearchList=", "");
    //            SetCookie('recentSearchList', ck.replace(param + ',', ''), { path: '/', expires: 365 });
    SetCookie("recentSearchList", ck.replace(param + ',', ''), "365");
    //SetCookie("recentSearchList", replaceAll(ck, '"' + param + '",', ''), "365");
}
function deleteHistory() {
    $('.tab1').fadeOut("slow");
    $('.del').fadeOut("slow");
    $('.clearHistory').html("There is no search history");
    //replaceAll(ck, param + ',', '')
    $.cookie('recentSearchList', '', { path: '/', expires: 365 });
    //SetCookie("recentSearchList1", "", "365");
}
function setView(param) {
    $.cookie('view', param, { path: '/', expires: 365 });
    search();
}

function setSort(param) {
    $.cookie('sort', param, { path: '/', expires: 365 });
    search();
}

function SetCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + cookieValue + ";expires=" + expire.toGMTString() + ";path=/";
}

function GetCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}

//        function replaceAll(text, strA, strB) {
//            while (text.indexOf(strA) != -1) {
//                text = text.replace(/ + strA + /i, strB);
//            }
//            return text;
//        }

jQuery(document).ready(function () {
    $(function () {
        $("ul.demo2").ytplaylist({ addThumbs: true, autoPlay: false, holderId: 'ytvideo2' });
        $('.videoMore').click(function (e) {
            $('.videosHidden').slideDown();
            $('.videoMore').hide();
        });
        $('.newsMore').click(function (e) {
            $('.newsHidden').slideDown();
            $('.newsMore').hide();
        });
        $('.topicsMore').click(function (e) {
            $('.topicsHidden').slideDown();
            $('.topicsMore').hide();
        });
        $('.tagsMore').click(function (e) {
            $('.tagsHidden').slideDown();
            $('.tagsMore').hide();
        });
    });


    //$('.h1').dropShadow({left: 1, top: 1});
});
function swapVid(id) {

    $("#vidDesc" + id).toggle();
    $("#vid" + id).toggle();
}
