/*!

    Copyright (c) 2011 Peter van der Spek

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
 */(function(a){function m(){if(!d){d=!0;for(var c in b)a(c).each(function(){var d,e;d=a(this),e=d.data("jqae"),(e.containerWidth!=d.innerWidth()||e.containerHeight!=d.innerHeight())&&f(d,b[c])});d=!1}}function l(a){b[a]&&(delete b[a],b.length||c&&(window.clearInterval(c),c=undefined))}function k(a,d){b[a]=d,c||(c=window.setInterval(function(){m()},200))}function j(){return this.nodeType===3}function i(b){if(b.contents().length){var c=b.contents(),d=c.eq(c.length-1);if(d.filter(j).length){var e=d.get(0).nodeValue;e=a.trim(e);if(e==""){d.remove();return!0}return!1}while(i(d));if(d.contents().length)return!1;d.remove();return!0}return!1}function h(a){if(a.contents().length){var b=a.contents(),c=b.eq(b.length-1);return c.filter(j).length?c:h(c)}a.append("");var b=a.contents();return b.eq(b.length-1)}function g(b){var c=h(b);if(c.length){var d=c.get(0).nodeValue,e=d.lastIndexOf(" ");e>-1?(d=a.trim(d.substring(0,e)),c.get(0).nodeValue=d):c.get(0).nodeValue="";return!0}return!1}function f(b,c){var d=b.data("jqae");d||(d={});var e=d.wrapperElement;e||(e=b.wrapInner("<div/>").find(">div"));var f=e.data("jqae");f||(f={});var j=f.originalContent;j?e=f.originalContent.clone(!0).data("jqae",{originalContent:j}).replaceAll(e):e.data("jqae",{originalContent:e.clone(!0)}),b.data("jqae",{wrapperElement:e,containerWidth:b.innerWidth(),containerHeight:b.innerHeight()});var k=!1,l=e;c.selector&&(l=a(e.find(c.selector).get().reverse())),l.each(function(){var d=a(this),f=d.text(),j=!1;if(e.innerHeight()-d.innerHeight()>b.innerHeight())d.remove();else{i(d);if(d.contents().length){k&&(h(d).get(0).nodeValue+=c.ellipsis,k=!1);while(e.innerHeight()>b.innerHeight()){j=g(d);if(!j){k=!0,d.remove();break}i(d);if(d.contents().length)h(d).get(0).nodeValue+=c.ellipsis;else{k=!0,d.remove();break}}c.setTitle=="onEllipsis"&&j||c.setTitle=="always"?d.attr("title",f):c.setTitle!="never"&&d.removeAttr("title")}}})}var b={},c,d=!1,e={ellipsis:"...",setTitle:"never",live:!1};a.fn.ellipsis=function(b,c){var d,g;d=a(this),typeof b!="string"&&(c=b,b=undefined),g=a.extend({},e,c),g.selector=b,d.each(function(){var b=a(this);f(b,g)}),g.live?k(d.selector,g):l(d.selector);return this}})(jQuery)

var currImageNum = 0;
var timerOn = false;
var timer, timerlevel;
jQuery(window).load(function() {   
    var theWindow = jQuery(window);
    function resizeBg() {
        $bg = jQuery("#bgimage");
        $bg2 = jQuery("#bgimage2");
        aspectRatio = $bg.width() / $bg.height();
        aspectRatio2 = $bg2.width() / $bg2.height();
        if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
            $bg
                .removeClass()
                .addClass('bgheight');
            $bg2
                .removeClass()
                .addClass('bgheight');
        } else {
            $bg
                .removeClass()
                .addClass('bgwidth');
            $bg2
                .removeClass()
                .addClass('bgwidth');
        }
    }
    theWindow.resize(function() {
        resizeBg();
        setPosition();
    }).trigger("resize");
    $bg.click(hideContent);
    $bg2.click(hideContent);
    jQuery('#bgimage').addClass('act-image');
    jQuery('#bgimage2').addClass('hidden-image');
    if (jQuery('#level1 li').length > 5) jQuery('#level1 .arrowdown').show();
    if (jQuery('#level2 li').length > 5) jQuery('#level2 .arrowdown').show();
    if (jQuery('#level3 li').length > 5) jQuery('#level3 .arrowdown').show();
    setPosition();
    jQuery('#bgimage2').attr('src', getImage(1));
    window.setInterval('nextImage()', slideInterval);
    if (jQuery.trim(jQuery('#insidestrip').text()) != '' && typeof(nocontent) == 'undefined') {
        showContent();
//        jQuery('#cont').draggable();
//         jQuery('#submenu a').each(function(i) {
//             var chunks = this.href.split('#');
//             var id = '#submenu';
//             if (chunks.length > 1) id = '#' + chunks[1];
//             jQuery(this).click(function() {  
//                 var pos = jQuery(id).position().top + jQuery('#contentstrip').scrollTop();
//                 jQuery('#contentstrip').scrollTop(pos);
//                 hideTopLinks();
//                 return false;
//             });
//         });
//        hideTopLinks();
    }
});



function hideTopLinks() {
    return;
    jQuery('.csc-linkToTop').each(function(i) {
        //if (jQuery(this).position().top > 500 || jQuery(this).position().top < 20) jQuery(this).hide();
    });
}
function hideContent() {
    jQuery('#cont').hide('slow');
    jQuery('#togglecontent').show();
}

function showContent() {
    jQuery('#cont').show('slow');
    jQuery('#togglecontent').hide();
}

function setPosition() {
    var over = 0;
    var w_width = jQuery(window).width();
    //jQuery('#logo h1 a').text(w_width);
    if (w_width < 1070) {
        jQuery('#menu #level3 li').css({width: '160px'}); 
    }
    else if (w_width < 1150) {
        jQuery('#menu #level3 li').css({width: '240px'}); 
    }
    else if (w_width < 1230) {
        jQuery('#menu #level3 li').css({width: '320px'}); 
    }
    else if (w_width < 1350) {
        jQuery('#menu #level3 li').css({width: '400px'}); 
    }
    else if (w_width < 1450) {
        jQuery('#menu #level3 li').css({width: '500px'}); 
    }
    else if (w_width < 1550) {
        jQuery('#menu #level3 li').css({width: '600px'}); 
    }
    else {
        jQuery('#menu #level3 li').css({width: '700px'}); 
    }
    jQuery('#level3 li').ellipsis({setTitle: true});
    if (jQuery(window).height() < 635) {
        jQuery('#cont').css({top: '0px', bottom: 'auto'});
        jQuery('#menu').css({top: '530px', bottom: 'auto'});
        over++;
    }
    else {
        jQuery('#cont').css({bottom: '105px', top: 'auto'});
        jQuery('#menu').css({top: 'auto', bottom: '20px'});
    }
    if (w_width < 1020) {
        jQuery('#address').css({left: '715px', right: 'auto'});
        jQuery('#languagech').css({left: '990px', right: 'auto'});
        jQuery('#cont').css({left: '390px', right: 'auto'});
        over++;
    }
    else {
        jQuery('#address').css({left: 'auto', right: '0px'});
        jQuery('#languagech').css({left: 'auto', right: '10px'});
        jQuery('#cont').css({left: 'auto', right: '125px'});
    }
    if (w_width < 895) {
        jQuery('#cont').css({left: 'auto', right: '0px'});
    }
    else if (w_width < 1020) {
        jQuery('#cont').css({left: '390px', right: 'auto'});
    }
    if (over > 0) jQuery('html').css('overflow', 'auto');
    else jQuery('html').css('overflow', 'hidden');
}

function nextImage() {
    setImageBig();
}

function getImage(num) {
    var images = slideimages.split(',');
    if (images.length <= 1) {
        return;
    }
    currImageNum += num;
    if (currImageNum < 0) currImageNum = images.length - 1;
    if (currImageNum >= images.length) currImageNum = 0;
    var src = '/uploads/media/' + images[currImageNum];
    return src;
}

function setImageBig() {
    var actId = jQuery('.act-image').attr('id');
    var hiddenId = jQuery('.hidden-image').attr('id');
    jQuery('#' + actId).animate({
        "opacity" : "0"
    }, slideSpeed, slideEffect, function() {
        jQuery('#' + hiddenId).addClass('act-image');
        jQuery('#' + hiddenId).removeClass('hidden-image');
        jQuery('#' + actId).addClass('hidden-image');
        jQuery('#' + actId).removeClass('act-image');
        jQuery('#' + actId).attr('src', getImage(1));
        jQuery('#' + actId).css("opacity", "100");
    });
}

function menuscroll(id, num) {
    if (num == 1) {
        if (id == 'contentstrip') {
            
        }
        else {
            jQuery('#' + id + ' li:visible:first').hide('slow');
            jQuery('#' + id + ' .arrowup').show();
            if (jQuery('#' + id + ' li:visible').length <= 5) jQuery('#' + id + ' .arrowdown').hide();
        }
    }
    else if (num == -1) {
        if (id == 'contentstrip') {
        }
        else {
            jQuery('#' + id + ' li:hidden:last').show('slow');
            jQuery('#' + id + ' .arrowdown').show();
            if (jQuery('#' + id + ' li:hidden').length == 0) jQuery('#' + id + ' .arrowup').hide();
        }
    }
    else if (num == 0) {
        if (timerOn) {
            window.clearInterval(timer);
            timerOn = false;
        }
    }
    else if (num < 0) {
        timerlevel = id;
        if (!timerOn) timer = window.setInterval('menuscrolldown()', 50);
        timerOn = true;
    }
    else if (num > 0) {
        timerlevel = id;
        if (!timerOn) timer = window.setInterval('menuscrollup()', 50);
        timerOn = true;
    }
}

function menuscrollup() {
    if (timerlevel == 'contentstrip') {
        jQuery('#cont .arrowup').show();
        var t = jQuery('#contentstrip').scrollTop() + 5;
        if (t > jQuery('#insidestrip').height() - 685) {
            if (timerOn) {
                window.clearInterval(timer);
                timerOn = false;
            }
            jQuery('#cont .arrowdown').hide();
        }
        jQuery('#contentstrip').scrollTop(t);
    }
    else menuscroll(timerlevel, 1);
}

function menuscrolldown() {
    if (timerlevel == 'contentstrip') {
        jQuery('#cont .arrowdown').show();
        var t = jQuery('#contentstrip').scrollTop() - 5;
        if (t <= 5) {
            if (timerOn) {
                window.clearInterval(timer);
                timerOn = false;
            }
            jQuery('#cont .arrowup').hide();
        }
        jQuery('#contentstrip').scrollTop(t);
    }
    else menuscroll(timerlevel, -1);
}

