

function strpos (haystack, needle, offset) {
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Onno Marsman
	// +   bugfixed by: Daniel Esteban
	// +   improved by: Brett Zamir (http://brett-zamir.me)
	// *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
	// *     returns 1: 14

	var i = (haystack+'').indexOf(needle, (offset || 0));
	return i === -1 ? false : i;
}

function refreshSnippets(payload) {
	// redirect
	if (payload.redirect) {
		window.location.href = payload.redirect;
		return;
	}

	// snippets
	if (payload.snippets) {
		for (var i in payload.snippets) {
			updateSnippet(i, payload.snippets[i]);
		}
	}
}


function updateSnippet (id, html) {
	$("#" + id).html(html);
}

$(document).ready(function() {
    
	$('#no-js-error').hide();

        $('a.scroll_to_top').click(function(event)
	{
		event.preventDefault();
	
		$.scrollTo(0,0,{duration:500});
	});
        
	$("a#show-streets").mouseover(function(){
		$('div#hidden-streets-delivery').fadeIn('30');
	});
	$("a#show-streets").mouseout(function(){
		$('div#hidden-streets-delivery').fadeOut('500');
	});

	//delete links
	$("a.delete, input[name=delete]").live('click', function(e) {
		
		var text = $(this).html() ? $(this).html() : "Zmazať";
		
		if(confirm(text + " ?") != true) {
			return false;
		}
	});
	
	function setCursorByID(id,cursorStyle) {
		var elem;
		if (document.getElementById &&
			(elem=document.getElementById(id)) ) {
			if (elem.style) elem.style.cursor=cursorStyle;
		}
	}
	
	//global ajax setup
	$(document).ajaxStart(function() {

		setCursorByID('container', 'wait');

	}).ajaxComplete(function() {
		setCursorByID('container', 'default');
	});
	
	/*
	 * AJAX links
	 * types:
	 *		ajax-modal: open modal window
	 *		ajax:				only touch link over AJAX
	 */
	$('a.ajax-modal').openDOMWindow({
		eventType:'click',
		windowSource: 'ajax'
	});

	$('a.ajax').live('click', function(e) {
		e.preventDefault();
	
		$.get(this.href, null, function(payload) {
			refreshSnippets(payload);
		});
	});

	/**
 *forms select autocomplete
 */
	$('select.autocomplete').select_autocomplete();

	/**
 * add * to required field
 */
	$('form').find('label.required').each( function(index, label) {
		$(label).html( $(label).html() + ' <b class="asterisk">*</b>');
	});
	
	/*
	 * snippets hide after timeout
	 * 
	 * default timeout is 2s
	 */
	//	setTimeout(function() {
	//		$('#snippet--flashes').fadeOut();
	//	}, shupito.flashesTimeout ? shupito.flashesTimeout : 2000);
	
	/*
	 * switching product types prices
	 */
	$('.product_size').change(function() {
		
		$(this).closest('li.product').find('.price').hide().end().find('#for_' + $(this).val() ).show();
		$(this).closest('li.product').find('.bar a.ajax-add-product').hide().end().find('.for_' + $(this).val() ).show();
		
	});
	
	$('a.ajax-add-product').live('click', function(e) {
		e.preventDefault();
	
		var quantity = $(this).parent().find('input[name=quantity]').attr('value');
	
		$.get(this.href, {
			quantity: quantity
		}, function(payload) {
			refreshSnippets(payload);
		});
	});
	
	
	$('.modal-html').click(function(e) {
		e.preventDefault();
		
		$.openDOMWindow({
			//			windowSource:	'inline',
			windowSourceID: '#' + $(this).attr('modal-for')
		});
		
	});
	
	//prevent from overClicking forms
//	$('input[type=submit]').click(function() {
//		$(this).attr('disabled', 'disabled');
//	});

});	//document ready



	

