$(document).ready(function(){

    $('.objectImages a').click(function(e) {
        e.preventDefault();
        var uid = $(this).attr('rel');
        $('.enlargedImage').hide();
        $('.enlargedImageContainer span.image'+uid).show();
        $('.enlargedImageContainer').show();
    });
    $('.enlargedImageContainer .enlargedImage').click(function(e) {
        e.preventDefault();
        $('.enlargedImageContainer').hide();
    });
    $('.closeEnlargedImageContainer').click(function(e) {
        e.preventDefault();
        $('.enlargedImageContainer').hide();
    });


    $('.deleteLink').click(function() {
        if (confirm('Sind Sie sicher?')) {
            return true;
        }
        return false;
    });
    $('form[name="tx_constructionsign_controller_pi1_indexForm"]').submit(function() {
        var agb_checkbox = $('input[name="tx_constructionsign_controller_pi1[agb_check]"]');
        if (agb_checkbox.attr('checked')) {
            return true;
        } else {
            alert('Bitte akzeptieren Sie unsere AGB!');
            return false;
        }
    });


    /*
     * Upload
     */
    if ($('#fotoplus ol').length > 0) {
        $('#fotoplus ol li').attr('class', function(index) {
            return 'item-' + index;
        });
    }
    if ($('#uploadSubmitter').length > 0) {
    	$('#uploadSubmitter.submit').hide();

    	$('#uploadSubmitter').click(function() {
    		$('#uploadOverlay').css('display', 'block');
    		if (!window.innerWidth && !window.innerHeight) {
    			$('#uploadOverlay').css('height', document.body.offsetHeight);
    			$('#uploadOverlay').css('width', document.body.offsetWidth);
    		}
    	});
    }

    if ($('#uploadedFiles').length > 0) {
        var uploadedFiles      = $('#uploadedFiles').val();
        var uploadedFilesArray = uploadedFiles.split(',');

        $('#upload > .uploadFormWrapper').hide();
        switch (uploadedFilesArray.length) {
            case 1:
            case 2:
            case 3:
                for (var i=0; i<uploadedFilesArray.length; i++) {
                    switch (uploadedFilesArray[i]) {
                        case '':
                        case 'IMG_DIM_ERROR':
                            $('#upload > .wrapper' + (i+1)).show();
                            break;
                        default:
                            $('#upload > .wrapper' + (i+1)).hide();
                            $('#upload > .wrapper' + (i+2)).show();
                    }
                }
                break;
            case 0:
            default:
                $('#upload > .wrapper1').show();
        }
    }


    $('.conditionsLink').click(function (e) {
		e.preventDefault();
		$('#modalContainer').modal({
			onShow: loadModalContent($(this).attr('href')),
			onClose: function (dialog) {
				dialog.data.hide();
				dialog.container.hide();
				dialog.overlay.hide();
				// must call this to have SimpleModal re-insert the data correctly and clean up the dialog elements
				$.modal.close();
			}
		});
	});

});

function displayImage(uid) {

}


function openCenteredWindow(url,winName,winWidth,winHeight,winParams)	{
    // coords to center popup
    var newX, newY;
    newX = (screen.width - winWidth) / 2;
    newY = (screen.height - winHeight) / 2;

    // check user resolution to center the popup
    if (screen.width > winWidth) {
        if (screen.height > winHeight) {
            newWinParams = winParams + ',width=' + winWidth + ',height=' + winHeight + ',left=' + newX + ',top=' + newY;
        }
        else {
            newWinParams = winParams + ',width=' + winWidth + ',height=' + screen.height + ',left=' + newX + ',top=0,scrollbars=yes';
        }
    }
    else {
        if (screen.width <= winWidth) {
            if (screen.height <= winHeight) {
                newWinParams = winParams + ',width=' + screen.width + ',height=' + screen.height + ',left=0,top=0,scrollbars=yes';
            }
            else {
                newWinParams = winParams + ',width=' + screen.width + ',height=' + winHeight + ',left=0,top=' + newY + ',scrollbars=yes';
            }
        }
    }

    var ppWindow = window.open(url,winName,newWinParams);
	if (ppWindow) {
        ppWindow.focus();
    }

    return ppWindow;
}

function loadModalContent(url) {
	var mC = $('.modalContent');
	$(mC).html('<iframe src="'+url+'" height="374" width="664" frameborder="0" scrolling="no"></iframe>');
}
function closeModal() {
    $.modal.close();
}


function submitUpload() {
        // set action to upload instead of create
    $('input[name="tx_constructionsign_controller_pi1[action]"]').val('upload');
        // submit form
    $('#uploadSubmitter').click();
}


