
$(document).ready(function(){
	// Navigation hover effects
	$lastActiveTab = null;
	$('#navigation li').mouseover(function(){
		$lastActiveTab = $('#navigation li.active');
		$('#navigation li').removeClass('hover');
		$('#navigation li').removeClass('active');
		$(this).addClass('hover');
	});
	$('#navigation li').mouseout(function(){
		$('#navigation li').removeClass('hover')
		
		if($lastActiveTab != null)
		{
			$lastActiveTab.addClass('active');
		}
	});
	
	$('#sub-navigation li').mouseover(function(){
		$lastActiveTab = $('#sub-navigation li.active');
		$('#sub-navigation li').removeClass('hover');
		$('#sub-navigation li').removeClass('active');
		$(this).addClass('hover');
	});
	$('#sub-navigation li').mouseout(function(){
		$('#sub-navigation li').removeClass('hover')
		if($lastActiveTab != null)
		{
			$lastActiveTab.addClass('active')
		}
	});

	
	// Decimal fix .. easiest way to do this.
	val = $('#id_price').val();
	
	if(val != null)
	{
		val = val.replace(',',' ');
		val = val.replace('.',',');
		$('#id_price').val(val);

		$('#form').submit(function(){
			newval = $('#id_price').val();
			if(/,[0123456789]{2}$/.test(newval))
			{
				newval = newval.replace(',','.');
				newval = newval.replace(' ','');
				$('#id_price').val(newval);
			}
		});		
	}
	
	// Initialize checkbox replacements
	crir.init();
	
	// Submit buttons can only be clicked once;
	$(':submit').bind('click', function() {
	       $(this).unbind('click');
	});

	initAjaxVideos();
	
	
});

function initAjaxVideos()
{
	$('.video-detail .pagination a').attr('href', 'javascript: void(0);')
	// Ajax paging on video site
	$('.video-detail .pagination .next').click(function(){ 
		if(currentPage)
		{
			currentPage += 1;
			ajaxUrl = "/videoklipp/?format=json&page=" + currentPage.toString();
			if(videoSlug){
				ajaxUrl = "/videoklipp/video/"+ videoSlug +"/?format=json&page=" + currentPage.toString();
			}
			$.ajax({	url: ajaxUrl, 
						context: document.body,
						success: function(data){
							json = eval(data)[0];
							drawVideos(json);
			      		}});
		}
	});
	
	$('.video-detail .pagination .prev').click(function(){ 
		if(currentPage)
		{
			currentPage -= 1;
			ajaxUrl = "/videoklipp/?format=json&page=" + currentPage.toString();
			if(videoSlug){
				ajaxUrl = "/videoklipp/video/"+ videoSlug +"/?format=json&page=" + currentPage.toString();
			}
			$.ajax({	url: ajaxUrl, 
						context: document.body,
						success: function(data){
							json = eval(data)[0];
							drawVideos(json);
			      		}});
		}
	});
}

function drawVideos(data)
{	
	pagination = '<span class="current">';
    pagination += '    Sida '+ data.page.toString() +' av '+ data.pages.toString();
    pagination += '</span>';
	if(data.page > 1)
	{
    	pagination += '<a href="javascript: void(0);" class="prev">F&ouml;reg&aring;ende</a>';
    	if(data.pages > data.page)
		{
			pagination += '|';
		}
	}
	if(data.pages > data.page)
	{
		pagination += '<a href="javascript: void(0);" class="next">N&auml;sta</a>';
	}
	
	$('.pagination .step-links').html(pagination);
	
	
	var videos = data.videos;
	var tags = data.tags;
	var text = "";
	for(i=0; i<videos.length; i++)
	{
		video = videos[i];
		text += '<div class="video">';
		text += '	<a href="/sv-se/videoklipp/video/'+ video.fields.slug +'/"><img src="/media/'+ video.fields.video_thumbnail +'" alt="'+ video.fields.title +'"></a>';
		text += '	<div class="text">';
		text += '		<h4><a href="/sv-se/videoklipp/video/'+ video.fields.slug +'/">'+ video.fields.title +'</a></h4>';
		text += '		<p class="summary"><a href="/sv-se/videoklipp/video/'+ video.fields.slug +'/">'+ video.fields.summary +'</a></p>';
		text += '		<div class="tags">';
		text += '			<p>Postad i';
		text += '				<strong>';
		for (j=0; j<video.fields.tags.length; j++)
		{
			tagID = video.fields.tags[j];
			for(k=0; k<tags.length; k++)
			{
				tag = tags[k];
				if(tagID == tag.pk)
				{
					text += '			<a href="/sv-se/videoklipp/kategori/'+ tag.fields.slug +'/">'+ tag.fields.name +'</a>';									
				}
			}
		}	
		text += '		</strong></p>';
		text += '		</div>';
		text += '	</div>';
		text += '	<div class="clear-fix"></div>';
		text += '</div>';
	}
	
	$('.videos .container').html(text);
	initAjaxVideos();
}

/* Search input boxes in subnav */
function searchInputFocus(message, target)
{
	if(target.value == message || target.value.indexOf(message) == 0)
	{
		target.value = "";
	}
	target.className = "focused";
}

function searchInputBlur(message, target)
{
	if(target.value == "" || target.value.indexOf(message) == 0)
	{
		target.value = message;
	}	
}


function postBack(ctrl, values, form_id)
{
	for(var attr in values)
	{
		$('#'+attr).val(values[attr]);
	}
	
	var form;	
	if(form_id != null)
	{
		form = $('#'+form_id)
	}
	else
	{
		$(ctrl).parents('form:first');
	}
	form.submit();
}

/*
	CRIR - Checkbox & Radio Input Replacement
	Author: Chris Erwin (me[at]chriserwin.com)
	www.chriserwin.com/scripts/crir/

	Updated July 27, 2006.
	Jesse Gavin added the AddEvent function to initialize
	the script. He also converted the script to JSON format.
	
	Updated July 30, 2006.
	Added the ability to tab to elements and use the spacebar
	to check the input element. This bit of functionality was
	based on a tip from Adam Burmister.
*/
crir = {
	init: function() {
		arrLabels = document.getElementsByTagName('label');
	
		searchLabels:
		for (var i=0; i<arrLabels.length; i++) {			
			// get the input element based on the for attribute of the label tag
			if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value != '') {
				labelElementFor = arrLabels[i].getAttributeNode('for').value;				
				inputElement = document.getElementById(labelElementFor);
			}
			else {				
				continue searchLabels;
			}	
							
			inputElementClass = inputElement.className;	
		
			// if the input is specified to be hidden intiate it
			if (inputElementClass == 'crirHiddenJS') {
				inputElement.className = 'crirHidden';
				
				inputElementType = inputElement.getAttributeNode('type').value;	
				
				// add the appropriate event listener to the input element
				if (inputElementType == "checkbox") {
					inputElement.onclick = crir.toggleCheckboxLabel;
				}
				else {
					inputElement.onclick = crir.toggleRadioLabel;
				}
				
				// set the initial label state
				if (inputElement.checked) {
					if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_checked'}
					else { arrLabels[i].className = 'radio_checked' }
				}
				else {
					if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_unchecked'}
					else { arrLabels[i].className = 'radio_unchecked' }
				}
			}
			else if (inputElement.nodeName != 'SELECT' && inputElement.getAttributeNode('type').value == 'radio') { // this so even if a radio is not hidden but belongs to a group of hidden radios it will still work.
				arrLabels[i].onclick = crir.toggleRadioLabel;
				inputElement.onclick = crir.toggleRadioLabel;
			}
		}			
	},	
	
	findLabel: function (inputElementID) {
		arrLabels = document.getElementsByTagName('label');
	
		searchLoop:
		for (var i=0; i<arrLabels.length; i++) {
			if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value == inputElementID) {				
				return arrLabels[i];
				break searchLoop;				
			}
		}		
	},	
	
	toggleCheckboxLabel: function () {
		labelElement = crir.findLabel(this.getAttributeNode('id').value);
	
		if(labelElement.className == 'checkbox_checked') {
			labelElement.className = "checkbox_unchecked";
		}
		else {
			labelElement.className = "checkbox_checked";
		}
	},	
	
	toggleRadioLabel: function () {			 
		clickedLabelElement = crir.findLabel(this.getAttributeNode('id').value);
		
		clickedInputElement = this;
		clickedInputElementName = clickedInputElement.getAttributeNode('name').value;
		
		arrInputs = document.getElementsByTagName('input');
	
		// uncheck (label class) all radios in the same group
		for (var i=0; i<arrInputs.length; i++) {			
			inputElementType = arrInputs[i].getAttributeNode('type').value;
			if (inputElementType == 'radio') {
				inputElementName = arrInputs[i].getAttributeNode('name').value;
				inputElementClass = arrInputs[i].className;
				// find radio buttons with the same 'name' as the one we've changed and have a class of chkHidden
				// and then set them to unchecked
				if (inputElementName == clickedInputElementName && inputElementClass == 'crirHidden') {				
					inputElementID = arrInputs[i].getAttributeNode('id').value;
					labelElement = crir.findLabel(inputElementID);
					labelElement.className = 'radio_unchecked';
				}
			}
		}
	
		// if the radio clicked is hidden set the label to checked
		if (clickedInputElement.className == 'crirHidden') {
			clickedLabelElement.className = 'radio_checked';
		}
	},
	
	addEvent: function(element, eventType, doFunction, useCapture){
		if (element.addEventListener) 
		{
			element.addEventListener(eventType, doFunction, useCapture);
			return true;
		} else if (element.attachEvent) {
			var r = element.attachEvent('on' + eventType, doFunction);
			return r;
		} else {
			element['on' + eventType] = doFunction;
		}
	}
}
