
function gallery(timechange, tmefadeout)
{
	change = function()
	{
		var j = id;
		j++;
		if (j > (divs.size()-1))
		{
			j = 0;
		}
		$(divs[j]).css({ zIndex: j+id });
		$(divs[id]).css({ zIndex: j+id+1 });
		$(divs[j]).show();
		$(divs[id]).fadeOut(tmefadeout, function() {$(this).hide();});
		id = j;
	}

	var divs = $('a',$('#gallery'));
	var id = 0;
	divs.each( function(i) 
	{ 
		$(this).css({ zIndex: i + 1 });
	});
	$(divs[id]).show();
	setInterval("change()", timechange);
}


/* Function check / uncheck all items */
function toggleAll(cb, frm) {
	var val = cb.checked;
/*	var frm = document.category_manager;*/
	var len = frm.elements.length;
	var i=0;

	for (i=0; i<len; i++) {

		if (frm.elements[i].name=='check_ids[]') {
			frm.elements[i].checked=val;
		}

	}
}


/* Function for check if selected any item */
function check(frm, msg) {
/*	var frm = document.category_manager;*/
	var len = frm.elements.length;
	var i=0;

	if (!msg) {
		msg = 'Are you sure you want to delete selected item(s)?';
	}

	for (i=0; i<len; i++) {

		if (frm.elements[i].name == 'check_ids[]') {

			if (frm.elements[i].checked) {
				return confirm(msg);
			}

		}

	}

	alert('There is no items selected');
	return false;
}


function cart_add_item_submit(item_id) {	

	var item_count = 0;
	var item_price = 0;
	var case_count = 0;
	var case_price = 0;
	var total_price = 0;

	$.ajax({
		type: 'POST',
		url: 'basket/add_item/',
		data: 'action=add_item&item_id='+item_id+'&item_count='+$('#item_count_'+item_id).attr('value')+'&case_count='+$('#case_count_'+item_id).attr('value'),
		success: function(msg){
			cart_get_count_item();
			if (msg == '1') {

				item_count = intval($('#item_count_'+item_id).attr('value'));
				item_price = floatval($('#item_price_'+item_id).attr('value'));
				case_count = intval($('#case_count_'+item_id).attr('value'));
				case_price = floatval($('#case_price_'+item_id).attr('value'));
				total_price = case_price*case_count + item_price*item_count;

				$('#cart_name').html($('#item_name_'+item_id).html());
				$('#cart_single').html(item_count);
				$('#cart_case').html(case_count);
				$('#cart_price').html(item_price.toFixed(2));
				$('#cart_total').html(total_price.toFixed(2));

				$('#shopping_cart_detail').show('');
				setTimeout("$('#shopping_cart_detail').slideToggle('slow')",5000);

			} else {
				if (msg != '' && msg != '0') {
					alert(msg); 
				} else if (msg != '0') {
					alert("Whooops! There was an error while processing your request. Please try again later."); 
				}
			}
		}
	});

};


function cart_get_count_item() {	

	$.ajax({
		type: 'POST',
		url: 'basket/get_order_count_item/',
		data: '',
		success: function(msg){
			$('#count_items').html(msg);
		}
	});

};


function intval( mixed_var, base ) {  
    // Get the integer value of a variable using the optional base for the conversion    
    //   
    // version: 812.3015  
    // discuss at: http://phpjs.org/functions/intval  
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // +   improved by: stensi  
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // *     example 1: intval('Kevin van Zonneveld');  
    // *     returns 1: 0  
    // *     example 2: intval(4.2);  
    // *     returns 2: 4  
    // *     example 3: intval(42, 8);  
    // *     returns 3: 42  
    // *     example 4: intval('09');  
    // *     returns 4: 9  
    var tmp;  
  
    var type = typeof( mixed_var );  
  
    if(type == 'boolean'){  
        if (mixed_var == true) {  
            return 1;  
        } else {  
            return 0;  
        }  
    } else if(type == 'string'){  
        tmp = parseInt(mixed_var * 1);  
        if(isNaN(tmp) || !isFinite(tmp)){  
            return 0;  
        } else{  
            return tmp.toString(base || 10);  
        }  
    } else if(type == 'number' && isFinite(mixed_var) ){  
        return Math.floor(mixed_var);  
    } else{  
        return 0;  
    }  
}  


function floatval(mixed_var) {  
    // +   original by: Michael White (http://getsprink.com)  
    // %        note 1: The native parseFloat() method of JavaScript returns NaN when it encounters a string before an int or float value.  
    // *     example 1: floatval('150.03_page-section');  
    // *     returns 1: 150.03  
    // *     example 2: floatval('page: 3');  
    // *     returns 2: 0  
    // *     example 2: floatval('-50 + 8');  
    // *     returns 2: -50  
    return (parseFloat(mixed_var) || 0);  
}


// Function for updating region lookup after country selected
function update_region_lookup(country_id, lookup_place, shipping_type_id) {
	$.ajax({
		type: 'POST',
		url: 'basket/region-lookup/',
		data: 'country_id='+country_id+'&lookup_place='+lookup_place,
		success: function(msg){
			if (msg != '') {
				$('#' + lookup_place + '_region_id').html(msg);
				updateSelects();
			}
		}
	});
	if (lookup_place == 'shipping') {
		$.ajax({
			type: 'POST',
			url: 'basket/shipping-lookup/',
			data: 'country_id='+country_id+'&shipping_type_id='+shipping_type_id,
			success: function(msg){
				if (msg != '') {
					$('#shipping_type_id').html(msg);
					updateSelects();
				}
			}
		});
	}
}


// Function for updating winary lookup after region selected
function update_winary_lookup(country_id, region_id, lookup_place) {
	$.ajax({
		type: 'POST',
		url: 'admin/wine-store-manager/winary-lookup/',
		data: 'country_id='+country_id+'&region_id='+region_id,
		success: function(msg){
			if (msg != '') {
				$('#' + lookup_place + '_winary_id').html(msg);
			}
		}
	});
}


function alert_hide() {
	createCookie('alert_21','1',1000);
	$('#alert_bg').hide('');
	$('#alert_body').hide('');
}


function alert_show() {
	if (readCookie('alert_21') != '1') {
		$('#alert_bg').show('');
		$('#alert_body').show('');
	}
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function eraseCookie(name) {
	createCookie(name,"",-1);
}


// Function for sending e-mail after order submitting
function email_order_submit() {
	$.ajax({
		type: 'POST',
		url: 'basket/email-order-submit/',
		data: '',
		success: function(msg){
			if (msg == '1') {
				return true;
			} else {
				alert("Whooops! There was an error while processing your request. Please try again later.");
				return false;
			}
		}
	});
}
