jQuery(document).ready(function() {

    clickactions();

    jQuery(".photofancy").fancybox({
            'titlePosition'		: 'inside',
            'transitionIn'		: 'none',
            'transitionOut'		: 'none'
    });

    jQuery('.cartlink').click(function() {
        var cartpid = jQuery('#cartpid').val();
        jQuery('#gotocart').html('<a href="index.php?id=' + cartpid + '">zum Warenkorb</a>');
        jQuery('#fotodb_pi3 h1').html('Folgende Produkte sind in dem Warenkorb');
        var photoID = this.id;
        var optionID = jQuery('#imageoption_'+ photoID).val();
        jQuery.get("index.php?eID=photocart&tx_hsfotodb_ajaxcart[photoID]=" + this.id + 
                                          "&tx_hsfotodb_ajaxcart[optionID]=" + optionID +
                                          "&tx_hsfotodb_ajaxcart[cartPID]=" + cartpid,
            function(data){fillData(data)});
        return false;
    });

    jQuery('.countphoto').change(function() {
        var idAndOption = this.id;
        var arr = idAndOption.split('_');
        var photoID = arr[1];
        var optionID = arr[2];
        var newAmount = jQuery(this).val();
        var shippingCosts = jQuery('#shippingcosts').val();

        if(parseInt(newAmount)) {        
            jQuery.get("index.php?eID=photocart&tx_hsfotodb_ajaxcart[photoID]=" + photoID +
                                              "&tx_hsfotodb_ajaxcart[optionID]=" + optionID +
                                              "&tx_hsfotodb_ajaxcart[newAmount]=" + newAmount +
                                              "&tx_hsfotodb_ajaxcart[shippingCosts]=" + shippingCosts,
                function(data){fillData(data)});
        } else {
            alert("Dies ist keine gültige Anzahl.");
            jQuery(this).val("1");
            jQuery.get("index.php?eID=photocart&tx_hsfotodb_ajaxcart[photoID]=" + photoID +
                                              "&tx_hsfotodb_ajaxcart[optionID]=" + optionID +
                                              "&tx_hsfotodb_ajaxcart[newAmount]=1" +
                                              "&tx_hsfotodb_ajaxcart[shippingCosts]=" + shippingCosts,
                function(data){fillData(data)});
        }
    });
    
    function fillData(data){
        eval(data);
        clickactions();
    }

    function clickactions() {
        jQuery('.delphotofromsession').unbind('click');
        var shippingCosts = jQuery('#shippingcosts').val();
        jQuery('.delphotofromsession').click(function() {
            jQuery('#photocontainer_' + this.id).remove();
            jQuery.get("index.php?eID=photocart&tx_hsfotodb_ajaxcart[deletePhoto]=" + this.id +
                                          "&tx_hsfotodb_ajaxcart[shippingCosts]=" + shippingCosts,
                function(data){fillData(data)});
        });
    }
});
