

function getSelectedOptionId(){
   if(document.images) {
      var which = document.productForm;

      for(i=0;i<which.length;i++) {
         var tempobj=which.elements[i];
         if(tempobj.name.substring(0,7)=='options' || tempobj.name.substring(0,16)=='checkbox_options') {
            var fieldType = tempobj.type;
            if(fieldType=='select-one'){
               if(tempobj.selectedIndex > -1){
                  //var myval = tempobj[tempobj.selectedIndex].value;
                  var myval = tempobj.selectedIndex;
               	  return myval;
               }

            } else {
				//ignore
            }
         }
      }
   }
}


function getChildOptionElement(){
   if(document.images) {
      var which = document.productForm;

      for(i=0;i<which.length;i++) {
         var tempobj=which.elements[i];
         if(tempobj.name.substring(0,10)=='options[2]') {
            return tempobj;           
         }
      }
   }
}

function updatePrice(){
   var which = document.productForm;
   var myoptions = getSelectedOptions();
   var productId = which.productID.value;
   var extraField = '';
   
   if(which.extraField){
       var extraField = which.extraField.value;
   }

   x_getLiveProductTotal(myoptions, productId, extraField, '1', totalHandler);
}

function totalHandler(response){
   if(response){
   		document.getElementById('price').innerHTML = response;
   }
}

function getSelectedOptions(){
   var myoptions = '';

   if(document.images) {
      var which = document.productForm;

      for(i=0;i<which.length;i++) {
         var tempobj=which.elements[i];
         if(tempobj.name.substring(0,7)=='options' || tempobj.name.substring(0,16)=='checkbox_options') {
            var fieldType = tempobj.type;
            if(fieldType=='select-one'){
               if(tempobj.selectedIndex > -1){
                  var myval = tempobj[tempobj.selectedIndex].value;
               	  myoptions += ',' + myval;
               }

            } else if(fieldType=='radio' || fieldType=='checkbox'){
               if(tempobj.checked){
                  var myval = tempobj.value;
                  myoptions += ',' + myval;
               }

            } else {
		//ignore 
            }
         }
      }
   }

   return myoptions;
}

$(document).ready( function (){
	
	$(".zoom_image").click( function(){
		
		var img = $(this).parent().parent().parent().find('div.images_holder').find('div.mainimage_container');
		var imgzoom = $(this).parent().parent().parent().find('div.images_holder').find('div.imgholder_zoom');

		if($(img).css('display') == 'block'){
			$(img).css('display', 'none');
			$(imgzoom).css('display', 'block');
		}else{
			$(img).css('display', 'block');
			$(imgzoom).css('display', 'none');
		}

	});

	var large_width = $(".imgholder_zoom").width();
	var large_height = $(".imgholder_zoom").height();

	$(".imgholder_zoom").mousemove( function(e){
		//position of the mouse inside the element
		var x = e.pageX - $(this).position().left;
		var y = e.pageY - $(this).position().top;

		//percentage of the mouse to the element
		var pw = (x / large_width) * 100;
		var ph = (y / large_height) * 100;

		//width and heigh of the image
		var img = $(this).find('img');
		var iw = $(img).width();
		var ih = $(img).height();

		//get the overflow of the image
		var ow = iw - large_width;
		var oh = ih - large_height;

		var movew = (ow / 100) * pw;
		var moveh = (oh / 100) * ph;

		$(img).css({
			'margin-left' : '-'+Math.floor(movew)+'px',
			'margin-top'  : '-'+Math.floor(moveh)+'px'
			//position : 'relative'
		});
	});

});
