$(document).ready(function() {
    $("a[rev='AJAX'][href*=#AJAX]").each(function() {
        var urlSplit = this.href.split('#');
        this.href = urlSplit[0];
        var temp = urlSplit[1].split('?')[1].split('&');
        var attribs = new Array();

        for (i = 0; i < temp.length; i++) {
            split = temp[i].split('=');
            attribs[split[0]] = split[1];
        }
        
        this.onclick = function() {
            if (attribs['action'] && attribs['id']) {
               window[attribs['action']](attribs['id']);
            } else if (attribs['action']) {
               window[attribs['action']]();
            }

            return false;
        };
    });
});
 
function ajaxErrorHandler(XMLHttpRequest, textStatus, errorThrown) {
    window.location = '/';            
}

function addToStorage(storageId) {
	$.ajax({
		type: 'GET',
		url: '?switch=storage&action=addToStorageAjax&offerId=' + storageId,
		cache: false,
		async: false,
		error: ajaxErrorHandler,
		success: function(data, textStatus) {
			tb_alwaysImage = 0;
			$("#storage_modList").html(data);
			
			if(data.search('storageProductInfo') == -1) {
				height = 120;
			} else {
				height = 190;
			}
			
            if (data.search('li class="warning"') != -1)
            {
                 height += 45;
            }
			
			tb_show(null, '#TB_inline?border=1&height=' + height + '&width=500&inlineId=storageAjaxResponse', false);
		}
	});  
}

function addToCompare(offerId) {
    addToCompareConfirm(offerId, 1);
}

function addToCompareConfirm(offerId, confirm) {
	$.ajax({
		type: 'GET',
		url: '?switch=compare&action=addToCompareAjax&offerId=' + offerId + '&confirm=' + confirm,
		cache: false,
		async: false,
		error: ajaxErrorHandler,
		success: function(data, textStatus) {
			tb_alwaysImage = 0;
			$("#compare_modList").html(data);

			if(data.search('confirmCompare') != -1) {
				tb_show(null, '#TB_inline?border=1&height=140&width=400&inlineId=confirmCompare', false);
				
				$('#confirmCompareYesButton').click(function () { 
					addToCompareConfirm(offerId, 0);
			   });
			} else {
				if(data.search('compareProductInfo') == -1) {
					height = 120;
				} else {
					height = 190;
				}

				tb_fastRemove();
				
				tb_show(null, '#TB_inline?border=1&height=' + height + '&width=500&inlineId=compareAjaxResponse', false);
			}
		}
	}); 
}

function addToBasket(offerId) {
	$.ajax({
		type: 'GET',
		url: '?switch=basket&action=addToBasketAjax&addToBasket=' + offerId,
		cache: false,
		async: false,
		error: ajaxErrorHandler,
		success: function(data, textStatus) {
			tb_alwaysImage = 0;
			$("#basket_modBasket").html(data);
			
			if(data.search('storageProductInfo') == -1) {
				height = 120;
			} else {
				height = 190;
			}
			
			tb_show(null, '#TB_inline?border=1&height=' + height + '&width=500&inlineId=basketAjaxResponse', false);
		}
	});
}

function getReviewForm(offerId) {
    $.ajax({
        type: 'GET',
        url: '/?switch=product&action=getReviewForm&pid=' + offerId,
        cache: false,
        async: false,
        error: ajaxErrorHandler,
        success: function(data, textStatus) {
            $('.reviewForm .reviewFormContent').html(data);
        }
    });
}

function getReviewFormAppend(offerId, uid) {
    $.ajax({
        type: 'GET',
        url: '/?switch=product&action=getReviewForm&uid='+uid+'&pid=' + offerId,
        cache: false,
        async: false,
        error: ajaxErrorHandler,
        success: function(data, textStatus) {
            $('#reviewForms').append('<div class="reviewForm"><div class="reviewFormContent" id="reviewFormContent_'+offerId+'">'+data+'</div></div>');
        }
    });
}

function getReviewFormPost(offerId)
{
	myData = $('#addReviewAgitowcy_'+offerId+' .cokupicAjaxForm').serialize();
	$.ajax({
		type: 'POST',
		url: '/',
		cache: false,
		async: false,
		data: myData,
		error: ajaxErrorHandler,
		success: function(data, textStatus) {
            $('.reviewForm #reviewFormContent_'+offerId).html(data);
		}
	});
}