 
$(document).ready(function () {
	$('#add_to_cart').removeAttr('onclick');

	$('#add_to_cart').click(function () {
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/callback',
			dataType: 'json',
			data: $('#product :input'),
			success: function (data) {
				if(data.status){
				var image = $('#image').offset();
				var cart  = $('#module_cart').offset();
	
				$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
	
				params = {
					top : cart.top + 'px',
					left : cart.left + 'px',
					opacity : 0.0,
					width : $('#module_cart').width(),  
					heigth : $('#module_cart').height()
				};		
	
				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});	
				  $('#module_cart .middle').html(data.output);
				}
				else{
				  alert(data.output);
				}
			}			
		});			
	});	
	
    $('img').each(function(i){
	     this.id=this.id.replace(/[^\w\-]/g,"");
		 this.name=this.name.replace(/[^\w\-]/g,"")
	});
	$('a').each(function(i){
	     //this.name=this.name.replace(/^(\w)$/,"");
		 this.name=this.name.replace(/[^\w\-]/g,"");
		 this.id=this.id.replace(/[^\w\-]/g,"");
	});
	$('div').each(function(i){
	     //this.id=this.id.replace(/^(\w)$/,"");
		 this.id=this.id.replace(/[^\w\-]/g,"");
	});
	/*$('.product_options').each(function(i){
	     //this.id=this.id.replace(/^(\w)$/,"");
		 var path=$(this).attr('path');
		 path=path.replace(/[^\w\-]/g,"");
		 $(this).removeAttr('path');
		 $(this).attr('path',path);
	});*/
	//$('.button_add_small').removeAttr('onclick');
	//$(document.getElementsByName('button_add_small')).removeAttr('onclick');

	$("a[ajaxname='button_add_small']").removeAttr('onclick');

	//$('.button_add_small').click(function () {
	//$(document.getElementsByName('button_add_small')).click(function () {

   

	$("a[ajaxname='button_add_small']").click(function () {
	
		var divID=this.name;
		//alert(divID);

	    var optionlength=$('#'+divID).find('.product_options').children().size();
		var n=true;
         if($('#'+divID).find('.product_options')){
		      $('#'+divID).find('.product_options').each(function(){
			  if(this.value==0){
				   n=false;
			  }			
			});
			if(!n){
			  return;
			}
		 }
             var imageId='image'+$(this).attr('name');
			 var jimageId='#image'+$(this).attr('name');
			 if(1){
				$.ajax({
						type: 'post',
						url: 'index.php?route=module/cart/callback',
						dataType: 'json',
						data:$('#'+$(this).attr('name')+' :input'),
						success: function (data) {
							if(data.status){
								$('#module_cart .middle').html(data.output);
									var image=_getEPlace(document.getElementById(imageId));
										 
										 var cart  = _getEPlace(document.getElementById('module_cart'));

										$(jimageId).before('<img  src="' +$(jimageId).attr('src') + '" id="temp" style="position: absolute; top: ' + image['t'] + 'px; left: ' + image['l'] + 'px;" />');

										params = {
											top : cart['t'] +'px',
											left : cart['l']+'px',
											opacity : 0.0,
											width : $('#module_cart').width(),  
											heigth : $('#module_cart').height()
										};		
										   
										$('#temp').animate(params, 'slow', false, function () {
											$('#temp').remove();
										});
										}
								else{
									alert(data.output);
								}
						}		
					});
				}//½áÊøif	
			});

	
});

function _getEPlace(e){
	var offset={'t':e.offsetTop,'l':e.offsetLeft};

	if(e.offsetParent!=null) {
		var tl=_getEPlace(e.offsetParent);

		offset['t']+=tl['t'];
		offset['l']+=tl['l'];
	}
	return offset;
}



function calculatePrice(obj){
          var price=obj.value;
          alert(price);


} 

function addFrame(frameId){
    $.ajax({
			type: 'post',
			url: 'index.php?route=product/product/addFrame',
			dataType: 'json',
			data: 'id='+frameId,
			success: function (data) {
			}			
		});
}
