
/******************************************************************/

/* Subfilter drop down menu */
var mainMenuOptions = {minWidth: 155, copyClassAttr: true, classDropBox: "none"};
//var moreArtInfoOptions = {minWidth: 255, copyClassAttr: true, classDropBox: "moreArt"};
var userMenuOptions = {minWidth: 135, copyClassAttr: true, classDropBox: "mailBox"};
var upSubMen = {minWidth: 135, copyClassAttr: true, classDropBox: "greybox"};
var userSubmitOptions = {minWidth: 105, copyClassAttr: true, classDropBox: "submit"};


 
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var iePos = appVer.indexOf('msie');
if (iePos !=-1) {
	is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
	is_major = parseInt(is_minor);
}
var IE  = ((iePos!=-1));
var IE6 = (IE && is_major == 6);


/* AJAX LOADING ANIMATION */
xajax.loadingFunction = function(){xajax.$('loadingMessage').style.display='block';};
xajax.doneLoadingFunction = function() { xajax.$('loadingMessage').style.display = 'none'; }


var W3CDOM = (document.createElement && document.getElementsByTagName);


var flags = new Array("ad","ae","af","ag","ai","al","am","an","ao","ar","as","at","au","aw","ax","az","ba","bb","bd","be","bf","bg","bh","bi","bj","bm","bn","bo","br","bs","bt","bv","bw","by","bz","ca","catalonia","cc","cd","cf","cg","ch","ci","ck","cl","cm","cn","co","cr","cs","cu","cv","cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","eh","england","er","es","et","europeanunion","fam","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge","gf","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","in","io","iq","ir","is","it","jm","jo","jp","ke","kg","kh","ki","km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc","md","me","mg","mh","mk","ml","mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my","mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz","om","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa","re","ro","rs","ru","rw","sa","sb","sc","scotland","sd","se","sg","sh","si","sj","sk","sl","sm","sn","so","sr","st","sv","sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to","tr","tt","tv","tw","tz","ua","ug","um","us","uy","uz","uk","va","vc","ve","vg","vi","vn","vq","vu","wales","wf","ws","ye","yt","za","zm","zw");

/* Cancel story action timeout */
var cancelTimeOut;





jQuery.fn.extend({
		
		// Remove HTML Tags from object html()
		removeHTMLTags: function(txt) {
			txt = txt.replace(/&(lt|gt);/g, function (strMatch, p1){
				return (p1 == "lt")? "<" : ">";
			});
			txt = txt.replace('\'',' ');
			return txt.replace(/<\/?[^>]+(>|$)/g, "");
		},
				 
				 
				 
				 
		addCtoHiddenDiv: function(id) {
		
		if( $('#'+id).css('display')=='none' ) {
			$('#'+id).attr('id',($('#'+id).attr('id'))+'c');
			$('#'+id+'c *').each( function() {
												if($(this).attr('id')!='') { 
													$(this).attr('id',($(this).attr('id'))+'c')
												}
										   });
			$('#'+id+'c form').each( function() {
												if($(this).attr('name')!='') { 
													$(this).attr('name',($(this).attr('name'))+'c')
												}
										   });
		}
	
	},

	removeCFromHiddentDiv: function(id) {
		
			$('#'+id+' *').each(function(){
												if($(this).attr('id')!='') { 
														$(this).attr('id',($(this).attr('id')).slice(0,-1)) 
												}
										  });
			$('#'+id+' form').each( function() {
													if($(this).attr('name')!='') { 
														$(this).attr('name',($(this).attr('name')).slice(0,-1)) 
													}
											   });
			$('#'+id).attr('id',($('#'+id).attr('id')).slice(0,-1));
		
	},


  hidePopLogin: function() {
	  $('#wlog1').remove();
	  $('#wlog2').remove();
  },

  assignLogin: function() {
	$('a.login').click(function (e) {
        e.preventDefault();
        // load the contact form using ajax
	    rurl = $(this).attr('title');

        $.get("/user/login_modal", function(data){  //Replace with login.php Path
            // create a modal dialog with the data
            $(data).modal({
                close: false,
                overlayId: 'contact-overlay',
                containerId: 'contact-container',
                onOpen: contact.open,
                onShow: contact.show,
                onClose: contact.close
            });
       	 });
    	});  
  },

  showPopLogin: function(rurl2) {
	if($('#wlog1').length <= 0) {
		var queryString = window.location.search.substring(1);
		var rurl = '?rurl=' + window.location.pathname;
		if(queryString != '') rurl = rurl + '?' + queryString;
		
		$(this).append("<div  onclick='$(this).hidePopLogin()'  class='hid' id='wlog1'> </div><div class='warning hid' id='wlog2' onclick='$(this).hidePopLogin()'><span class='close'>X</span><h3>Sorry</h3><p><a title='"+rurl2+"' class='login blue'>Login</a> or <a name='goCom' href='/register/" + rurl + "' class='blue'>register</a> to endorse this article.<br/> It will take only a few seconds, we promise!</p>"); 
		$('').assignLogin();
		$('#wlog1').slideDown();
		$('#wlog2').slideDown();
	}
  
  },
  
   showPopLoginVid: function(rurl2) {
	
	if($('#wlog1').length <= 0) {
		var queryString = window.location.search.substring(1);
		var rurl = '?rurl=' + window.location.pathname;
		if(queryString != '') rurl = rurl + '?' + queryString;
		
		$(this).append("<div  onclick='$(this).hidePopLogin()'  class='hid' id='wlog1'> </div><div class='warning hid' id='wlog2' onclick='$(this).hidePopLogin()'><span class='close'>X</span><h3>Sorry</h3><p><a title='"+ rurl2 +"' class='login blue'>Login</a> or <a name='goCom' href='/register/" + rurl + "' class='blue'>Register</a> to rate this video.<br/> It will take only a few seconds, we promise!</p>"); 
		$('').assignLogin();
		$('#wlog1').slideDown();
		$('#wlog2').slideDown();
	}
  
  },
  
   showPopLoginAbuse: function(id) {
	
	if($('#wlog1').length <= 0) {
		var queryString = window.location.search.substring(1);
		var rurl = '?rurl=' + window.location.pathname;
		if(queryString != '') rurl = rurl + '?' + queryString;
		
		$(this).append("<div  onclick='$(this).hidePopLogin()'  class='hid' id='wlog1'> </div><div class='warning hid' id='wlog2' onclick='$(this).hidePopLogin()'><span class='close'>X</span><h3>Sorry</h3><p><a title='<? echo $_SERVER[REQUEST_URI];?>' class='login blue'>Login</a> or <a name='goCom' href='/register/" + rurl + "' class='blue'>register</a> to report abuse.<br/> It will take only a few seconds, we promise!</p>"); 
		$('').assignLogin();
		$('#wlog1').slideDown();
		$('#wlog2').slideDown();
		s=window.location+'';window.location = s.substring(0,s.indexOf('#top')) + '#top';
	}
  
  },


  sendMessageLinks: function() { 
		$('a.sendMessage').click(function (e) {
			e.preventDefault();
			var api_data = {
				username: $(this).attr('username'),
				user_id: $(this).attr('userid')
			};
			$.get('/messages/compose_modal', api_data, function(data) {
				$(data).modal({
					close: false,
					overlayId: 'contact-overlay',
					containerId: 'contact-container'
				});
			});
		});
   },
				 
   
   toggleUserInfo: function() {
        return this.each( 
            function() {
                $(this).find("#uITop").toggle();
                $(this).find(".links").toggle();
                $(this).find(".close").toggleClass('open');
            })
    },
	
	hideClueTip: function() {
		$("#cluetip").hide();	
	},
	
	
	makeItDisappear:function(){
		if(IE)  $(this).hide(); 
		else    $(this).slideUp();
	},
	
	makeItReappear:function(){
		if(IE)  $(this).show(); 
		else   $(this).slideDown();
	},

	makeItFadeOut:function(){
		$(this).fadeOut('slow'); 
	},


	toggleUserProfileBox: function(title) {
				if  ($('#'+title).css("display")=="block")	$('#'+title).makeItDisappear();
				else $('#'+title).makeItReappear(); 
			  
	},
	
	flags: function() {
        return this.each( 
			function() {	
				var cnt = $(this).attr('title');
				var i = flags.length;
				$(this).html("");
				
				while(i>0) {
					if(flags[i]==cnt) {
						i=-1;	
						$(this).append("<img src='/img/flags/"+ $(this).attr('title')  +".gif' alt='"+ $(this).attr('title')  +"' />");	
					} else {
						i-=1;	
					}
				}
		 })
	},
	
	hideStory: function(title) {
		clearTimeout(cancelTimeOut);
		var content = title.split("_");
	        var confirmation = "";

        	confirmation = confirm("Are you sure you wish to delete this item?");	
		toHide = content[1] + "_" + content[2];
		if (confirmation == true) {
			// call a page to update the database
			var number = document.getElementById('activity_count').innerHTML;
			var testreturn = xajax_processDeleteActivity(content[2], number);

			$("#" + toHide).makeItFadeOut();
			$("#"+content[0]+"_cancel").attr("rel",toHide);
			$("#"+content[0]+"_cancel").makeItReappear();
			cancelTimeOut = setTimeout(function() {$("#"+content[0]+"_cancel").makeItFadeOut()}, 10000);
		}
	},



	trackCountry: function(title) {
		if (title == 'US') {
			$('#zipBlock').show();
		}
		else {
			$('#zipBlock').hide();
		}
	},
	

	cssCheckbox: function () {
		$("input[@type='checkbox'] + label", this)
			.hover( 
				function() { $(this).addClass("over"); },
				function() { $(this).removeClass("over"); }
			)
			.click( function() {
				$(this).toggleClass("checked")
			})

	},
	
	resetCheck: function() {
		$("input[@type='checkbox'] + label", this).removeClass("checked")
	},
	redirectPage: function(url) {
		window.location.href=url;
	},
	
	
	showInternDiv: function(what,articleId,base) {
			$(".hid").slideUp();
			$("#loader").show();
			// displayModalDiv(what + articleId, what + "#" + base);
			//$.modal($("#" + what + articleId));
			displayModalDiv(what + articleId, what + "#" + base);	 
			$("#" + what + articleId).makeItReappear();	
			
			/*
			$("#" + what + articleId).modal({
				 close: false,
				 overlayId: 'contact-overlay',
				 containerId: 'contact-container',
				 onOpen: contact.open,
				 onShow: contact.show,
				 onClose: contact.close
			});
			 */
			return false;
	},
	
	
	
	
	showReply:function(is_quoting) {
		if(!is_quoting) { is_quoting = 0; }
            $(this).find('#comment_is_quote').val(is_quoting);
			$('.hid').slideUp();
			if(IE) $(this).show();
			else   $(this).slideDown();
			$(this).find('textarea').attr('value','');
	},
	
	
	// QUOTES ON FORUMS:
	replace:function() {
		var stack = [];
		return this.domManip(arguments, true, 1, function(a){
			this.parentNode.replaceChild( a, this );
			stack.push(a);
		}).pushStack( stack );
	},
	
	
	replaceToTextArea:function() {
		var id_p= $(this).attr('id');
		if($(this).length>0)
		$(this).replace("<textarea id='" + id_p + "' value='" + $().removeHTMLTags($('#'+id_p).html()) + "' style='width:95%' rows='6'>"+$('#'+id_p).html()+"</textarea>");
	},
	
	replaceToP:function() {
		var id_p= $(this).attr('id');	
		if( ($(this).length>0) && ($('#'+$(this).attr('id')+"_clone").length>0)) {
			//$(this).replace("<p id='" + id_p +"'>"+$('#'+id_p).html()+"</p>");
			$('#'+$(this).attr('id')+"_clone").show();
			var currentID = $(this).attr('id')+"_clone";
			var newId = currentID.substring(0,currentID.length-6);
			$('#'+$(this).attr('id')+"_clone").attr('id',newId);
			$(this).remove();
		}
		
	},
	
	update:function(textAreaQuote_id) {
		var range = $(this).getSelection();
		if(range.text!='') {
			$('#'+textAreaQuote_id).html("Quote:" +  "[Quote]" + range.text + "[/Quote]\n" ).attr("value", "[Quote]" + range.text + "[/Quote]\n");
		}
	},

	quote:function(id_p,textAreaQuote_id) {
		
		 /* Cloning:
		 var clone = $('#'+id_p).clone(true);
		 $('#'+id_p).after(clone);
		 $(clone).hide();
		 $(clone).attr('id',$(clone).attr('id')+"_clone");
		 */
		 
		 
		 var clone = $(document.createElement("p"));
		 $('#'+id_p).after(clone);
		 $(clone).hide();
		 $(clone).html($('#'+id_p).html())
		 $(clone).attr('id',id_p+"_clone");
		 
		
		// Transform p into textArea	
		$('#'+id_p).replaceToTextArea();
		
		// Show 'Express yourself box
		$(this).find('#comment_is_quote').val(1);
		$('.hid').slideUp();
		if(IE) $(this).show();
		else   $(this).slideDown(); 
	
		$('#'+textAreaQuote_id).html("---Select the text you want to quote---").attr('value','---Select the text you want to quote---');
	
		
	
		$('#'+id_p).keydown(function() {var range = $(this).getSelection();if(range.text!='') {$('#'+textAreaQuote_id).html("Quote:" +  "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n" ).attr("value", "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n");}})
				 .keyup(function() {var range = $(this).getSelection();if(range.text!='') {$('#'+textAreaQuote_id).html("Quote:" +  "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n" ).attr("value", "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n");}})
				 .mousedown(function() {var range = $(this).getSelection();if(range.text!='') {$('#'+textAreaQuote_id).html("Quote:" +  "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n" ).attr("value", "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n");}})
				 .mouseup(function() {var range = $(this).getSelection();if(range.text!='') {$('#'+textAreaQuote_id).html("Quote:" +  "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n" ).attr("value", "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n");}})
				 .mousemove(function() {var range = $(this).getSelection();if(range.text!='') {$('#'+textAreaQuote_id).html("Quote:" +  "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n" ).attr("value", "[Quote]" + $('').removeHTMLTags(range.text) + "[/Quote]\n");}})
		
	}
	// END QUOTES ON FORUM 	

})








$(window).scroll(function()
{
  $('#loader').css("top",$(window).scrollTop()+"px");
});



/* When Document is Loaded */
$(function() {
		   
		   
	// PNG IE6 
	if(IE6) { $(document).pngFix(); }
	  
	  
	// Profile Toggle
	if(!IE6)
	$('.toggle').click(function(){$(this).toggleUserProfileBox($(this).attr("title"))});
	
	// Hide Story
	$('.to_hide').click(function(){$(this).hideStory($(this).attr("rel"))});

	// Cancel hide story
	$('.cancel').click(function(){clearTimeout(cancelTimeOut);$("#"+$(this).attr("rel")).makeItReappear();$(this).makeItFadeOut() });
	
	// Tabs on UP Home
	$('.tabLink').click(function(){$('.tabLinkC').hide();$($(this).attr('href')).show();return false;});
	
	// Tabs on Up Friends/Foes
	$('.tabLink2').click(function(){$($(this).attr('href')).show(); $($(this).attr('name')).hide(); return false;});

	// Show/Hide Zip code 
	$('.track_country').click(function(){$(this).trackCountry($(this).attr("value"))});
	
	// DROP DOWN
	$('#mmMore').menu(mainMenuOptions); 
	//$('.moreArtInfo').menu(moreArtInfoOptions); 
	$('#mailBoxDD').menu(userMenuOptions); 
	$('#friendsDD').menu(userMenuOptions);
	$('#uploadSomething').menu(userSubmitOptions);
	$('#saySomething').menu(userMenuOptions);
	$('#submitUPMenu').menu(userMenuOptions);
	$('#upSubMen').menu(userSubmitOptions);
	$('#blogSubMen').menu(userSubmitOptions);
	$('#blogTMenu').menu(userSubmitOptions);
	
	// FLAGS (update pix)
	$('span.country').flags();
	
	// TOOL TIPS ON LARGE FORM
	if(!IE6) {
		$('input').focus(function(){$('.tip').hide();});
		$('.tipped').focus(function(){$('.tip').hide();$(this).children('.tip').show()});
		$('input').click(function(){$('.tip').hide();});
		$('.tipped').click(function(){$('.tip').hide();$(this).children('.tip').show()});
		$('.tip').css('cursor','pointer').click(function(){$(this).fadeOut('slow')});
	}
	
	

	// AUTO SCROLL
	var val = $.cookie(location.href.replace(/=/g,''));
	if(val!=null) { $('html,body').scrollTop(val); }

	// EMPTY recover_captcha fields
	$('#recover_captcha').attr('value','');
  

	// Counters
	$("textarea.count[@maxlength]").charRemain();
	$("input.count[@maxlength]").charRemain();


})









// On Window Unload 
/*
$(window).unload( function() {});*/

/** ONLY FOR IE and FIREFOX */
function scrollCookie() {
	
	var scrollTop=0;
							   
	if (typeof(window.pageXOffset) == 'number') {
		 scrollTop = window.pageYOffset;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {	
		 scrollTop = document.body.scrollTop;
	}
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		 scrollTop = document.documentElement.scrollTop;
	}
	var date = new Date();
	date.setTime(date.getTime() + (3 * 60 * 1000));
    $.cookie(location.href.replace(/=/g,''), scrollTop, { path: '/', expires: date });
	
}
 
window.onbeforeunload = scrollCookie;


function processProfileEmailForm() {

    document.getElementById('email_submit').disabled = true;
    document.getElementById('email_submit').value    = 'Saving...';

    xajax_processProfileEmailForm(xajax.getFormValues('email_form'));

   return false;
}


function processProfilePasswordForm() {

    document.getElementById('password_submit').disabled = true;
    document.getElementById('password_submit').value    = 'Saving...';

    xajax_processProfilePasswordForm(xajax.getFormValues('password_form'));

   return false;
}


function checkUrlExists() {
   $('#loader').slideDown();
   xajax_checkUrlExists(document.getElementById('entry_sourceurl').value, 'news');
   $('#loader').slideUp();
   return false;
}

function checkBlogUrlExists() {
   $('#loader').slideDown();
   xajax_checkUrlExists(document.getElementById('entry_sourceurl').value, 'blog');
   $('#loader').slideUp();
   return false;
}


function cleanString(s) {
	var myRegExp = new RegExp("\"|/>|'|\\\\|\"","g");
	return  s.replace(myRegExp, "");
}

 