
function validatePayment() {
	$("#continue_order").click(function(){
		var payment = $("#paytypeselect").attr('value');
		var transport = $("#transfertypeselect").attr('value');
		if(payment == 0 || transport == 0) {
			alert("Způsob platby a dopravy musejí být zvoleny.");
			return false;
		}
	});
}

function validate_form() {
	$('FORM#pay_form').submit(function(){
		var pole = new Array('fo_username','fo_lastname','fo_street','fo_lastname','fo_postcode','fo_phone','fo_email','paytypeselect','transfertypeselect');

		var errors =  {'fo_username':'Jméno',
									'fo_lastname':'Příjmení',
									'fo_street':'Ulice',
									'fo_postcode':'PSČ',
									'fo_phone':'Telefon',
									'fo_email':'E-mail',
									'paytypeselect':'Typ platby',
									'transfertypeselect':'Typ dopravy'
									};
		var error = false;
		var error_message = 'Opravte, prosím, následující položky:\n\n';
		for(var id in pole) {
			if($('#' + pole[id]).length > 0) {
				if( !$('#' + pole[id]).val() || $('#' + pole[id]).val() == 0 ) {
					error = true;
					error_message += errors[pole[id]] + "\n";
				}
			}
		}
		if(error) {
			alert(error_message);
			return false;
		}
		return true;	
	});
	
}

Shadowbox.loadSkin('classic', '/shadowbox');
Shadowbox.loadLanguage('cs', '/shadowbox');

$().ready(function(){
  Shadowbox.init();
  $('.promo-box .img-wrap a').pngFix();
  validatePayment();
  imeiRepeater();
	validate_form();
	xmas_banner_control();
	xperia_hover();
	remove_default();
	admin();
	item_add();
});

function item_add() {
	
	$('BODY').ajaxSend(function(){
		$('A[rel="item_add"], A[rel="item_remove"]').hide();
	});
	
	$("A[rel='item_add']").click(function(){
		$.get('f_editorder.php',{'count':item_count,'ajax':true},function(data,textStatus){
			$('#order_items TBODY').append(data);
			$('A[rel="item_add"], A[rel="item_remove"]:last').show();
			item_count++;
		});
		return false;
	});

	$("A[rel='item_remove']").live('click',function(){
		var item_count = $(this).attr('count');
		$('tr#item'+item_count).remove();
		item_count--;
		$('A[count="'+item_count+'"]').show();
		return false;
	});

	$("SELECT.phone_select").live('change',function(){
		var where_to_place_id = $(this).parent('td').parent('tr').attr('count');
		var price_to_assign = phone[$(this).val()]['price'];
		var dph_to_assign = phone[$(this).val()]['dph'];
		$('#item' + where_to_place_id + ' input.price').val(price_to_assign);
		$('#item' + where_to_place_id + ' input.dph').val(dph_to_assign);
	});

}

function remove_default() {
	$('INPUT.remove_default').focus(function(){
		
		if( $(this).attr('value') == $(this).attr('default_value') ) {
			$(this).attr('value','');
		}
		
	});
	$('INPUT.remove_default').blur(function(){
		
		if( $(this).attr('value') == '' ) {
			$(this).attr('value',$(this).attr('default_value'));
		}
		
	});

}

function admin() {
	$('A[rel=close_priority_table]').click(function(){
		$('.priority TABLE').toggle();
		return false;
	});
	
}

function xperia_hover() {
	/*
	$("#xperia").hover(function(e){
		alert('asd');
	});
	*/
	$("H1 A").mousemove(function(e){
		$(this).children('EM, IMG').css('top',e.clientY - $(this).offset().top + 20).css('left',e.clientX - $(this).offset().left);
  });
	
}

function xmas_banner_control() {
	
	var slide = $('#banner_slide');
	var slides = slide.children('DIV:first').children('.slide');
	
	slide.append('<a rel="banner_slide-close" class="banner_slide-close" href="#"></a>');
	if( slides.length > 1 ) {
		slides.eq(0).addClass('active');
		slide.append('<a rel="banner_slide-next" class="banner_slide-next" href="#"></a><a rel="banner_slide-prev" class="banner_slide-prev" href="#"></a>');	
	}
		
	$('A[rel="banner_slide-close"]').click(function(){
		slide.slideUp('slow');
		return false;
	});
	
	$('A[rel="banner_slide-next"]').click(function(){
		// currently active slide
		active = slides.filter('.active');
		// if there is mext slide
		if(active.next('.slide').length > 0) {
			// find next
			var next = active.next('.slide');
			var next_index = $('#banner_slide .slide').index(next);
			// mark and move
			slider_mark_active(next_index);
			slider_goto(next_index);			
			
		} else {
			// default mark and move
			slider_mark_active(0);
			slider_goto(0);
		}
		return false;
	});

	$('A[rel="banner_slide-prev"]').click(function(){
		// currently active slide
		active = slides.filter('.active');
		// if there is prev slide
		if(active.prev('.slide').length > 0) {
			// find prev
			var prev = active.prev('.slide');
			var prev_index = $('#banner_slide .slide').index(prev);
			// mark and move
			slider_mark_active(prev_index);
			slider_goto(prev_index);
		// otherwise go to the end
		} else {
			slider_mark_active(slides.length - 1);
			slider_goto(slides.length - 1);
		}
		return false;
	});	

}

function slider_mark_active(index) {
	$('#banner_slide .slide').removeClass('active').eq(index).addClass('active');
}

function slider_goto(index) {
	var block_width = $('#banner_slide .slide:first').width();
	$('#banner_slide DIV:first').animate({'left': - block_width * index + 'px'},500);
}

function imeiRepeater() {
	$('#imei-repeater-submit').click(function(){
		var repeat = $('#imei-repeater').attr('value');
		for(var i = 0; i < repeat; i++) {
			$('#imei-rep').before('<div class="form-item"><label>IMEI</label><input type="text" name="imei[]" /></div>');
		}

		$('#imei-rep').hide();
		$('#imei-rep').after('<input type="submit" value="Přidej do skladu" />');
		return false;
	});

	$('#imei-repeater2-submit').click(function(){
		var repeat = $('#imei-repeater').attr('value');
		for(var i = 0; i < repeat; i++) {
			$('#imei-rep').before('<div class="form-item"><label>IMEI</label><input type="text" name="imei[]" /></div>');
		}			
		$('#imei-rep').hide();
		$('#imei-rep').after('<input type="submit" value="Odepiš ze skladu" />');
		return false;
	});	
}
