/**
 * PDF zum Bestellzettel hinzufügen
 * @param item
 */
function addItemToDownloadsCart(item) {
    jQuery.getJSON('downloadCartService.php', {action: 'add', item: item}, function(data) {
        for(var i = 0; i < data.length; i++) {
            $('#'+data[i]['id']).html(data[i]['value']);
        }
    });
}

function removeItemFromDownloadsCart(item, mode) {
    jQuery.getJSON('downloadCartService.php', {action: 'remove', item: item, mode: mode}, function(data) {
        for(var i = 0; i < data.length; i++) {
            $('#'+data[i]['id']).html(data[i]['value']);
        }
    });
}

function showDialog(id, cb) {
    width = (id == 'image_layer') ? 716 : 694;
    dialogClass = (id == 'image_layer') ? 'image_dialog' : '';
    if($('#'+id).length == 0) {
            $('body').append('<div id="'+id+'"></div>');
            var options = {
                autoOpen: false,
                modal: true,
                resize: false,
                width: width,
                title: false,
                draggable: false,
                stack: true,
                closeText: '',
                dialogClass: dialogClass,
                height: 'auto',
                open: function(){closedialog=1;$(document).bind('click', function(){if(closedialog){$('#'+id).dialog('close');}closedialog=1;});},
                focus: function(){closedialog = 0;},
                close: function(){$(document).unbind('click');},
                position: ['center',175]
            }
            if($.browser.msie && $.browser.version=='6.0') {
                // IE6 does not work well with modal in this case:
                options.modal = false;
            }
            $('#'+id).dialog(options);
        } else {
            $('#'+id+'_content').html('');
            //$('#'+id).dialog('open');
        }
        dialog = $('#'+id).dialog('open');
        closedialog = 0;
        var htmlContent = '';
        jQuery.post('index.php', {"layer:id": id, template: '99_layer.html'}, function(data) {
            htmlContent = data;
            if($.browser.msie && $.browser.version=='6.0') {
            // IE6 does not work well with modal in this case:
            //htmlContent = '<div id="'+id+'_content"></div>';
            }
            $('#'+id).html(htmlContent);
            if(typeof cb == 'function') {
                cb();
            }
        });
//        var htmlContent = '<div class="close_label"><a href="#" onClick="$(\'#'+id+'\').dialog(\'close\');return false;">schliessen <img src="project/media/images/layer_close.gif" /></a></div>'
//            +'<div id="'+id+'_content"></div>';
        
}

function showDownloadsCart(mode) {
        showDialog('download_cart_layer', function() {
            getDownloadsCartContent(mode);
        });
	//contentNode = document.getElementById("overlayContainerContent");
	//contentNode.innerHTML = '<div style="width:100%; text-align:center; margin:auto; margin-bottom:50px;"><img src="project/media/images/loading.gif" border="0" /></div>';
	
}

function showImage(file) {
    showDialog('image_layer', function() {
        $('#image_layer_content').html('<img src="'+file+'" />');
    });
}

function getDownloadsCartContent(mode) {
    jQuery.getJSON('downloadCartService.php', {action: 'get', mode: mode}, function(data) {
        for(var i = 0; i < data.length; i++) {
            $('#'+data[i]['id']).html(data[i]['value']);
        }
    });
}

function updateDownloadsCart(item, num, mode) {
    jQuery.getJSON('downloadCartService.php', {action: 'update', item: item, num: num, mode: mode}, function(data) {
        for(var i = 0; i < data.length; i++) {
            $('#'+data[i]['id']).html(data[i]['value']);
        }
    });
}

function showDownloadCartForm() {
    showDialog('download_cart_layer', function() {
        displayContent('index.php?pageID=cartform&template=11_cart_form_single.html', 'download_cart_layer_content');
    });
}

$(function() {
    jQuery.cookie = function(name, value, options) {
	    if (typeof value != 'undefined') { // name and value given, set cookie
	        options = options || {};
	        if (value === null) {
	            value = '';
	            options.expires = -1;
	        }
	        var expires = '';
	        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
	            var date;
	            if (typeof options.expires == 'number') {
	                date = new Date();
	                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
	            } else {
	                date = options.expires;
	            }
	            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
	        }
	        // CAUTION: Needed to parenthesize options.path and options.domain
	        // in the following expressions, otherwise they evaluate to undefined
	        // in the packed version for some reason...
	        var path = options.path ? '; path=' + (options.path) : '';
	        var domain = options.domain ? '; domain=' + (options.domain) : '';
	        var secure = options.secure ? '; secure' : '';
	        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	    } else { // only name given, get cookie
	        var cookieValue = null;
	        if (document.cookie && document.cookie != '') {
	            var cookies = document.cookie.split(';');
	            for (var i = 0; i < cookies.length; i++) {
	                var cookie = jQuery.trim(cookies[i]);
	                // Does this cookie string begin with the name we want?
	                if (cookie.substring(0, name.length + 1) == (name + '=')) {
	                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	                    break;
	                }
	            }
	        }
	        return cookieValue;
	    }
	};

    $("#layer").dialog({
		modal: true,
		width: 600,
		draggable: false,
		autoOpen: false
	});
	$("#layer a.button").click(function(){
	    $('input:checkbox[name=save_choice]').val($(this).attr('data-choice'))
	    $.cookie("save_choice", $('input:checkbox[name=save_choice]:checked').val(), {expires: 30});
	});
	
	$('#subject_first a').click(function(){
	    $('#layer').dialog('open');
	    return false;
	});
});


function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 2000, function() {
            $active.removeClass('active last-active');
        });
}

var initSlideShow = function() {
    if($('#slideshow').length == 1) {
        // nur einmal wechseln:
        //setTimeout('slideSwitch()', 4000);
        setInterval('slideSwitch()', 6000 );
    }
}

$(document).ready(function() {
    initSlideShow();
});
