jQuery(function($) {
	var postfix = '_on';
	$('.rollover a img').not('[src*="'+ postfix +'."]').each(function() {
		var img = $(this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.'))
		           + postfix
		           + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		img.hover(function() {
			img.attr('src', src_on);
		}, function() {
			img.attr('src', src);
		});
	});
});

function addFigure(str) {
	var num = new String(str).replace(/,/g, "");
	while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	return num;
}

function getTotal(){
	var total = 0;
	$('span.spn').each(function(){
		var st = $(this).text();
		st = Number(st.replace(',',''));
		total += st;
	});
	return total;
}

function is_smartphone () {
  var useragents = [
    'iPhone',         // Apple iPhone
    'iPod',           // Apple iPod touch
    'Android',        // 1.5+ Android
	'Mobile',        // 1.5+ Android
    'dream',          // Pre 1.5 Android
    'CUPCAKE',        // 1.5+ Android
    'blackberry', 　　// blackberry
    'webOS',          // Palm Pre Experimental
    'incognito',      // Other iPhone browser
    'webmate'         // Other iPhone browser
  ];
  for (var i = 0; i<useragents.length; i++){
	if(navigator.userAgent.indexOf(useragents[i])>0){
		return true;
	}
  }
  return false;
}

function setCurrency(){
	var tanka = Number($(this).parent().parent().find('.tanka').text().replace(',',''));
	var shokei = $(this).parent().parent().find('.sp').find('.spn');
	var shokei_input = $(this).parent().parent().find('.sp').find('input.cpn');
	var itemcnt = 0;
	itemcnt = Number($(this).val());
	if(itemcnt == 'NaN' || itemcnt == 'null' || itemcnt < 0  || !itemcnt) itemcnt = 0;
	
	var shokei_val = tanka * itemcnt;
	shokei.text(addFigure(shokei_val));
	shokei_input.val(addFigure(shokei_val));
	
	var totalPrice = String(getTotal());
	$('span#currency').text(addFigure(totalPrice));
}

