$(function(){

	$("#playVideo").click(function(){
		
		$.colorbox({
			html: $('#videoCode').html(),
			transition: 'fade',
			innerWidth: '640px',
			innerHeight: '390px'		
		});
		
		return false;
		
	});
	
	$("#playVideo2").click(function(){
		
		$.colorbox({
			html: $('#videoCode2').html(),
			transition: 'fade',
			innerWidth: '640px',
			innerHeight: '390px'		
		});
		
		return false;
		
	});
	
	$('div.gallery a, a.modal').colorbox({
		width: '80%',
		height: '80%',
		scrolling: false,
		current: "image {current} of {total}",
		preloading: true,
		slideshow: true,
		slideshowSpeed: 3000,
		slideshowAuto: false,
		transition:'fade'
	});
	
	$('div.testimonial a, table.testimonial a').colorbox({
		iframe: true,
		innerWidth: '960px',
		innerHeight: '600px',
		rel: 'testimonialLink',
		scrolling: false,
		current: "testimonial {current} of {total}",
		preloading: true,
		transition:'fade'
		
	});
	
	$('#OutfitSearchForm').submit(function(){
		
		$.post(
			$(this).attr('action'), 
			$(this).serialize(),
			function(data){
				
				if (data == 'not found') {
					alert('Sorry, no outfit has been found with that code.')
				} else {
					window.location = '#!' + data;
				}
				
				
			}
		);
		
		return false;
		
	});
	
	$ajaxBoxClose = function(){
		
		newLoc = window.location.toString();
		hashPos = newLoc.indexOf("#!", 0);
		
		newLoc = newLoc.substring(0, hashPos);		
		
		window.location = newLoc + '#!';
		
	}

	$('.favourite a').live('click', function(){
	
		id = $(this).attr('rel');
		cookie = $.cookie('chof_favs');
		
		if(id) {
		
			if (cookie == null) {
				$.cookie('chof_favs', id, { path: '/', expires: 365});				
			} else {
				
				vals = cookie.split('|');
				pos = $.inArray(id, vals);
				
				if (pos <= 0) {
					cookie = cookie + '|' + id;
				} else {
					
				}
				
				$.cookie('chof_favs', cookie, { path: '/', expires: 365});				
			}			
		}
		
		$(this).text('This outfit is now in your favourites').addClass('addedFav');		
		
		$("div.box.favourites").load('/outfits/favourites_box', function(){
			$('div.box.favourites').fadeTo('fast', 1);
		});		
		
		$.get('/outfits/count_fav/' + id);
		
		return false;		
	});
	
	$('a.removeFromFavs').live('click', function(){
	
		id = $(this).attr('rel');
		cookie = $.cookie('chof_favs');
		
		if(id) {
		
			if (cookie == null) {
				$.cookie('chof_favs', id, { path: '/', expires: 365});				
			} else {
				
				vals = cookie.split('|');				
				pos = $.inArray(id, vals);
				
				vals.splice(vals.indexOf(id), 1);
				
				cookie = vals.join('|');
				
				$.cookie('chof_favs', cookie, { path: '/', expires: 365});				
				
				
			}			
		}	

		$("div.box.favourites").load('/outfits/favourites_box', function(){
			$('div.box.favourites').fadeTo('fast', 1);
		});

		$(this).parent().remove();
		
		return false;		
	});
	
	$('a.clearFavs').live('click', function(){
		
		if (confirm('Are you sure you wish to clear your favourite outfits?')) {
		
			$.cookie('chof_favs', null, { path: '/', expires: 365});
			$("div.box.favourites").load('/outfits/favourites_box', function(){
				$('div.box.favourites').fadeTo('fast', 1);
			});
				
		}
				
		return false;
				
	});
	
	$(window).hashchange(function(){
		
    	url = location.hash;
		if (url.indexOf('#!') > -1) {
		
			newUrl = url.replace('#!', '');

			if (newUrl) {			
		
				$.colorbox({
					iframe: false,
					href: newUrl,
					transition: 'fade',
					onClosed: $ajaxBoxClose,
					innerHeight: '640px'
				
				});		
			} else {
				$.colorbox.close();
			}	
		}
		
	});
	
	$(window).hashchange();
	
});


