$(function() {
	
	
	$('a[href^=http://]').attr('target', '_blank');
	
	if($('.frontpage').length) {
		$('#page').masonry({ singleMode: true, columnWidth: 210, itemSelector: '.post' });
	}
    
    $('#highscore_btn').toggle(
        function() {
            $('#highscore').animate({ height: '450px' }, 200);
        }, 
        function() {
            $('#highscore').animate({ height: '0px' }, 200);
        }
    );
   
   
   	
    get_combinations();
    update_highscore();
				
    
    $('#slideshow').slideshow();
    
    
  if ($('#highscore_dialog').length) {
    $('#highscore_dialog').dialog({
		bgiframe: true,
		modal: true,
		width:280,
		height:350,
		autoOpen: false,
		title:"Lagre poengsum",
		buttons: {
			Lagre: function() {
				dialog = $(this);
				
				$('#highscore_form').find('input:visible').each(function() {
					if($(this).val() == "") {
						$(this).addClass('ui-state-error');
					} else {
						$(this).removeClass('ui-state-error');
					}
				});
				
				if($('.ui-state-error').length == 0) { 
					$.ajax({
						url: '/slots/register',
						data: $('#highscore_form').serialize(),
						dataType:'html',
  						success: function(html) {
				   		 	if(html == "1") {
								dialog.dialog('close');  
								update_highscore();	
				   	 		}
				   	 	}
					});
				}
				
			}
		}
	});
	}
	
  $('#project_create_ref').bind('blur', function (e) {
    var value = $(this).val();
    
    if (!value.length) return;
    
    $.ajax({
      method:   'get',
      url:      '/intra/project/ajax_check_ref',
      dataType: 'json',
      data:     'ref=' + value,
      success:  function(r) {
        console.log(r);
        $('#project_create_ref_msg').html(r.message);
      }
    });
   
	} );

});

$.fn.slideshow = function() {
	
	var slideshow = $(this);
	var images = $(this).find('li');
	
	$(this).find('li:not(:first)').hide();
	
	var current_image = images.filter(':visible');
	var current_index = 0;
	var navigation = $('<ul>').attr('id', 'slideshow_navigation');
	var next = $('<li>').html('<a href="#" id="slideshow_next">&#x25B6;</a>');
	var prev = $('<li>').html('<a href="#" id="slideshow_prev">&#x25C0;</a>');
	
	
	if(images.length > 1) {
		navigation.append(prev);
		
		images.each(function(i){
			$(this).addClass('slideshow_image_'+i);
			navigation.append($('<li>').html('<a href="#" class="slideshow_navigation btn_'+i+'" data-image="'+i+'">'+(i+1)+'</a>'));
		});
	
		navigation.append(next);
		slideshow.after(navigation);
		
		$('.slideshow_navigation:first').addClass('active');
		
		$('.slideshow_navigation').bind('click', function(ev) {
			images.filter(':visible').hide();
			$(".slideshow_image_"+$(this).attr('data-image')).show();
			
			$('.slideshow_navigation').removeClass('active');
			$(this).addClass('active');
			
			current_index = $('.slideshow_navigation').index($(this));
			
			return false;	
		});
		
		
		$('#slideshow_next').bind('click', function(ev) {
			if($('.btn_'+(current_index+1)).length) {
				current_index++;
				$('.btn_'+current_index).trigger('click');
			}
			return false;
		});
		
		$('#slideshow_prev').bind('click', function(ev) {
			if($('.btn_'+(current_index-1)).length) {
				current_index--;
				$('.btn_'+current_index).trigger('click');
			}
			return false;
		});
	}
};

function open_register_dialog() {
	$('#highscore_dialog').dialog('open');
}

function update_highscore() {
	$.ajax({
		url: '/slots/highscore',
		data: '',
		dataType:'html',
  		success: function(html) {
			$('#highscore_list').html(html);
		}
	});
}

function get_combinations() {
	var xml;
	
	$.ajax({
		url: '/flash/images.xml',
		data: '',
		dataType:'xml',
  		success: function(response_xml) {
			xml = response_xml;
			parse_xml(xml);
		}
	});
}

function fadein() {
    $('body').fadeIn();
    if($('.frontpage').length) {
		$('#page').masonry({ singleMode: true, columnWidth: 210, itemSelector: '.post' });
	}
}

function parse_xml(xml) {
	var output = $('<div>');
	var combos = [];	
	$xml = $(xml);
	
	
	$xml.find('combo').each(function() {
		combos.push({ combo: $(this).attr('chevrons').split(" "), score: $(this).attr('score') });
	});
	
	$.each(combos, function(combo_i, combo_hash) {
		var ul = $('<ul>');
		
		$.each(combo_hash.combo, function(chevron_i, chevron) {
			if(chevron != "*") {
				var src = $xml.find('reel:eq('+chevron_i+')').find('chevron[idx='+chevron+']').attr('src');
			} else {
				var src = '/flash/gfx/joker.png';
			}
			
			var li = $('<li>');
			li.append($('<img>').attr({'src': src, 'class':'combo_img'}));
			ul.append(li);
		});
		ul.append('<li><span class="score">'+combo_hash.score+'</span></li>');
		output.append(ul);
	});
	
	$('#combo_list').html(output);
}



