/* http://javascriptcompressor.com/ */
$(function(){
	startup();
	var anchorname = (location.href.split('#')[1]); 
	if (screen.width<940) $('head').append('<link href="_src/stylesheet_left.css" rel="stylesheet" type="text/css" />');

	poll_popup();
	$('.autotitle').titleInValue();
	
	initCatDir();


	// add to / transfer
	$('.add-to-qu a').each(function(){
		if ($(this).attr('href')=='#') 
		{
			$(this).bind('click',function(){
				prodAddToQuote($(this).attr('id').split('_')[1],$(this).attr('id').split('_')[2]);
				return false;
			});
		}
	});
	$('.add-to-id a').each(function(){
		if ($(this).attr('href')=='#') 
		{
			$(this).bind('click',function(){
				prodAddToIdeas($(this).attr('id').split('_')[1],$(this).attr('id').split('_')[2]);
				return false;
			});
		}
	});
	$('.add-to-ps a').each(function(){
		if ($(this).attr('href')=='#') 
		{
			$(this).bind('click',function(){
				prodAddToTransfers($(this).attr('id').split('_')[1],$(this).attr('id').split('_')[2]);
				return false;
			});
		}
	});
	//
	$('.fs-embed-play').each(function(){
		$(this).bind('click',function(){
			$('.fullscreen').hide(0);
			var id=$(this).attr('id').split('_')[1];
			block=$('#'+id).css('display','block');
			block=$('#'+id+' a').bind('click',function(){block=$('#'+id).css('display','none');$('.fullscreen').show(0);});
		});
	});
	enableDeleteMsg();


	$('#currency_self_on').bind('click',function(){
		if (this.checked==true) {
			$('#currency_self_on_show').css('display','inline');
		}
		else
		{
			$('#currency_self').val('');
			$('#currency_self_on_show').css('display','none');
		}
	});
	
	$('#showFeedback').bind('click',function(){$('#feedbackbox').show();$('#form_feedbackbox textarea').focus();});
	$('#hideFeedback').bind('click',function(){$('#feedbackbox').hide(200);});
	$('#form_feedbackbox').bind('submit',function(){
		loadReplacePost('form_feedbackbox','load_feedbackbox',1,true,'Sending...'); // old.
		setTimeout("ge('feedbackbox').style.display='none';",2000);
		return false;
	});
	$('#doLogout').bind('click',function(){$('#logoutform').submit();});
	$('.collapse').each(function(){
		var thish2=$(this).find('h2');
		var thisblock=$(this).find('blockquote');
		thish2.append(' <span>Click to expand / hide</span>');
		thish2.bind('click',function(){thisblock.toggle();})
	});


	$('.loadselectlogo').bind('click',function(){
		$('#logostorepopup').remove();
		var ids=$(this).attr('rel').split('-');
		if (ids[2]==undefined) {ids[2]='';}
		$.get('ajax.php?go=logostore-popup&do=choose&selected='+ids[1]+'&sendto='+ids[0]+'&extra='+ids[2],function(d){
			blackout(true);
			$('body').append('<div id="logostorepopup">'+d+'</div>');
			$('#logostorepopup').center();
			checkForFunc();
			logoChoose();
			popupLogos();
		});
	});
	$('.dddrop li').dddrop();

	/* pre nigelogo */

	$('.imagepreview').each(function(e){
		$(this).bind('mouseover',function(e){
			$('#imagepreview').remove();
			$('body').append('<div id="imagepreview" style="padding:5px;background:#ccc;border:1px solid black;position:absolute;z-index:9999999;"><img src="'+$(this).attr('rel')+'" width="300" height="300" /></div>');
			$('#imagepreview').css({top:(e.pageY-125)+'px',left:(e.pageX+125)+'px'});
		});
		$(this).bind('mouseout',function(){$('#imagepreview').remove();});
	});
	
	$('a.prod-image-link-large, .vsPopup').each(function(){
		$(this).unbind('click').bind('click',function(){
			popMsg2('<img src="'+$(this).attr('href')+'" height="600" width="600" alt="Click to close" />',true,'','clickclose');
			return false;
		});
	});
	$('.thumbpop').thumbpop();

	/* nigelogo */


	/* --- */

	var prodimagealtdefault=$('#prod_img').attr('src');
	$('.prod-image-alts li').each(function(){
		$(this).bind('mouseover',function(){
			$('#prod_img').attr('src',$(this).attr('ref'));
		}).bind('mouseout',function(){
			$('#prod_img').attr('src',prodimagealtdefault);
		});
	});
	initBondIcons();
	
	
	
	$.nyroModalSettings({autoSizable:true, minHeight:200, css:{overflow:'visible'}});
	
	$('.bondlogoclear').bind('click',function(){
		$('#bondlogo').val(0);
		imgBondImages(0,'session');
	});
	$('.loadselectlogovault').click(function(e){
		e.preventDefault();
		var ids=$(this).attr('rel').split('-');
		if (ids[2]==undefined) ids[2]='';
		if (ids[3]==undefined) ids[3]='';
		var id=ids[0];
		var logo_id=$('#'+id).val();
		$.nyroModalManual({
			url:'/w/?img.many&selected='+ids[1]+'&sendto='+ids[0]+'&extra='+ids[2]
			, minWidth:920
			, minHeight:500
			, forceType: 'iframe'
			, endShowContent: function (){
			}
			,endRemove:function(){
				var new_logo_id=$('#'+id).val();
				if (logo_id!=new_logo_id)
				{
					needsSaving();
					if (new_logo_id>0) {
						$.get('/w/?img.misc&do=src&size=125&u='+new_logo_id,function(d){
							$('#'+id+'_img').html('<img src="'+d+'" alt="Image" style="width:125px;" />');
						});
						if (ids[2]=='bond') imgBondImages(new_logo_id,ids[3]);
					}
					else $('#'+id+'_img').html('');
				}
			}
		});
	});
	tabbytabs();
});

function tabbytabs()
{
	$('ul.tabbytabs').each(function(i){
		lis=$(this).children('li');
		lis.each(function(i){
			var el=$(this);
			if (el.hasClass('on')) tabon=i;
			el.click(function(){
				lis.removeClass('on');
				el.addClass('on');
				var block=$('.tabbyblocks > li:eq('+i+')');
				$('.tabbyblocks li').hide();
				block.show();
				var a=el.find('a');
				if (a.attr('href')!=undefined && a.attr('href').substr(0,1)!='#')
				{
					$(block).html('Loading...');
					href=a.attr('href');
					a.attr('href','#');
					$(block).load(href);
				}
				return false;
			});
		});
		if (tabon==undefined) tabon=0;
		$('.tabbyblocks > li').each(function(i){
			if (i!=tabon) $(this).hide();
		});
	});
}

function imgBondImages(logo_id,func)
{
	/*
	repost the logo 
	change all images
	*/
	if (logo_id>0)
	{
		popMsg('Branding images... Some product images may take a few seconds to refresh after this message disappears.',true);
		$('.oneclick').css('opacity',0.5);
		$.get('/w/?img.uploadtotl&u='+logo_id,function(d){
			if (func=='session') $.get('ajax.php?go=misc&do=bondlogo&bondlogo='+logo_id+'&bondlogoref='+d);
			popMsg(false);
			$('.oneclick').each(function(){
				imgBondImage($(this),d);
			});
		});
	}
	else
	{
		if (func=='session') $.get('ajax.php?go=misc&do=bondlogo&bondlogo=0&bondlogoref=0');
		$('.oneclick').each(function(){
			imgBondImage($(this),0);
		});
	}
}

function imgBondImage(elimg,d)
{
	elimg.animate({opacity:0.1},150);
	var classes=elimg.attr('class').split(' ');
	for (i=0; i<classes.length; i++)
	{
		if (classes[i].substr(0,9)=='oneclick_')
		{
			var oneclickid=classes[i].split('_')[1];
		}
		if (classes[i].substr(0,9)=='imagesize_')
		{
			var size=classes[i].split('_')[1];
		}
	}
	if (size==undefined) var size=300;/*elimg.width();*/
	var imgoffset=elimg.offset();
	var tempclass=('bonding'+oneclickid+size).replace('/','');
	$('<div class="'+tempclass+'">Branding...</div>').appendTo('body').css({position:'absolute',top:(imgoffset.top+10)+'px',left:(imgoffset.left+10)+'px'});
	if (d!=0)
	{
		var src='http://images.tradeonlysearch.com/eod/'+oneclickid + '--' + d + '--'+size+'.jpg';
		var rel='http://images.tradeonlysearch.com/eod/'+oneclickid + '--' + d + '--300.jpg';
	}
	else
	{
		var image_prod_id=oneclickid.split('/')[0];
		var src='http://toimg.no-ip.org/products/'+image_prod_id+'/'+image_prod_id+'_'+size+'.jpg';
		var rel='';
	}
	$('<img />').attr('src',src).load(function(){
		elimg.attr('src',src).attr('rel' ,rel).animate({opacity:1},300);
		$('.'+tempclass).remove();
	});
}

function pageFilter(val,lookin)
{
	$(lookin).each(function(){
		var s=$(this).attr('rel');
		if (s!=undefined)
		{
			if (s.toLowerCase().indexOf(val.toLowerCase())==-1) $(this).hide();
			else $(this).show();
		}
	});
}


function poll_popup()
{
	$.get('ajax.php?go=poll&do=unanswered',function(d){
		if (d!='')
		{
			data=d.split('|');
			if (data[0]=='t')
			{
				popMsg('<h2>Quick question...</h2>'+data[1]+'',true,'');
				$('#poll').ajaxForm({
					beforeSubmit: function(){$('#poll .submit').val('Thanks...');$('#poll').css('opacity',0);blackout(false);}
					, success: function(){popMsg(false);}
				});
			}
		}
	});
	
}


function initBondIcons()
{
	$('.tech_icon_bond').each(function(){
		var a=$(this).find('a');
		if (a.attr('rel')!=undefined)
		{
			var url=a.attr('href');
			var rel=a.attr('rel').split('_');
			var prod_id=rel[0];
			var one_click_id=rel[1];
			var stats_id=$('#stats_id').text();
			a.attr('href','#');
			$(this).click(function(){
								   
				popMsg2('<p>This product is Brand-on-Demand&trade; enabled, do you want to <br /><br /><a href="#" onclick="popMsg2(false);bondProduct('+one_click_id+');return false;" title="Brand-on-Demand">automatically place a new or existing logo in seconds using Brand-on-Demand&trade;</a>? or <br /><br /><a href="'+url+'&skipto=vs&stats_id='+stats_id+'" target="_blank" title="Virtual Sample" onclick="popMsg2(false);return true;">spend a few minutes using the old Virtual Sample method</a>?</p>',true,'','clickcloseblack','Brand-on-Demand&trade;');
				// stats
				$.get('ajax.php?go=misc&do=prodstat&tab=v&stats_id='+stats_id+'&prod_id='+prod_id,function(){});
				return false;
			});
		}
	});
};
function bondProduct(one_click_id)
{
	$('#logostorepopup').remove();
	$('#one_click_id').html(one_click_id);
	$.get('ajax.php?go=logostore-popup&do=choose&extra=bondproduct',function(d){
		blackout(true);
		$('body').append('<div id="logostorepopup">'+d+'</div>');
		$('#logostorepopup').center();
		logoChoose();
		checkForFunc();
	});
};
function bondedProductPopup(one_click_id,clickpath)
{
	$.get('ajax.php?go=bond&one_click_id='+one_click_id+'&clickpath='+clickpath,function(d){
		popMsg2(d, true, '', 'clickcloseblack', 'Brand-on-Demand&trade;');
	});
	
};

function popupLogos()
{
	$('.popuplogos a').each(function(i){
		$(this).css('text-decoration','none').bind('click',function(){return false;}).bind('mouseover',function(e){
			var imageurl=$(this).attr('href');
			$('body').append('<div class="hint" id="poppeduplogo'+i+'" style="position:absolute;top:'+(e.pageY+20)+'px;left:'+(e.pageX-100)+'px;"><img src="'+imageurl+'" /></div>');
		}).bind('mouseout',function(){
			$('#poppeduplogo'+i).remove();
		});
	});
};

function logoChoose()
{
	$('.logochoose').each(function(i){
		$(this).css('cursor','pointer').bind('click',function(){
			var id=$(this).attr('id').split('_')[1];
			var extra=$(this).attr('id').split('_')[2];
			var sendto=$(this).attr('rel');
			$('#'+sendto).val(id);
			$('#logostorepopup').remove();
			$.get('ajax.php?go=logostore&do=img&logoid='+id,function(d){$('#'+sendto+'_img').html(d)});
			blackout(false);
			if ($('#tu_logo_prefix').html()!=undefined)
			{
				var tu_logo_prefix=$('#tu_logo_prefix').html();
				if (extra=='oneclick')
				{
					popMsg('Refreshing product images.',true,'loading');
					setTimeout("popMsg(false);",2500);
					$('.oneclick').each(function(i){
						var oneclickid=$(this).attr('class').split(' ')[1].split('_')[1];
						if (oneclickid!=undefined)
						{
							$(this).attr('src','http://toimg.no-ip.org/eod/eod/'+oneclickid + '--' + tu_logo_prefix + id + '--126.jpg').attr('rel' ,'http://toimg.no-ip.org/eod/'+oneclickid + '--' + tu_logo_prefix + id + '--300.jpg');
						}
					});
				}
				else if (extra=='bondproduct')
				{
					var one_click_id=$('#one_click_id').html();
					bondedProductPopup(one_click_id,tu_logo_prefix + id);
				}
			}
			needsSaving();
		});
	});
};

function checkForFunc()
{
	$('div').each(function(){
		if ($(this).attr('rel')!=undefined) {
			var rel=$(this).attr('rel').split(' ');
		}
		else
		{
			var rel=new Array();
		}
		if (rel.length>0 && rel[0]=='func')
		{
			$(this).css('cursor','pointer').bind('click',function(){
				var func=rel[1];
				if (func=='close_parent')
				{
					$(this).parent().remove();
					blackout(false);
				}
				else if (func=='expand' && rel.length>2)
				{
					var cffclass=rel[2];
					$('.'+cffclass+' blockquote').show();
				}
				else if (func=='collapse' && rel.length>2)
				{
					var cffclass=rel[2];
					$('.'+cffclass+' blockquote').hide();
				}
			});
		}
	});

};

function blackout(bool)
{
	if (bool)
	{
		$('body').append('<div id="blackout" style="background:#000;display:block;position:absolute;top:0;left:0;width:'+$(document).width()+'px;height:'+$(document).height()+'px;z-index:998;"></div>');
		$('#blackout').css('opacity',0.7);
	}
	else
	{
		$('#blackout').remove();
	}
};

function popMsg(s,bk,t,c)
{
	$('#popMsg').remove();
	blackout(false);
	if (s!=false)
	{
		blackout(bk);
		if (c!=undefined) 
		{
			for (i=1;i<=c;i++) {setTimeout("$('#popmsgcountdown').text("+(1+c-i)+")",1000*i);}
			countdown='<br />About <span id="popmsgcountdown">'+c+'</span> seconds remaining.';
		}
		else countdown='';
		$('body').append('<div id="popMsg" class="popMsg"><div class="hd"></div><div class="bd">'+s+countdown+'</div></div>');
		if (t=='alert') $('#popMsg').addClass('popMsgError');
		else if (t=='loading') $('#popMsg').addClass('popMsgLoading');
		$('#popMsg').center();
	}
};

function popMsg2(s,bk,c,func,t)
{
	$('#popMsg').remove();
	blackout(false);
	if (s==false) return false;
	var title='';
	if (s!=false)
	{
		blackout(bk);
		if (t!=undefined && t!='') title='<div class="hd"><h3>'+t+'</h3></div>';
		$('body').append('<div id="popMsg" class="popMsg popMsg2">'+title+'<div class="bd">'+s+'<p style="text-align:right;"><a href="#" onclick="return popMsg2(false);" title="Close">X</a></p></div></div>');
		if (c=='alert') $('#popMsg').addClass('popMsgError');
		else if (c=='loading') $('#popMsg').addClass('popMsgLoading');
		$('#popMsg').center();
	}
	if (func=='clickclose')
	{
		$('#blackout,#popMsg').attr('title','Click to close').css('cursor','pointer').bind('click',function(){$('#popMsg,#blackout').remove();});
	}
	if (func=='clickcloseblack')
	{
		$('#blackout').attr('title','Click to close').css('cursor','pointer').bind('click',function(){$('#popMsg,#blackout').remove();});
	}
};

function topMsg(s,wait,t)
{
	$('#topMsg').hide();
	if (s!=false)
	{
		if (t=='alert') $('#topMsg').addClass('error');
		$('#topMsg').html(s);
		if (wait>0) setTimeout("$('#topMsg').show(200);",wait);
		else $('#topMsg').show(300);
	}
};

function loading(bool)
{
	if (bool) popMsg('Loading...',true,'loading');
	else popMsg(false);
};

function initCatDir()
{
	$('#catdir').find('li.ecs').each(function(){
		var a=$(this).find('a');
		var pa=$(this);
		var href=a.attr('href');
		var stuff=$(this).attr('class').split(' ')[1];
		var prod_category_id=stuff.split('_')[1];
		var cct=stuff.split('_')[2];
		var pct=stuff.split('_')[3];
		
		if (cct>0)
		{
			$(this).attr('class','');
			a.attr({href:'#',title:'Click to expand, double click to view category'}).prepend('+ ');
			a.bind('dblclick',function(){
				window.location.href='index.php?go=search&ca='+prod_category_id;
			});
			$(this).append('<div id="ecs_load_'+prod_category_id+'"></div>');

			a.bind('click',function(){
				if ($('#ecs_load_'+prod_category_id).html()=='')
				{
					$('#ecs_load_'+prod_category_id).html('Loading').show(0);
					$.get('ajax.php?go=dir&cat_id='+prod_category_id,function(d){
						$('#ecs_load_'+prod_category_id).html(d);
						initCatDir(); // run script again to run on newly opened info.
					})
				} 
				else
				{
					$('#ecs_load_'+prod_category_id).hide(200);
				}
				return false;
			});
		}
	});
};

$.fn.extend({
	dddrop: function(){
		var anchorname = (location.href.split('#')[1]); 
		$(this).each(function(){
			var el=$(this);
			var id=$(this).attr('id').split('_')[1];
			block=$(this).find('blockquote');
			em=$(this).find('h3 em');
			em.html('&#9658;');
			anch=$(this).find('a.ddanch').attr('name');
			block.hide();
			rel=$(this).attr('rel');
			if (rel!=undefined)
			{
				rels=rel.split(' ');
				var once='';
				var func='';
				for (j=0;j<rels.length;j++)
				{
					if (rels[j]=='open') { block.show(); }
					if (rels[j].substr(0,4)=='once') {
						once=rels[j].split(':')[1];
						//eval(once);
					}
					if (rels[j].substr(0,4)=='func') {
						func=rels[j].split(':')[1];
						//eval(func);
					}
				}
			}
			if (anchorname!=undefined && anch==anchorname) 
			{
				block.show();
				eval(func);
				eval(once);
				once='';
				em.html('&#9660;');
			}
			el.find('input').bind('change',function(){needsSaving();});
			el.find('h3 .toggle').bind('click',function(){
				/* need to re-find these objects in the click otherwise it affects the last one only */
				var el=$(this).parents('li');
				block=el.find('blockquote');
				em=el.find('h3 em');
				if (el.find('h3 .x input:first').length) checkbox=el.find('h3 .x input:first');
				else checkbox=false;
				if (!checkbox || (checkbox && checkbox.is(':checked')))
				{
					block.toggle(100,function(){
						if (block.is(':visible')) 
						{
							if (checkbox) checkbox.val(2); /* use checkbox val to send info to db that list item was left open */
							eval(func);
							eval(once);
							once='';
							em.html('&#9660;');
						}
						else 
						{
							em.html('&#9658;');
							if (checkbox) checkbox.val(1);
	
						}
					});
				}
			});
			$(this).find('h3 .handle').html('&nbsp;&nbsp;&nbsp;&nbsp;').css('cursor','move').attr('title','Drag and drop to move this product');
			$(this).find('h3 .x').each(function(){
				var checkbox=$(this).find('input:first');
				checkbox.css({position:'absolute', left:'-1000px'});
				if (checkbox.is(':checked')) 
				{
					el.css('opacity',1);
				}
				else 
				{
					block.hide();
					el.css('opacity',0.3);
				}
				
				$(this).bind('click',function(){
					needsSaving();
					var el=$(this).parents('li');
					if (el.find('h3 .x input:first').length) checkbox=el.find('h3 .x input:first');
					else checkbox=false;
					if (checkbox)
					{
						if (checkbox.is(':checked')) 
						{
							el.css('opacity',1);
							el.find('h3 em').html('&#9658;');
							block=el.find('blockquote');
							if (el.hasClass('prodsli')) 
							{
								el.attr('title','');
								
							}
						}
						else 
						{
							block.hide();
							el.css('opacity',0.3);
							if (el.hasClass('prodsli')) 
							{
								el.attr('title','Unmark for removal to view/edit'); //$('#prodsli_'+id+'')
								el.find('h3 em').html('Remove:');
							}
							else el.find('h3 em').html('&#0149;');
							
								
						}
					}
					/*
					if ($('#prods_'+id).val()==1)
					{
						block.hide();
						$('#prods_'+id).val(-1);
						el.css('opacity',0.3).attr('title','Unmark for removal to view/edit'); //$('#prodsli_'+id+'')
						$('#prodsli_'+id+' h3 em').html('Remove:');
					}
					else
					{
						$('#prods_'+id).val(1);
						el.css('opacity',1).attr('title','');
						$('#prodsli_'+id+' h3 em').html('&#9658;');
					}
					*/
				});
			});
		});
	}
});
