$(document).ready(function(){

    //defaultni nastaveni inner podle vysky okna prohlizece
    /*
    jQuery("#insert").click(function(){
        jQuery("#text").show();
    });
    */

    $('#searchForm').submit(function() {
       
        $("#pBottom").css('position', 'relative');
        $('#pInner').css('min-height',600);
        $('#pInnerContent').hide();
        $('#pInnerContentCorner').hide();
        $('#pInnerLoading').show();
        $('#pOpacity').show();
        $('#hpLayer').hide();
        //run the ajax

        var search = "search&q="+$("#searchInput").val();

        getPage(search);
        setBottom();

        return false;
    });
    
    $('#pInnerLoading').css("left",($("#pInner").width()/2) - ($('#pInnerLoading').width()/2) );
    //Check if url hash value exists (for bookmark)
    $.history.init(pageload);

    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('selected');

    //Seearch for link with REL set to ajax
     
    $('a[rel=ajax]').click(function () {

        //grab the full url
        var hash = this.href;

        //remove the # value
        hash = hash.replace(/^.*#/, '');

        //for back button
        //$.history.load(hash);

        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');

        //hide the content and show the progress bar
        $("#pBottom").css('position', 'relative');
        $('#pInner').css('min-height',600);
        $('#pInnerContent').hide();
        $('#pInnerContentCorner').hide();
        $('#pInnerLoading').show();
        $('#pOpacity').show();
        $('#hpLayer').hide();
        //run the ajax

        //getPage(hash);
        setBottom();
    //cancel the anchor tag behaviour
    //return false;
    });

    //default height of flash on HP
    //$("#PublicisBG").height(3000);

});

function setBottom() {
    //alert($("#pContent").height()+$("#pTop").height()+" " +$(document).height());

    if (($("#pContent").height()+$("#pTop").height()) < $(document).height()) {
        $("#pBottom").css('position', 'absolute');
        $("#pBottom").css('bottom', '0px');
    }

}

function pageload(hash) {
    //if hash value exists, run the ajax

    if (!hash) {
        //pokud neni zadny hash, tak bez na HP
        hash = "/homepage";
    }

    if (hash) {
        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');

        //hide the content and show the progress bar
        $("#pBottom").css('position', 'relative');
        $('#pInner').css('min-height',600);
        $('#pInnerContent').hide();
        $('#pInnerContentCorner').hide();
        $('#pInnerLoading').show();
        $('#pOpacity').show();
        $('#hpLayer').hide();
        $('#flashLayer').hide();
        $('#hpLayer').css('visibility', 'hidden');

        getPage(hash);
        setBottom();
    }
}

function getPage(hash) {

    //generate the parameter for the php script
    
    if (hash) {
        var data = 'page='+ hash;
        var menuhash = hash;
    } else {
        var data = 'page=' + document.location.hash.replace(/^.*#/, '');
        var menuhash = document.location.hash.replace(/^.*#/, '');

    }

    $.ajax({
        url: "/ajaxLoader",
        type: "POST",
        data: data,
        cache: false,
        success: function (html) {

            //hide the progress bar
            $('#pInnerLoading').hide();
            //$("#pInnerContent").css('padding','30px');
            //add the content retrieved from ajax and put it in the #content div
            $('#pInnerContent').html(html);

            //display the body with fadeIn transition
            $('#pInnerContent').fadeIn('slow');
            //$('#pInnerContentCorner').fadeIn('slow');
            if (hash.indexOf('reference') == -1) {
                //$('#pInnerContent').removeClass('noPadding');
                $//('#pInnerContent').removeClass('backgroundTransparent');
                //$('#pInnerContentCorner').show();
            } else {
                $('#pInnerContent').addClass('backgroundTransparent');
                $("#pInnerContent").removeClass('borderRight');
                $("#referenceDetailTop").addClass('borderRight');
            }

            cufonReplace();

            if (menuhash == '/homepage') {
                $("#flashLayer").show();
                $('#hpLayer').css('visibility', 'visible');
                $('#hpLayer').show();
                $("#pInnerContent").addClass('noPadding');
                $("#pInnerContent").addClass('backgroundTransparent');
                $("#pOpacity").hide();
                $('#pInner').css('min-height',100);
                $("#pInnerContentCorner").hide();
                $("#pInnerContent").removeClass('borderRight');
            } else if (hash.indexOf('reference') == -1) {
                //$("#pInnerContent").removeClass('backgroundTransparent');
                //$("#pInnerContent").addClass('borderRight');
            }
 //alert($("#PublicisBG").height());
 //alert($(document).height()+" "+$(window).height());
            //$("#PublicisBG").height($(document).height());
            $("#PublicisBG").height(2500);

             

            $('a[rel=ajaxin]').click(function () {

                //grab the full url
                var hash = this.href;

                //remove the # value
                hash = hash.replace(/^.*#/, '');

                //for back button
                //$.history.load(hash);

                //clear the selected class and add the class class to the selected link
                $('a[rel=ajax]').removeClass('selected');
                $(this).addClass('selected');

                //hide the content and show the progress bar
                $("#pBottom").css('position', 'absolute');
                $('#pInnerContent').hide();
                $('#pInnerContentCorner').hide();
                $('#pInnerLoading').show();
                $('#hpLayer').hide();
                //run the ajax

                getPage(hash);
                setBottom();
            //cancel the anchor tag behaviour
            });


            checkMediaContainer();

             

        }
    });
}

/**
  * check media container to click left or right
  */
function checkMediaContainer() {
    $("#pMediaLeft").click(function(){
        slideMediaContainer('left');
        return false;
    });

    $("#pMediaRight").click(function(){
        slideMediaContainer('right');
        return false;
    });

    $(".otherBox .mediaImage").find("a").click(function(){
        showSpecificMedia($(this));
        return false;
    });

    showFirstMedia();
}
 
function showFirstMedia() {
    var curMedia = $("#pReferenceMediaSettings").attr('curmedia');
    if (curMedia) {
        //call media ajax
        $("#pReferenceMediaContent").addClass('loading');
        $("#pReferenceMediaContent").html(' ');

        var data = 'media='+curMedia;

        $.ajax({
            url: "/mediaLoader",
            type: "POST",
            data: data,
            cache: false,
            success: function (html) {
                $("#pReferenceMediaContent").removeClass('loading');
                $("#pReferenceMediaContent").html(html);
            }
        });
    }

}

function showSpecificMedia(a) {
    var curMedia = a.attr('mediaid');

    $("#pReferenceMediaSettings").attr("curmedia",curMedia);

    //call media ajax
    $("#pReferenceMediaContent").addClass('loading');
    $("#pReferenceMediaContent").html(' ');

    var data = 'media='+curMedia;

    $.ajax({
        url: "/cms_dev.php/mediaLoader",
        type: "POST",
        data: data,
        cache: false,
        success: function (html) {
            $("#pReferenceMediaContent").removeClass('loading');
            $("#pReferenceMediaContent").html(html);
        }
    });
}

/**
 * slide media container, call media ajax
 */
function slideMediaContainer(slideTo) {
    var curMedia = $("#pReferenceMediaSettings").attr('curmedia');
    var allMedia = $("#pReferenceMediaSettings").attr('allmedia');
    var mediaArray = allMedia.split(",");
    var newCurMedia;

    if (slideTo == 'left') {
        for (i = 0; i < mediaArray.length; i++) {
            if (mediaArray[i] == curMedia) {
                if (i == 0) {
                    newCurMedia = mediaArray[mediaArray.length - 1];
                } else {
                    newCurMedia = mediaArray[i - 1];
                }
            }
        }
    } else if (slideTo == 'right') {

        for (i = 0; i < mediaArray.length; i++) {
            if (mediaArray[i] == curMedia) {
                if (i == (mediaArray.length - 1)) {
                    newCurMedia = mediaArray[0];
                } else {
                    newCurMedia = mediaArray[i + 1];
                }
            }
        }

    }

    $("#pReferenceMediaSettings").attr("curmedia",newCurMedia);

    //call media ajax
    $("#pReferenceMediaContent").addClass('loading');
    $("#pReferenceMediaContent").html(' ');

    var data = 'media='+newCurMedia;
    
    $.ajax({
        url: "/cms_dev.php/mediaLoader",
        type: "POST",
        data: data,
        cache: false,
        success: function (html) {
            $("#pReferenceMediaContent").removeClass('loading');
            $("#pReferenceMediaContent").html(html);
        }
    });

}

function cufonReplace() {

    Cufon.replace('#pSearchBox .lang', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
    Cufon.replace('h2', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
    Cufon.replace('h3', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
    Cufon.replace('.pRights', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
    Cufon.replace('.pBlock', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
    Cufon.replace('.topHead', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
    Cufon.replace('.perexNews', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
    Cufon.replace('.borderDiv', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
    Cufon.replace('#bottomNews', {
        fontFamily: 'Berkeley Old Style Itc T OT'
    });
}

function autoHeight(autoHeight) {
    var minHeight = $("#pInner").css('min-height');
    var m = parseInt(minHeight.replace('px',''));

    //pouze pokud je predpokladana vyska vetsi nez min. vyska
    if ( ($("#pInnerContent").height() > m) || (autoHeight) ) {
        var newHeight = $("#pInner").height()+$("#pInnerContent").height();
        var heightToChange = "";
        if (newHeight < m) {
            heightToChange = m;
        } else heightToChange = newHeight;
        
        $("#pInner").css('height',heightToChange);
    } else {
        $("#pInner").css('height',m);
    }
}


