// Theme: Dailymotion
// Author: Fawaz Tahir
// File Type: JS	
// Description: These are my own custom functions to enhance theme's functionallity
// Updated : 8th July, 2010.
// Update : Added ToggleStyle() function. 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//


// Function used to change to the Text to certain Div
function changeText(Div,Text) {
	$(Div).text(Text);
}

// Function used to toggle 'clicked' class between tabs
function changeTabs(Div,obj) {
	$(Div).removeClass('clicked');
	$(obj).addClass('clicked');
}

// Function to close result div
function closeMessage(result) {
	var ResultDiv = "#"+result;	
	$(ResultDiv).ajaxComplete(function(){
		insertDiv(ResultDiv);
		// If you want the message to disappear automatically remove two slashes from setTimeout,
		// and message will fadeOut after 5 seconds of appearing. Change 5000 to change duration,
		// Time is set in milli-seconds so 5000 means 5 sec, likewise 3000 means 3 sec
		//setTimeout(function(){ fadeDiv(ResultDiv);},3000);
	});
}

// Function used to fade the result DIV
function fadeDiv(result) {
	$(result).fadeOut(350);	
}

// Function used to add the close DIV
function insertDiv(ResultDiv) {
	var DIV = '"'+ResultDiv+'"';
	var Divclose = "<div class='close' onClick='fadeDiv("+DIV+")' title='Close'>Close</div>";
	$(ResultDiv).append(Divclose);
}

// Function used to do something which i can't explain in sleep ... ZZzzZzzZZz
function formActions(Div,target) {
	$(Div).bind("click",function() {
		$(target).focus();
		$(target).css({ 'border' : 'none'});
		$(Div).addClass('focused');
	});
	
	$(target).bind("blur",function() {
		$(this).parents(Div).removeClass('focused');						
	});
}

function showDiv(Div) {
	$("#"+Div).show();	
}

function hideDiv(Div) {
	$("#"+Div).hide();	
}

function addFirstClass() {
	$('.uv_opts li:first').addClass('clicked');	
}

function toggleArrow(obj,Div) {
	$(obj).find('.blk_arrow').toggleClass('blk_arrow_opened');
	$(Div).toggle();
}

function newUsername(Div,username) {
	$(window).bind("load",function() {
		$(Div).val(username);								   
	});
}

function replyTo(cid,Div) {
	var commentLink = "Reply To #"+cid+"";
	$("#reply_to").val(cid);
	window.location = Div;
	$(Div).val(commentLink);
}

function ToggleMoreInfo(obj,Div) {
	var current = $(obj).children().text();
	var less = 'Less Info';
	var more = 'More Info';
	
	if(current == more) {
		$(obj).addClass('less_info');
		$(obj).children().text(less);
		$(obj).children().attr('title',less);
		$(Div).show();
	} else {
		$(obj).removeClass('less_info')
		$(obj).children().text(more);
		$(obj).children().attr('title',more);
		$(Div).hide();
				
	}
}

function openURL(url) {
	document.location = url;
}


// The Following code is by http://valums.com
// Visit this link to view orignal post http://valums.com/scroll-menu-jquery/
function horScroller() {
	//Get our elements for faster access and set overlay width
	var div = $('div.editors_vdos'),
	ul = $('ul.editors_vid'),
	// unordered list's left margin
	ulPadding = 15;
	
	//Get menu width
	var divWidth = div.width();
	
	//Remove scrollbars
	div.css({overflow: 'hidden'});
	
	//Find last image container
	var lastLi = ul.find('li:last-child');

	//When user move mouse over menu
	div.mousemove(function(e){
		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		div.scrollLeft(left);
	});	
}


function ToggleStyle(obj) {
	var obj = $(obj),
		obj_id = obj.attr('id'),
		parent = obj.parent().attr('id'),
		target = $("#"+parent).next().attr('id');
		//alert('#'+parent+' #'+target+' .grid_view');
		if(obj_id == "grid") {
			$('#'+parent+' + #'+target+' .dm_v_container').removeClass('list_view').addClass('grid_view');
			$("#"+parent).children().removeClass('selected').filter(obj).addClass('selected');
			$.cookie("current_style","grid_view")				
		} else {
			$('#'+parent+' + #'+target+' .dm_v_container').removeClass('grid_view').addClass('list_view');
			$("#"+parent).children().removeClass('selected').filter(obj).addClass('selected');
			$.cookie("current_style","list_view")				
		}
}
