// JavaScript Document
function popup(url,winname,w,h,feat)
{
	if (!(isNaN(w) || isNaN(h)))
	{
		var x=parseInt((screen.width-w)/2);
		var y=parseInt((screen.height-h)/2);
		if (x<0)	x=0;
		if (y<0)	y=0;
		
		if (feat!=null && feat!="")
		{
			feat=","+feat;
		}
		else
		{
			feat="";
		}
		feat="left="+x+",top="+y+",width="+w+",height="+h+feat;
	}
	window.open(url,winname,feat);
}

// --------------------------------------------------
// Count number of checkbox that have been ticked
// --------------------------------------------------
function tickcount(form_str,name_str)
{
	var f_array=document.forms[form_str];
	var c_int=0;
	if (f_array)
	{
		for (var i_int=0; i_int<f_array.length; i_int++)
		{
			if (f_array[i_int].name==name_str && f_array[i_int].value!="")
			{
				c_int+=(f_array[i_int].checked) ? 1 : 0;
			}
		}
	}
	return c_int;
}

function helpMe() {
	popup(dotdotpath+"help","",900,700,"");
}

function toggleSubnav(div,mId){
	var subnav = document.getElementById(div);
	var divs = subnav.getElementsByTagName('div');
	var subsubnavs =[];
	for(var a=0; a < divs.length; a++){
		current=divs[a];
		if(current.className == 'loc_subsubmenu hide' || current.className == 'loc_subsubmenu')
		{
			subsubnavs.push(current);
		}
	}
	var n=subsubnavs.length;
	for(var i=1; i <= n; i++){
		var ul = document.getElementById('ssn_'+i);
		var ullink=  document.getElementById('sn_'+i);
		if(i == mId){
			if(ul.className!='loc_subsubmenu'){
				ul.className='loc_subsubmenu';
				if(ullink.className !='inactive')
				{
				ullink.className='active';
				}
			}else{
				ul.className='loc_subsubmenu hide';
				if(ullink.className !='inactive')
				{
				ullink.className='';
				}
			}
		}else{
			ul.className='loc_subsubmenu hide';
			if(ullink.className !='inactive')
			{
			ullink.className='';
			}
		}
	}
	resizeMap();
}

function toggleSubnavBus(div,mId){
	var subnav = document.getElementById(div);
	var divs = subnav.getElementsByTagName('div');
	
	
	var subsubnavs =[];
	for(var a=0; a < divs.length; a++){
		current=divs[a];
		if(current.className == 'bus_subsubmenu hide' || current.className == 'bus_subsubmenu')
		{
			subsubnavs.push(current);
		}
	}
	var n=subsubnavs.length;
	for(var i=1; i <= n; i++){
		var ul = document.getElementById('ssnbus_'+i);
		var ullink=  document.getElementById('snbus_'+i);
		if(i == mId){
			if(ul.className!='bus_subsubmenu'){
				ul.className='bus_subsubmenu';
				if(ullink.className !='inactive')
				{
				ullink.className='active';
				}
			}else{
				ul.className='bus_subsubmenu hide';
				if(ullink.className !='inactive')
				{
				ullink.className='';
				}
			}
		}else{
			ul.className='bus_subsubmenu hide';
			if(ullink.className !='inactive')
			{
			ullink.className='';
			}
		}
	}
	
	resizeMap();
}



function checkAll(group){
  for (i=0; i<document.subcats.length; i++)
     if(document.subcats.elements[i].name==group) document.subcats.elements[i].checked = true
}

function clearAll(group){
  for (i=0; i<document.subcats.length; i++)
     if(document.subcats.elements[i].name==group) document.subcats.elements[i].checked = false
}


function showSubnav(div,mId){
	var subnav = document.getElementById(div);
	var divs = subnav.getElementsByTagName('div');
	var subsubnavs =[];
	for(var a=0; a < divs.length; a++){
		current=divs[a];
		if(current.className == 'loc_subsubmenu hide' || current.className == 'loc_subsubmenu')
		{
			subsubnavs.push(current);
		}
	}
	var n=subsubnavs.length;
	for(var i=1; i <= n; i++){
		var ul = document.getElementById('ssn_'+i);
		var ullink=  document.getElementById('sn_'+i);
		if(i == mId){
			ul.className='loc_subsubmenu';
			ullink.className='active';
		}else{
			ul.className='loc_subsubmenu hide';
			if(ullink.className !='inactive')
			{
			ullink.className='';
			}
		}
	}
	resizeMap();
}


function noclick(e)
{
	if (!e)
	{
		var e=window.event;
	}
	e.cancelBubble=true;
	if (e.stopPropagation)
	{
		e.stopPropagation();
	}
}

function addtofav(id)
{
	$.post(dotdotpath + "_include/ssi/favpost.cfm", {tokenid: id }, function(data){
		var div = document.getElementById("favpost"+id);																	
		if (data == "Added"){
			div.innerHTML = "<span class=\"favs_lnk right\"><a href=\""+dotdotpath+"profile/favourites/\">View Favourites</a></span>";
		}
	});
	
}

function removefav(id)
{
	$.post(dotdotpath + "_include/ssi/favremove.cfm", {tokenid: id });	
}



function loadimageitem(resourceid,thumbnailfilepath)
{
	$.post(dotdotpath + "_include/ssi/module/imageitem.cfm", {thumbnailfilepath: thumbnailfilepath}, function(data){
		var div = document.getElementById("theimage"+resourceid);
		div.innerHTML = data;
		//do next image
	});	
}

function sendtofriend(url,yourname,email,message)
{
	$.post(dotdotpath + "_include/ssi/sendtoafriendpost.cfm", {url: url, yourname: yourname, emailaddress: email, message:message}, function(data){
		var div = document.getElementById("send_friend_box");	
		var div2 = document.getElementById("send_friend_box_sent");
		if (data == "Sent"){
			div2.style.display = "";
			div.style.display = "none";
		}
	});
	
}

function showhide(o,st)
{
	//alert('showhide');
	if (typeof(o)=="string")
	{
		o=document.getElementById(o);
	}
	if (!o)
	{
		return;
	}
	if (typeof(st)!="boolean")
	{
		st=(o.style.display=="none");
	}
	o.style.display=(st) ? "" : "none";
	return st;
}

function show(o){
	if (typeof(o)=="string")
	{
		o=document.getElementById(o);
	}
	if (!o)
	{
		return;
	}
	o.style.display='';
}

function hide(o){
	if (typeof(o)=="string")
	{
		o=document.getElementById(o);
	}
	if (!o)
	{
		return;
	}
	o.style.display='none';
}

function showquicksearchtab(){
	show('quick_search_tab');
	hide('adv_search_tab');
	
	document.getElementById('quick_search_tabbtn').className = 'active';
	document.getElementById('adv_search_tabbtn').className = '';
}

function showadvancedsearchtab(){
	hide('quick_search_tab');
	show('adv_search_tab');
	
	document.getElementById('quick_search_tabbtn').className = '';
	document.getElementById('adv_search_tabbtn').className = 'active';
}

function showimagesfiltertab(){
	show('images_filters');
	hide('section_filters');
	
	document.getElementById('images_filtersbtn').className = 'active';
	document.getElementById('section_filtersbtn').className = '';
}

function showsectionfiltertab(){
	hide('images_filters');
	show('section_filters');
	
	document.getElementById('section_filtersbtn').className = 'active';
	document.getElementById('images_filtersbtn').className = '';
}

function showhidesubcats(a,o,st)
{
	var alink=document.getElementById(a);
	
	if (typeof(o)=="string")
	{
		o=document.getElementById(o);
		
	}
	if (!o)
	{
		return;
	}
	if (typeof(st)!="boolean")
	{
		st=(o.style.display=="none");
	}
	o.style.display=(st) ? "" : "none";
/*	if(alink.innerHTML=="Subcategories"){
		alink.innerHTML="Subcategories";
	}else{
		alink.innerHTML="Subcategories";
	}
*/	return st;
}

function showhideTabs(div, n){
	
	var menu = document.getElementById(div+"_menu");
	var num = menu.getElementsByTagName('a');
	
	for(var i=1; i <= num.length; i++){
		var tab =document.getElementById(div +'_'+i);
		var btn =document.getElementById(div +'_btn_'+i);
		if(i==n){
			tab.style.display='';
			btn.className = 'active';
		}else{
			tab.style.display='none';
			btn.className = '';
		}
	}
}

function changesearchtext(st){
	if (st == true)
	{
		if(document.s.keyword.value=="Type Keyword"){
		 document.s.keyword.value="";	
		}
	} else {
		if(document.s.keyword.value==""){
		 document.s.keyword.value="Type Keyword";
		}
	}
}
function changeadvsearchtext(st){
	if (st == true)
	{
		if(document.adv_search.keyword.value=="Keyword"){
		 document.adv_search.keyword.value="";	
		}
	} else {
		if(document.adv_search.keyword.value==""){
		 document.adv_search.keyword.value="Keyword";
		}
	}
}
function changetopsearchtext(st){
	if (st == true)
	{
		if(document.q_search.keyword.value=="Search"){
		 document.q_search.keyword.value="";	
		}
	} else {
		if(document.q_search.keyword.value==""){
		 document.q_search.keyword.value="Search";
		}
	} 
}

function imagelist(theimages,totalimages)
{
	document.m.newrelatedresourceguid.value = theimages;
	document.getElementById("resourcelist").innerHTML = "You have selected " + totalimages + " images.";
}

function videolist(thevideos,totalvideos)
{
	document.m.newrelatedresourceguid.value = thevideos;
	document.getElementById("resourcelist").innerHTML = "You have selected " + totalvideos + " videos.";
}

function changepage(id)
{
	for(i=1; i<=totalpages; i++){
		document.getElementById("page"+i).style.display = "none";	
	}
	document.getElementById("page"+id).style.display = "";
	thepage = id;
	if (document.getElementById("thepage"))
	{
		document.getElementById("thepage").innerHTML = thepage;
	
		if (thepage > 1)
		{
			document.getElementById("firstbit").innerHTML = "<li class=\"first\"><a href=\"##\" onclick=\"changepage(1); return false;\" title=\"first page\"><span>First</span></a></li>";
		} else {
			document.getElementById("firstbit").innerHTML = "<li class=\"first inactive_lft_first\"><span>First</span></li>";
		}
	
		if (thepage > 1)
		{
			document.getElementById("secondbit").innerHTML = "<li class=\"prev\"><a href=\"##\" onclick=\"changepage(thepage-1); return false;\" title=\"previous page\">Previous</a></li>";
		} else {
			document.getElementById("secondbit").innerHTML = "<li class=\"prev inactive_lft\">Previous</li>";
		}
	
		if (thepage < totalpages)
		{
			document.getElementById("thirdbit").innerHTML = "<li class=\"next\"><a href=\"##\" onclick=\"changepage(thepage+1); return false;\" title=\"next page\">Next</a></li>";
		} else {
			document.getElementById("thirdbit").innerHTML = "<li class=\"next inactive_rgt\">Next</li>";
		}
		
		if (thepage < totalpages)
		{
			document.getElementById("fourthbit").innerHTML = "<li class=\"last\"><a href=\"##\" onclick=\"changepage(totalpages); return false;\" title=\"last page\"><span>Last</span></a></li>";
		} else {
			document.getElementById("fourthbit").innerHTML = "<li class=\"last inactive_rgt_last\"><span>Last</span></li>";
		}	
		
	}
	var StartLoop = 1;
	var EndLoop = 1;
	
	StartLoop = (5 * ((thepage-1)/5));
	if (StartLoop <= 0)
	{
		StartLoop = 1;
	}
	
	if (totalpages > 5)
	{
		var endofpaging = totalpages - 4
		if(StartLoop > endofpaging)
		{
			StartLoop = endofpaging;	
		}
	}
	else
	{
		StartLoop = 1;	
	}
	
	StopLoop = StartLoop + 4
	if (StopLoop > totalpages)
	{
		StopLoop = totalpages;
	}
    
	var paging = "";
	for(i=StartLoop; i<=StopLoop; i++)
		{
			paging = paging + "<li class='page"; 
			if (i == StopLoop)
			{
				paging = paging + "pg_last";
			}
			
			paging= paging + "'><a href='' onclick='changepage(" +i+ "," +totalpages+ "); return false;' title='page "+i+ "' ";
			
			if (thepage == i)
			{
				paging = paging + "class='active'";	
			}
			
			paging = paging + ">" +i+ "</a></li>";
			document.getElementById("paging").innerHTML = paging;
		}

}


// overlays

function addtobox(id)
{	
	$.post(dotdotpath + "_include/ssi/boxpost.cfm", {resourceid: id }, function(data){
		
		var div = document.getElementById("boxpost"+id);
		
		var viewitem_id = false;
		
		if (data.indexOf("Added") > -1){
			div.className = "added2lghtbx";
			div.innerHTML = "Item added to lightbox";
			lightboxlist = lightboxlist + id + ",";
			
			var viewitem_id = data.replace(/Added_/i, "");
			
		} else {
			if(div.className == "added2lghtbx"){
			//do nothing	
			}else{
			div.className = "add2lghtbx";	
			}
		}
	
		//$.fn.colorbox({inline:true,open:true,opacity:0.85,href:'#lightboxwindow'});
		popup(dotdotpath + "_include/ssi/lightbox.cfm?viewid="+viewitem_id+"",'lightbox',842,670);
	});
}

function deletefolder(foldername)
{
	if (confirm("Are you sure you want to delete this folder ?"))
	{
		$.post(dotdotpath + "_include/ssi/lightbox/deletefolder.cfm?nocached="+Math.round(Math.random()*10000000), {foldername:foldername}, function(data){
			backtofolders('Default','');																															 
		});
	}
}

function sharefolder (foldername)
{
	mailstring="mailto:?subject=iMOVIEi: Share Lightbox&body=Hi,%0A%0AI've created a folder of images in my iMOVIEi lightbox. Please click on the link below to view the images I've selected:-%0A%0Ahttp://www.imoviei.com/index.cfm?sharefolder="+foldername+"%26shareemail="+myemail;
	
	window.open(mailstring,"_self");
}

function openoverlay(element,folder,shareemail) {
		
	popup(dotdotpath + "_include/ssi/lightbox.cfm?folder="+folder+"&shareemailaddress="+shareemail+"",'lightbox',842,670);
	
	
	/*$.post(dotdotpath + "_include/ssi/lightbox/folders.cfm?nocached="+Math.round(Math.random()*10000000), {folder:folder}, function(data){
		var div = document.getElementById("wrap");	
		
		//div.innerHTML=data;
		$(div).html(data);
		
		$.fn.colorbox({
			inline:true,
			open:true,
			opacity:0.85,
			href:element,
			onComplete:function(){
				//setTimeout("dofolders('')",1000);
				//setTimeout("setfolder('"+folder+"','"+shareemail+"')",1500);
				dofolders('');
				setfolder(folder,shareemail);
			}
		});
																						   
		
	});*/
	
	//$.fn.colorbox({inline:true,open:true,opacity:0.85,href:element});
}

function mycarousel_itemVisibleInCallback(carousel, theitem, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, theitem, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(theitem)
{
	return '<div style="width:115px; height:140px; padding:10px 0px 0px 10px;" onclick="setfolder(\''+theitem.title+'\',\'\');" onmouseover="this.style.backgroundColor=\'#202a34\';" onmouseout="this.style.backgroundColor=\'#060606\';"><img src="' + theitem.url + '" width="105" height="80" alt="' + theitem.title + '" /><p><span class="block_subhdr">Folder: ' + theitem.title + '</span><br /><a href="#" onclick="deletefolder(\''+theitem.title+'\'); return false;">Delete</a> | <a href="#" onclick="sharefolder(\''+theitem.title+'\'); return false;">Share</a></p></div>';
};


function dofolders(foldername)
{	
	$.post(dotdotpath + "_include/ssi/lightbox/listfolders.cfm?nocached="+Math.round(Math.random()*10000000), {folder:foldername}, function(data){
		thedata=new Object();
			
		var valueArray = data.split("{");
	
		mycarousel_itemList=new Array();
		for(var i=1; i<valueArray.length; i++){

			urlstart=valueArray[i].indexOf("'")+1;
			urlend=valueArray[i].indexOf("'",urlstart+1);

			titlestart=valueArray[i].indexOf("title: '")+8;
			titleend=valueArray[i].indexOf("'",titlestart+1);
			
		
			thedata.url=valueArray[i].substring(urlstart,urlend);
			thedata.title=valueArray[i].substring(titlestart,titleend);
			
			mycarousel_itemList.push({url: thedata.url, title:thedata.title});
			
		}
		/*jQuery('#mycarousel').jcarousel({
			size: mycarousel_itemList.length,
			itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
			itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
		});*/

		/*jQuery('#mycarousel').jcarousel({
       		scroll: 2,
			initCallback: loadcorousel,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
		
		function loadcorousel(carousel)
		{
			for (var i = 1; i <= 6; i++) {
				var idx = carousel.index(i, mycarousel_itemList.length);
				carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
			}
			jQuery('.jcarousel-control a').bind('click', function() {
				carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
				return false;
			});
		 
			//jQuery('.jcarousel-scroll ul').bind('change', function() {
			//	carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
			//	return false;
			//});
 
			jQuery('#mycarousel-next').bind('click', function() {
				carousel.next();
				return false;
			});
		 
			jQuery('#mycarousel-prev').bind('click', function() {
				carousel.prev();
				return false;
			});
		}*/
		
	});
}

function closewindow () {
	$.fn.colorbox.close();
}


// overlays
function createfolder(foldername,lightboxid) {
	
	$.post(dotdotpath + "_include/ssi/lightbox/createfolder.cfm?nocached="+Math.round(Math.random()*10000000), {foldername: foldername}, function(data){
		setTimeout("movelightboxitem("+lightboxid+",'"+foldername+"');",1000);
	});
}

function setfolder(foldername,shareemail) {
	
	if (foldername == 'Default'){foldername='';}
	
	document.getElementById("lightbx_back_button").name=foldername;
	
	$.post(dotdotpath + "_include/ssi/_lightbox.cfm?nocached="+Math.round(Math.random()*10000000), {folder: foldername, shareemail: shareemail}, function(data){
		var div = document.getElementById("lightboxupdate");	
		div.innerHTML=data;
	});
	$("#lightboxupdate").fadeIn()	
}

function backtofolders(folder,shareemail) {
		
	$("#lightbx_back_button").fadeOut()
	$("#lightboxupdate").fadeOut()
	$("#lightboxupdate").html();
	
	$.post(dotdotpath + "_include/ssi/lightbox/folders.cfm?nocached="+Math.round(Math.random()*10000000), {folder:folder}, function(data){
		var div = document.getElementById("wrap");	
		
		div.innerHTML=data;
		popup(dotdotpath + "_include/ssi/lightbox.cfm?folder="+folder+"&shareemailaddress="+shareemail+"",'lightbox',842,670);
	});
}

function viewlightboxitem(lightboxid,foldername) {
	
	$.post(dotdotpath + "profile/lightbox/miniview.cfm?nocached="+Math.round(Math.random()*10000000), {folder: foldername, LightboxID: lightboxid}, function(data){
		var div = document.getElementById("lightboxupdate");	
		div.innerHTML=data;
	});
}

function viewlightboximage(lightboxid,foldername,shareemail) {
	
	lightboxid = (lightboxid) ? lightboxid : 0;
	
	$.post(dotdotpath + "_include/ssi/lightbox/miniview.cfm?nocached="+Math.round(Math.random()*10000000), {LightboxID: lightboxid, shareemail:shareemail}, function(data){
		var div = document.getElementById("lightboxupdate");	
		div.innerHTML=data;
	
		document.getElementById("lightbx_back_button").onclick=function() {
		  backtofolders(foldername,shareemail);
		}
		
		document.getElementById("wrap").style.display="none";
		$("#lightbx_back_button").fadeIn();
	});
}

/*function viewlightbox_new_item(lightboxid,foldername) {
	
	$.post(dotdotpath + "profile/lightbox/newitems.cfm?nocached="+Math.round(Math.random()*10000000), {folder: foldername, LightboxID: lightboxid}, function(data){
		var div = document.getElementById("lightboxupdate");	
		div.innerHTML=data;
		
	});
}*/


function movelightboxitem(lightboxid,foldername) {
	$.post(dotdotpath + "_include/ssi/lightbox/moveitem.cfm?nocached="+Math.round(Math.random()*10000000), {folder: foldername, LightboxID: lightboxid}, function(data){
		backtofolders(foldername,'');
	});
}

function createfolder_move(lightboxid, foldername, newfoldername) {
	
	if(foldername == "00000newfoldercreate00000")
	{
		if(newfoldername != "")
		{
			createfolder(newfoldername);
			
			movelightboxitem(lightboxid, newfoldername);
			
			setfolder(newfoldername);
		}
		else
		{
			return false;	
		}
	}
	else
	{
		movelightboxitem(lightboxid, foldername);
		
		setfolder(foldername);
	}
	
	return false;
}

function loadImage(imageID,imageFilePath,LargeImageFilePath,Description,CurrentRecord,TotalRecords,addtolightbox)
{
	document.getElementById("bigImage").src=securerooturl + "resource/" + imageFilePath;
	if (addtolightbox)
	{
	document.getElementById("bigImagelink").href=securerooturl + "resource/" + LargeImageFilePath + "?id=" + imageID;
	} else {
	document.getElementById("bigImagelink").href=securerooturl + "resource/" + LargeImageFilePath;
	}
	document.getElementById("bigImagelink").title=Description;

	for (i=1; i<=TotalRecords; i++)
	{	
		if (document.getElementById("thecopyright"+i))
		{
			document.getElementById("thecopyright"+i).style.display="none";
		}
	}

	document.getElementById("thecopyright"+CurrentRecord).style.display="";
	checklightbox(imageID);
}


function deletelightbox(lightboxid, foldername)
{
	if (confirm("Are you sure you want to remove from your lightbox?"))
	{
	$("#lightboxupdate").fadeOut()
	$("#lightboxupdate").html('');
		$.post(dotdotpath + "_include/ssi/lightbox/postdelete.cfm?nocached="+Math.round(Math.random()*10000000), {folder: foldername, lightboxid: lightboxid}, function(data){
			setfolder(foldername);
			$("#wrap").fadeIn();
			$("#lightbx_back_button").fadeOut();
		});
	}
}

function gotosearch(area)
{
	location.href=dotdotpath+area+"/?st="+area;
}

function setTagSearch(tagname,tagid,tagrow,totalrows)
{
	for (var i=1; i<=totalrows; i++)
	{
		document.getElementById("taglink"+i).className = "";
	}
	document.getElementById("taglink"+tagrow).className = "thistag";
	document.getElementById("tagname").value = tagname;
	document.getElementById("tagid").value = tagid;
}

function updaterelative(name, val, checked, label, href, container)
{
	if(checked == true && name.indexOf('tmp') > -1 && $('input[name='+name+'][value='+val+']').length == 0) createrelative(name, label, href, val, container);
	
	$('input[name='+name+'][value='+val+']').attr('checked', checked);	
}

function createrelative(name, label, href, val, container)
{
	var chx = "<input type=\"checkbox\" checked=\"checked\" name=\""+name+"\" value=\""+val+"\" onclick=\"updaterelative('"+name.replace('tmp', '')+"', this.value, this.checked);\"><a href='"+href+"' title='"+label+"'>" + label.substr(0,30) + "</a><br />";
	
	$("#"+container).append(chx);
					
	$("#"+container).animate({scrollTop: $("#"+container)[0].scrollHeight});
}

jQuery().ready(function(){
	
		$("#socialContainer").mouseover(function(){
			$(this).css({width:'102px' });	
		});
		
		$("#socialContainer").mouseout(function(){
			$(this).css({width:'33px' });	
		});
		
});

