/*
Function used in specific pages. for example: designs/index uses -> designsIndex()
Uses jquery.general-functions.js
*/

function adminDesigns()
{
	$('#TileamountTileamount').change(function(e)
	{
		var selected = $('#TileamountTileamount').val();
		var currentUploads = $('.uploadField');
		var template = '<div class="input file"><label for="photo_normal_id_">Afbeelding voor _name_</label><input class="uploadField" type="file" name="data[photo_normal][_id_]" id="photo_normal_id_" /></div>';
		
		$.each(currentUploads, function(index, value) { 
		
			currentUploads.parent().remove();
			
		});

		for(i in selected)
		{
			var tempstring = template;
			tempstring = tempstring.replace('_id_',selected[i]);
			tempstring = tempstring.replace('_id_',selected[i]);
			tempstring = tempstring.replace('_id_',selected[i]);
			
			var text = '';
			$("#TileamountTileamount option").each(function()
			{
				if(this.value == selected[i])
			    text = this.text;
			});

			
			tempstring = tempstring.replace('_name_',text);
			currentHtml= $('#fileInputs').html();
			currentHtml+=tempstring;
			$('#fileInputs').html(currentHtml );
			
		}
	});
	
}


function designsIndex () {
	var baseHeight = 0;
	// in photo icon show belonging assignments
	$('.designs > .design > .design_data > .show_assignments').click(function(){
		createOverlay({'style':'width:702px; margin-left:-361px;'},{'style':'width:600px'});
		$('#in_overlay').load($(this).attr('href'));
		
		return false;
	});
	
	$('#design_slider ul').live('mouseout',(function(e){
		$('#arrowLeft').css('display','none');
		$('#arrowRight').css('display','none');
	}));
		
	$('#design_slider').live('mousemove',(function(e){
		
		offset = (document.getElementById('design_slider').offsetLeft + document.getElementById('overlay').offsetLeft);
		if(e.pageX - offset < 34)
		{
			
			$('#arrowLeft').css('display','block');
			$('#arrowRight').css('display','none');
		}
		else if(e.pageX - offset > 466 && e.pageX - offset <500)
		{
			$('#arrowLeft').css('display','none');
			$('#arrowRight').css('display','block');
		}
		else
		{
			$('#arrowLeft').css('display','none');
			$('#arrowRight').css('display','none');
		}
	}));
		
	$('.designs > .design > .design_image > a').click(function(){
		createOverlay({'style':'width:792px; margin-left:-401px;'},{'style':'width:690px'});
		
		$.ajax({
		  url: $(this).attr('href'),
		  context: document.body,
		  success: function(data){
		  	$('#in_overlay').html(data);
			
		  	baseHeight=$(".rightFloatContainer").height()
			current_img = $('#design_slider li.first');
			if(current_img.height()==0)
			{
				height = 500;
			}
			else
			{
				height = current_img.height();
			}
			var height = Math.max(baseHeight,height);
			$('#design_slider').height(height);
			
		  }
		});


		
		return false;
	});
	
	
	$("#arrowLeft").live('click',function(e)
	{
		
		
		current_img = $('#design_slider li img:visible');
		current = current_img.parent();
		next = current.prev('li');
		
		if(next.length==0)
		{
			next = $('#design_slider li.last');
		}

		$(".nav a.shown").removeClass('shown');
		$(".nav #show_"+next.attr('id')).addClass('shown');
		
		current.children('img').animate({opacity: 0.0}, {"duration": 200, complete:function(){
		current.hide();
		
		
		next.show();
		
		var height = Math.max(baseHeight,next.find('img').height());
		
		$('#design_slider').height(height);
		next.children('img').animate({opacity: 1.0});
	
		}});
		return false;
		
	});
	
	$("#arrowRight").live('click',function(e)
	{
		current_img = $('#design_slider li img:visible');
		current = current_img.parent();
		next = current.next();
		first = $('#design_slider li:first');
		if(next.length==0)
		{
			next= first;
		}
		
		$(".nav a.shown").removeClass('shown');
		$(".nav #show_"+next.attr('id')).addClass('shown');
		
		current.children('img').animate({opacity: 0.0}, {"duration": 200, complete:function(){
		current.hide();
		
		
		next.show();
		
		var height = Math.max(baseHeight,next.find('img').height());
		
		$('#design_slider').height(height);
		next.children('img').animate({opacity: 1.0});
	
		}});
		return false;
		
	});
	
	$(".nav a").live('click',function(e)
	{
		$(".nav a.shown").removeClass('shown');
		$(this).addClass('shown');
		current_img = $('#design_slider li img:visible');
		current = current_img.parent();
	
		next = $($(this).attr('href'));
		
		current.children('img').animate({opacity: 0.0}, {"duration": 200, complete:function(){
		current.hide();
		
		
		next.show();
		
		var height = Math.max(baseHeight,next.find('img').height());
		
		$('#design_slider').height(height);
		next.children('img').animate({opacity: 1.0});
	
		}});
		return false;
	});
	
	
	
	/*
	// on hover show bigger image
	$('.designs > .design > .design_image > img').mouseover(function(){
		$(this).prev('.img_wrap').fadeIn('fast');
		
		$('.img_wrap a').click(function(){
			createOverlay({}, {});
			$('#in_overlay').html('<img src="'+$(this).attr('href')+'" border="0" />');
			return false;
		});
	});
	$('.designs > .design > .design_image > .img_wrap > a').mouseout(function() {
		$('.designs > .design > .design_image > .img_wrap').fadeOut('fast');
	});
	*/
}



function designsView () {
	$('a.next_assignment, a.prev_assignment').click(function(){
		// set vars
		goto_class = $(this).attr('class');
		current_assignment = $('#related_assignments .related_assignment:visible');
		next_assignment = current_assignment.next();
		prev_assignment = current_assignment.prev();
		first_assignment = $('#related_assignments .related_assignment:first');
		last_assignment = $('#related_assignments .related_assignment:last');
		// hide current
		current_assignment.fadeOut('fast', function(){
			// goto next item
			if (goto_class=='next_assignment') {
				if (next_assignment.length==0) {
					first_assignment.fadeIn('slow');
				}
				else{
					next_assignment.fadeIn('slow');
				}
			}
			// goto prev item
			else{
				if (prev_assignment.length==0) {
					last_assignment.fadeIn('slow');
				}
				else{
					prev_assignment.fadeIn('slow');
				}
			}
		});
		return false;
	});
}

function assignmentsView() {
	
	$('#thumbs_bar > #thumbs > a.thumb').click(function(){
		createOverlay({},{});
		$('#in_overlay').load($(this).attr('href'));
		return false;
	});
	
	$("body").everyTime(7000,function(i) {
		nextAssignmentHeader();
	});
	
	$('#assignment_design a#zoom_design').click(function(){
		createOverlay({'style':'top:900px'},{});
		$('#in_overlay').load($(this).attr('href'));
		return false;
	});
	
	$('a.next_design, a.prev_design').click(function(){
		// set vars
		goto_class = $(this).attr('class');
		current_assignment = $('#assignment_designs ul li:visible');
		next_assignment = current_assignment.next();
		prev_assignment = current_assignment.prev();
		first_assignment = $('#assignment_designs ul li:first');
		last_assignment = $('#assignment_designs ul li:last');
		// hide current
		current_assignment.fadeOut('fast', function(){
			// goto next item
			if (goto_class=='next_design') {
				if (next_assignment.length==0) {
					first_assignment.fadeIn('slow');
				}
				else{
					next_assignment.fadeIn('slow');
				}
			}
			// goto prev item
			else{
				if (prev_assignment.length==0) {
					last_assignment.fadeIn('slow');
				}
				else{
					prev_assignment.fadeIn('slow');
				}
			}
		});
		return false;
	});
}

function pagesViewHome() {
	
	$("body").everyTime(8000,function(i) {
		nextHomeHeader();
	});
	
	$(".block").click(function(event){
		
		 var $target = $(event.target);
		window.location.href = $target.closest('div.block').find('a').attr("href");
		return false;
	});
	
	// select #flowplanes and make it scrollable. use circular and navigator plugins
	$("#scroll_news").scrollable({ vertical: true, mousewheel: true, items: '.block', next: '.next_scroll', prev: '.prev_scroll' });
}

function newsitemsView() {
	$('#photo').click(function(){
		createOverlay({}, {});
		$('#in_overlay').html('<img src="'+$(this).attr('href')+'" border="0" />');
		return false;
	});
	
}

function adminDesignsIndex () {
	createTableSortable(".designs.index", 'admin/designs/order');
}

function adminAssignmentsIndex () {
	createTableSortable(".assignments.index", 'admin/assignments/order');
}

function adminFaqsIndex () {
	createTableSortable(".faqs.index", 'admin/faqs/order');
}

function adminNewsitemsEditAdd () {
	$("input[name='data[Newsitem][type]']").change(function(){
		type = $(this).val();
		$('#news_link, #news_text').hide();
		if (type=='link') {
			$('#news_link').show();
		}
		else{
			$('#news_text').show();
		}
	});
}




