/// <reference path="jquery-vsdoc.js" />

/*
using titio.global.js
using jquery.js
using jquery.cycle.all.min.js
using jquery.dd.js
using gallery.js
*/

/**
*   CHECK IF JAVASCRIPT IF OF
*/

$(document).ready(function () {
    $('.hideDueJavaScript').removeClass('hidden');
});


/**
*   using jquery.cycle.all.min.js
*/
$(document).ready(function () {
    $('#mainHPphotos').cycle({
        //fx: 'scrollDown',
        //fx: 'turnDown',
        fx: 'fade',
        pause: 1,
        speed: 3600,
        timeout: 7000,
        pager: '#nav'
        //slideExpr: 'div'
    });
});


jQuery(document).ready(function () {
    jQuery('#articleCarousel').jcarousel({
        scroll: 6
        //start: 1
    });
});

$(document).ready(function () {
    var no = $("[id^='generalPartner']").length; // start with string
    //alert(no);
    for (i = 1; i <= no; i++) {
        $('#generalPartner' + i).cycle({
            fx: 'scrollUp',
            pause: 1,
            speed: 1000,
            timeout: 5000
        });
    }
});


/**
* using jquery.dd.js
*/
$(document).ready(function (e) {
    try {
        reloadCombos();
    } catch (e) {
        alert(e.message);
    }
});

function reloadCombos() {
    try {
        $("body select").msDropDown();
        $("#switchLang .ddTitle").width(100);
        $("#switchLang .ddChild").width(106);

        $("#CountryID2 .ddTitle").width(200);
        $("#CountryID2 .ddChild").width(206);

        $("#TournamentID .ddTitle").width(200);
        $("#TournamentID .ddChild").width(206);

        $("div[id^='TeamCategoryID'] .ddTitle").width(200);
        $("div[id^='TeamCategoryID'] .ddChild").width(206);

        $("#TeamLevel .ddTitle").width(200);
        $("#TeamLevel .ddChild").width(206);

        $("#teamRole .ddTitle").width(100);
        $("#teamRole .ddChild").width(106);

        $("#holdingStick .ddTitle").width(100);
        $("#holdingStick .ddChild").width(106);

        $("#accomoCategory .ddTitle").width(200);
        $("#accomoCategory .ddChild").width(206);

        $("#typeMatch .ddTitle").width(200);
        $("#typeMatch .ddChild").width(206);

        $("#transferType .dd").width(222);
        $("#transferType .ddTitle").width(200);
        $("#transferType .ddChild").width(206);

        $("#accomoRequir .ddTitle").width(260);
        $("#accomoRequir .ddChild").width(266);

        $("#transfersTimer .ddTitle").width(50);
        $("#transfersTimer .ddChild").width(56);

        $("#typeMatch .dd").width(222);
        $("#typeMatch .ddTitle").width(200);
        $("#typeMatch .ddChild").width(206);
        //alert("combosOK");
    } catch (e) {
        // alert("error: " + e.Description );
    }
}



$(document).ready(function () {
    $('a.lightbox').lightBox(); // Select all links with lightbox class
});




function switchLanguage(lang) {

    var currLang = lang;
    var selectedLangCont = document.getElementById('selectedLang');
    var selectedLang = selectedLangCont.options[selectedLangCont.selectedIndex].value;


    //alert("lang: " + selectedLang + ", currLang: " + currLang);

    var currHost = window.location.host;
    var currPath = window.location.pathname;
    //alert("host: " + currHost + ", path: " + currPath);
    var newPath;

    var filtr = new RegExp("^/" + currLang); // start with "/cz..."

    if (currPath.match(filtr)) {
        newPath = currPath.replace(filtr, '/' + selectedLang);
    } else { // this will be only na homepage in czech language
        newPath = currPath.replace("/", "/" + selectedLang);
    }

    var newLocation;

    newLocation = "http://" + currHost + newPath;
    //alert("newLocation: " + newLocation);

    window.location = newLocation;
    return true;


}



/* 
Gallery
using gallery.js
*/
function article_imageopen(imageid, elementid) {
    document.getElementById('img_' + elementid + '_main').src = "/image/itemid-" + imageid + "/w-466/h-340/q-85/" + imageid + ".jpg";
    document.getElementById('perex_' + elementid + '_a').innerHTML = document.getElementById('articles_hidden_' + elementid + '_' + imageid).innerHTML;
    document.getElementById('img_' + elementid + '_main_a').href = "/image/itemid-" + imageid + "/mw-800/mh-600/q-95/" + imageid + ".jpg";

    //Lightbox = Class.create();


    //    document.getElementById('item_' + elementid + '_' + articleimage).className = 'gallery-item';
    //    articleimage = imageid;
    //    document.getElementById('item_' + elementid + '_' + articleimage).className = 'gallery-item active';
}


/* 
Gallery
using gallery.js
*/
function article_click(elementid) {
    var id = 'articles_hidden_' + elementid + '_' + articleimage;
    var fireOnThis = document.getElementById(id);

    if (document.createEvent) {
        var evObj = document.createEvent('MouseEvents');
        evObj.initEvent('click', true, false);
        fireOnThis.dispatchEvent(evObj);
    }
    else if (document.createEventObject) {
        fireOnThis.fireEvent('onclick');
    }

    return false;
}

/*
input form
*/

/*
Pools
*/
function votePoll(answ, questid) {
    // set id of answer
    $("#answer").attr('value', answ);
    //alert("answ: " + answ);
    //alert("#" + questid);
    // send form
    $("#" + questid).submit()
}

function redirectToCategory() {
    var url = window.location.href;
    index = url.indexOf("#");
    tag = url.substring(index);
    if (index == -1) {
        $('#TeamMenuCategory a:first').click();
    } else {
        strlength = tag.length;
        tagCat = tag.substring(0, 4);

        //alert("url: " + url + " index: " + index + " tag: " + tag + " length: " + strlength);
        $('a[href="' + tagCat + '"]').click();
        window.setTimeout(function () {
            $('a[href="' + tag + '"]').click();
            //alert("xxx");
        }, 1000);

    }
}


function getCategoryResult(homefolder, categoryid, type, lang) {

    switch (type) {
        case "results":
            var url = "/?template=/System/Templates/Results/CategoryResults&homefolder=" + homefolder + "&categoryid=" + categoryid + "&lang=" + lang
            break;
        case "matches":
            var url = "/?template=/System/Templates/Schedules/CategoryResults&homefolder=" + homefolder + "&categoryid=" + categoryid + "&lang=" + lang
            break;
        case "resultsplayoff":
            var url = "/?template=/System/Templates/Results/CategoryResultsPlayOff&homefolder=" + homefolder + "&categoryid=" + categoryid + "&lang=" + lang
            break;
    }

    resp = $.ajax({
        type: "GET",
        url: url,
        success: function (data) {
            $('#resultsOfGroups').html(data);
        }
    });
    
    if (type != 'resultsplayoff'){
    groupmenu = $.ajax({
        type: "GET",
        url: "/?template=/System/Templates/Results/GroupList&homefolder=" + homefolder + "&categoryid=" + categoryid + "&type=" + type + "&lang=" + lang,
        success: function (data) {
            $('#TeamMenuGroup').html(data);
        }
    });
    
 }
    updateCategoryMenu(categoryid);
   
}


function updateCategoryMenu(categoryid) {
    $('#TeamMenuCategory span[id*="linkCategory"]').removeClass('activeMenuCategory');
    $('#TeamMenuCategory span[id="linkCategory-' + categoryid + '"]').addClass('activeMenuCategory');
}

function getGroupResult(categoryid, groupid, type, lang) {
    switch (type) {
        case "results":
            var url = "/?template=/System/Templates/Results/GroupResult&groupid=" + groupid + "&categoryid=" + categoryid + "&lang=" + lang
            break;
        case "matches":
            var url = "/?template=/System/Templates/Schedules/GroupResult&groupid=" + groupid + "&lang=" + lang
            break;
        case "resultsplayoff":
            //var url = "/?template=/System/Templates/Results/GroupResult&groupid=" + groupid + "&categoryid=" + categoryid + "&lang=" + lang
            break;
            
    }
    res = $.ajax({
        type: "GET",
        url: url,
        success: function (data) {
            $('#resultsOfGroups').html(data);
        }
    });

    updateGroupMenu(groupid);
}

function updateGroupMenu(groupid) {
    $('#TeamMenuGroup span[id*="linkGroup"]').removeClass('activeMenuGroup');
    $('#TeamMenuGroup span[id="linkGroup-' + groupid + '"]').addClass('activeMenuGroup');
}
