//actions on document ready
$(document).ready(function(){
    initIE();
    initMenu();
    initSlideshow();
    initOKTv();
});

//initializing the slideshow ---------------------------------------------------
function initSlideshow(){
    $('#headline_slideshow').headline_slideshow({
        'speed': 5000,
        'transitionspeed': 400,
        'textheight': 50
    });
}

//initializes the video gallery OKTV -------------------------------------------
var oktv_newthumbholderwidth = 0;
var oktv_thumbscrollwidth = 0;
var oktv_thumbviewportwidth = 0;
var oktv_thumbscrolling = false;
function initOKTv(){
    var thumbcount = 0;
    oktv_thumbviewportwidth = $('#videothumbs').width();
    
    $('#videothumbsholder>a.thumb').each(function(){
        oktv_newthumbholderwidth += $(this).outerWidth(true);
        thumbcount++;
        $(this).click(function(){
            $('#oktv>iframe').attr('src',page_url+'serverscripts/showVideoPlayer.php?video_id='+$(this).attr('title'));
            return false;
        });
    });
    $('#videothumbsholder').width(oktv_newthumbholderwidth);
    
    oktv_thumbscrollwidth = oktv_newthumbholderwidth/thumbcount;
    
    $('#nextvideos').bind('mousedown',function(){
        if(parseInt($('#videothumbsholder').css('left')) > (oktv_thumbviewportwidth-oktv_newthumbholderwidth) && oktv_thumbscrolling == false){
            oktv_thumbscrolling = true;
            $('#videothumbsholder').animate({'left':'-='+oktv_thumbscrollwidth+'px'},500,'easeInOutQuint',function(){
                oktv_thumbscrolling = false;
            });
        }
    });
    
    $('#prevvideos').bind('mousedown',function(){
        if(parseInt($('#videothumbsholder').css('left')) < 0 && oktv_thumbscrolling == false){
            oktv_thumbscrolling = true;
            $('#videothumbsholder').animate({'left':'+='+oktv_thumbscrollwidth+'px'},500,'easeInOutQuint',function(){
                oktv_thumbscrolling = false;
            });
        }
    });
}

/* ----------- MENU ----------------------------------------------------------*/

//setstting styles for particular IE versions after pageload--------------------
function initIE(){
    if($.browser.msie && $.browser.version == '7.0'){
        $('#menuholder>ul>li').css('display','inline'); //for menu elements
        $('#footercontent>ul>li').css('display','inline'); //for footer elements
    }
}

//initializes the menu on pageload
function initMenu(){
    var menuholder = $('#menuholder>ul');
    var menulis = menuholder.children();
    menulis.each(function(){
       var element = $(this);
       element.children('ul').css({
           marginLeft: '-=50',
           width: '+=20',
           'background-position': ((element.width()-38)/2)+'px 0%',
           'border-bottom-right-radius': '8px',
           'border-bottom-left-radius': '8px'
       });
    });
    menuholder.empty();
    var first = true;
    menulis.each(function(){
        if(!first){
            menuholder.append('<li>|</li>');
        }else{
            first = false;
        }
        $(this).appendTo(menuholder);
    });
    menulis.hover(function(){menuL1Hover(this,'on');},function(){menuL1Hover(this,'off');});
}

//opens and closes the mainmenu elements
function menuL1Hover(menuelement,action){
    var elementobject = $(menuelement);
    var second_menulevel = elementobject.children('ul');
    
    if(action == "on"){console.log('on');
        elementobject.css('background-image','url('+page_url+'design/menuhover.png)');

        second_menulevel.animate({
            opacity: 'toggle',
            marginLeft: '+=50'
        },150,'easeInCubic');

    }else{
        elementobject.css('background-image','none');

        second_menulevel.animate({
            opacity: 'toggle',
            marginLeft: '+=50'
        },150,'easeInCubic',function(){
            $(this).css({marginLeft: '-=100'});
        });
    }
}

//shows the email address in contacts ------------------------------------------
function showContactMails(){
    $(document).ready(function(){
        var x = 'mailto:';
        var vidova = 'vidova';
        var grafik = 'grafik';
        var inzercia = 'inzercia';
        var admin = 'admin';
        var gabriel = 'gabriel';
        var z = '@';
        var y = 'okmagazine.sk';
        $('#vidova').attr('href',x+vidova+z+y).html(vidova+z+y);
        $('#grafik').attr('href',x+grafik+z+y).html(grafik+z+y);
        $('#inzercia').attr('href',x+inzercia+z+y).html(inzercia+z+y);
        $('#admin').attr('href',x+admin+z+y).html(admin+z+y);
        $('#gabriel').attr('href',x+gabriel+z+y).html(gabriel+z+y);
    });
}

//scrolling a page to article when the article is opened
function scrollPage(){
    $(document).ready(function(){
        window.scrollTo(0,180);
    });
}

/* ----------- SEARCH --------------------------------------------------------*/

//activating the search input
function activateSearch(element){
    if($(element).val() == 'Hľadaný výraz ...'){
        $(element).val('').css('color','#000000');
    }
}

//deactivating the search input
function deactivateSearch(element){
    if($(element).val() == ''){
        $(element).val('Hľadaný výraz ...').css('color','#aaaaaa');
    }
}

//checking the search string in search input
function checkSearch(form){
    var value = $(form).children('.searchstring').val();
    if(value == '' || value == ' ' || value == 'Hľadaný výraz ...'){
        return false;
    }else{
        return true;
    }
}

/* ----------- BOOKMARK ------------------------------------------------------*/

//adding the bookmark
function addBookmark(element){
    var anchor = $(element);
    var bookmarkUrl = anchor.attr('href');
    var bookmarkTitle = document.title;

    if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
    } else if(window.opera) {
        anchor.attr('title',bookmarkTitle);
    }else { // for other browsers which does not support
        alert('Záložka sa nepridala. Pokúste sa pridať záložku manuálne pomocou stlačenia klávesovej skratky Ctrl+D.');
    }
    return false;
}

// initializes the adslideshow gallery
function initGallery(){
    $(document).ready(function(){
        $('.ad-gallery').adGallery();
    });
}

//initializes the titulky gallery
function initTitulky(){
    $(document).ready(function(){
        $('#titulky>a>img').hover(function(){
            $(this).css({'margin-left':'-10px','margin-top':'-10px',width: '150px'});
        },function(){
            $(this).css({'margin-left':'0px','margin-top':'0px',width: '130px'});
        });
    });
}

//adds the ok background to article
function addOKBackground(){
    $(document).ready(function(){
        $('.article').css({
            'background-image': 'url(design/okbg.jpg)',
            'background-position': 'top left',
            'background-repeat': 'repeat',
            'margin-left': '20px',
            'margin-right': '20px'
        });
    });    
}

