(function($){
	
$.fn.extend({	
	showArtistDescription: function(offset_layer, offset_out)
	{

		var layer = $(this).parent().find('.artist-data');
		var out = $(this).parent().find('.out');
		
		$('.artist-data').css('display', 'none');
		$('.out').css('display', 'none');

		layer.css('display', 'block');
		out.css('display', 'block');


		// offset layer to artist image
		var pos = $(this).position();
		pos.top -= offset_layer;
		pos.left -= offset_layer;
		layer.css( pos );
		
		// offset 'out' layer to artis div
		pos.top -= offset_out;
		pos.left -= offset_out;
		out.css( pos );
		
		
		out.mouseover(function(){
			layer.css('display', 'none');
			$(this).css('display', 'none');
			
		})
	},
	showElement: function(id, offset_y){
		
		var _self = getObject(id);
		
		if (_self.is(":hidden")){
		
			_self.css('top', '0');
			_self.css('left', '0');
			
			var pos = $(this).offset();
			pos.top -= parseInt(_self.height() + offset_y);
			_self.css( pos );
			
			_self.show("slow");
			
		}	

	},
	showElementVisibility: function(id, offset_x, offset_y){
		
		var obj = $('#' + id);
		var scrollTop 	= $(document).scrollTop();
		var scrollLeft 	= $(document).scrollLeft();
		
		obj.css('visibility', 'hidden');
		
		var pos = $(this).offset();
		pos.top 	-= parseInt(offset_y);
		pos.left 	+= parseInt(offset_x);
		obj.css( pos );
		
		obj.css('visibility', 'visible');
		
		if (scrollTop > pos.top){
			obj.animate({
				top: scrollTop + 'px'
			});
		}
	},
	limitTextArea: function(textId, limit, infoDiv){
		 
		var text = $('#' + textId).val(); 
		var textLength = text.length;
		 
		 if(textLength > limit){
		 
			 $('#' + infoDiv).html('You cannot write more then ' + limit + ' characters!');
			 $('#'+textId).val(text.substr(0, limit));
		 
			 return false;
		 
			}else{
		 
				$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
				return true;
				
			}
	},
	resetInput: function(){
		
		var _self = $(this);
		
		if (_self.hasClass('error')){
			_self.removeClass('error');
			_self.val('');
		}
		
	},
	w_PostBack: function(form, target, action){
		
		_self = $(this);
		_form = getObject(form);
		_target = getObject(target);
		
		getObject(faction).val(action);
		
		var options = { 
			target:        '#' + target,   // target element(s) to be updated with server response 
			type:					 'post',	
			beforeSubmit:  function (){
				activateProgress(_self);
			},
			success:       function (statusText, responseText) {
				deactivateProgress();
			}
		};		
		
		_form.submit(function(){
			
			$(this).ajaxSubmit(options);
			
			return false;
			
		});
					
		$form.submit();
	},
	clearSearchSession: function(){
		
		var _self = $(this);
		href  = _self.attr('href');
		
		$.ajax({
			url: baseUrl + '/clear',
			cache: false,
			beforeSend: function(){ _self.addClass('cursor-wait');},
			success: function(data) { window.location = href; }
		});
		
		return false;
	}
	});
})(jQuery);

	function getNameSelectedImage(container){

		var r = -1;
		
		$("#" + container + " input[name=ch]'").each(function(){
			
					if (this.checked)
						r =  unescape(this.value);
				
		});
		
		
		return r;
	}
	
	
	function test(){
		alert('2');
	}


	function setMenu(){
		
		current = null;
		
			$(document).ready(function(){
				
				$('.menu li.menu-item').hover(
						function () {
							if (this.id != 'menu-item-selected'){
								//$('#menu-item-selected ul').hide();
								$('#menu-item-selected').removeClass('menu-item-selected');
								//$('#' + this.id + ' ul').show();
							}	
						}, 
						function () {
							if (this.id != 'menu-item-selected'){
								//$('#' + this.id  + ' ul').hide();
								$('#menu-item-selected').addClass('menu-item-selected');
								//$('#menu-item-selected ul').show();
							}	
						}
				);
				
			});		
	}
	
	function setMouseOverBorder(){
	
			$(document).ready(function(){
					
				$('div.box-artist').mouseover(function(){
					$(this).find('div.border-mouseover-left').css('visibility', 'visible');
					$(this).find('div.border-mouseover-right').css('visibility', 'visible');
					$(this).find('div.border-mouseover-top').css('visibility', 'visible');
					$(this).find('div.border-mouseover-bottom').css('visibility', 'visible');
					
					if ($('body').hasClass('management'))
						$(this).find('div.button-edit').css('visibility', 'visible');
				});
				
				$('div.box-artist').mouseout(function(){
					$(this).find('div.border-mouseover-left').css('visibility', 'hidden');
					$(this).find('div.border-mouseover-right').css('visibility', 'hidden');
					$(this).find('div.border-mouseover-top').css('visibility', 'hidden');
					$(this).find('div.border-mouseover-bottom').css('visibility', 'hidden');
					
					if ($('body').hasClass('management'))
						$(this).find('div.button-edit').css('visibility', 'hidden');
				});
				
			});	
	}
	
	function setOpenCallMouseOverBorder(){
	
			$(document).ready(function(){
					
				if ($('body').hasClass('management')){
					
					$('#open-call-container').mouseover(function(){
						$(this).find('div.border-opencall-mouseover-left').css('visibility', 'visible');
						$(this).find('div.border-opencall-mouseover-right').css('visibility', 'visible');
						$(this).find('div.border-opencall-mouseover-top').css('visibility', 'visible');
						$(this).find('div.border-opencall-mouseover-bottom').css('visibility', 'visible');
						$(this).find('div.button-edit').css('visibility', 'visible');
					});
				
					$('#open-call-container').mouseout(function(){
						$(this).find('div.border-opencall-mouseover-left').css('visibility', 'hidden');
						$(this).find('div.border-opencall-mouseover-right').css('visibility', 'hidden');
						$(this).find('div.border-opencall-mouseover-top').css('visibility', 'hidden');
						$(this).find('div.border-opencall-mouseover-bottom').css('visibility', 'hidden');
						$(this).find('div.button-edit').css('visibility', 'hidden');
					});
				
				};	
			});
	}
	
	function setEditBorder(element){
	
			/*$(document).ready(function(){
				
				var _self = getObject(element);
					
				if ($('body').hasClass('management')){
					
					_self.mouseover(function(){
						$(this).find('div.border-opencall-mouseover-left').css('visibility', 'visible');
						$(this).find('div.border-opencall-mouseover-right').css('visibility', 'visible');
						$(this).find('div.border-opencall-mouseover-top').css('visibility', 'visible');
						$(this).find('div.border-opencall-mouseover-bottom').css('visibility', 'visible');
						$(this).find('div.button-edit').css('visibility', 'visible');
					});
				
					_self.mouseout(function(){
						$(this).find('div.border-opencall-mouseover-left').css('visibility', 'hidden');
						$(this).find('div.border-opencall-mouseover-right').css('visibility', 'hidden');
						$(this).find('div.border-opencall-mouseover-top').css('visibility', 'hidden');
						$(this).find('div.border-opencall-mouseover-bottom').css('visibility', 'hidden');
						$(this).find('div.button-edit').css('visibility', 'hidden');
					});
				
				};	
			});*/
	}		
	
	
	function customizeSelectBox(){
		$(document).ready(function(){
			//$('#selectbox-search-country').selectbox({debug: true});
			//$('#selectbox-search-category').selectbox({debug: true});
			//$('select').selectbox({debug: true});
			$("select").selectbox();
		});		
	}
	
	function showErrorMessages(){
		$(document).ready(function(){
			if ($("#message-error")){
				$('#message-error').dialog(
				{ 
					width: 'auto',
					height: 'auto',
					bgiframe: false,
					resizable: false,
					zIndex: 1050,
					dialogClass: 'box-alpha'
				}
				);
			}
		});	
	}
	
	/*function showWidget(title, type, nid){
		$(document).ready(function(){
			
			$('body').append('<div id="widget" title="' + title +'"></div>');
			$('#widget').append('<div class="dialog-content"></div>');
			$('#widget .dialog-content').append('<div id="wooloo-widget"><h1>Alternative content</h1><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>');
			
			if (type)
					xmlPath = baseUrl + "/xml/widget.xml&widgetType=ARTIST"
			else
					xmlPath =  baseUrl + "/xml/widget.xml&widgetType=EXHIBITION"
			
			var flashvars = {
				basePath: baseUrl + '/',
				nodeId: (nid) ? nid : -1,
				xmlPath: xmlPath 
			};
			
			var params = {
				menu: "false"
			};
			
			var attributes = {
				id: "flashWidget",
				name: "flashWidget"
			};
				
			swfobject.embedSWF(baseUrl + "/flash/wooloo-widget.swf", "wooloo-widget", "300", "400", "9.0.0", baseUrl + "/js/swfobject/expressInstall.swf", flashvars, params, attributes);			
			
			if ($("#widget")){
				
					$('#widget').dialog(
					{ 
						autoOpen: false,
						draggable: true, 
						width: 340,
						height: 476,
						bgiframe: false,
						resizable: false,
						zIndex: 1050,
						open: function() { $('#widget').fadeIn();},
						close: function() {$('#widget').dialog('destroy'); $('#widget').remove() ;}
					}
					);	
					
					$('#widget').dialog('open');
			}
		});	
	}
	
	function startUlImage(){
		$(document).ready(function(){
			if ($(".ul-images")){
				$(".ul-images").each(function(){
					$(".ul-images :first-child").show();
				});
			}
		});	
	}
	
	function showUlChild(parent, id){
		$(document).ready(function(){
			if ($("#" + parent)){
				$("#" + parent + ' li:visible').hide();
				$('#' + parent + ' li:eq(' + id + ')').fadeIn('slow');
			}
		});	
	}*/

	function closeWolooPopup(id){
		$('#' + id).dialog('close');
	} 

	function closeImageResizer(){
		
		closeWolooPopup('cv-image-resizer');
		
	}	
	
	function activateProgress(obj){
		
		var $self = $('#loader');
		offset = obj.offset();
		offsetX  = getSafeXPosition($self, offset.left, '');
		setLayerPosition($self, offsetX , offset.top);
		showLayer($self, 200);
		
	}
	
	function deactivateProgress(){
		var $self = $('#loader');
		hideLayer($self);
	}
	
	function setLayerPosition(obj, x, y){
		
		cssProp = {
			left: getPixelUnit(x),
			top: getPixelUnit(y)
		};
		
		obj.css(cssProp);
	}
	
	function getSafeXPosition(obj, startX, maxWidth){
		
			var bodyWidth = $('body').width();
			var popupWidth = (maxWidth) ? maxWidth : obj.width();
			var scrollLeft = $('body').scrollLeft();
			
			if(startX + popupWidth > bodyWidth + scrollLeft -10){
				startX = bodyWidth + scrollLeft - popupWidth-10;
			}
			
			return startX;
	}
	
	function getPixelUnit(pixels){
		return pixels + "px";
	}
	
	function getObject(id){
		return $('#' + id);
	}

	function showLayer(obj, speed){
		obj.fadeIn(1000);
	}

	function hideLayer(element){
		element.hide();
	}
	
	function slideLayer(element, offset, speed){
		
		scrollTop  = element.scrollTop();
		
		if (scrollTop >= 0){
			element.scrollTop(element.scrollTop() + offset);
			window.setTimeout(slideLayer, speed, element, offset, speed);
		}else{
			$('#out').text('1');
			element.scrollTop(element.scrollTop() - offset);
			window.clearTimeout();
		}	
	}
	
	/**
		Function to getting artwork data
		param: int nid
		return boolean true | false
	*/
	function showPictureExhibition(aid, eid, uid){

		var _self = getObject('box-entry-container');
		
		$.ajax({
			url: baseUrl + '/exhibition/get/' + aid + '/' + eid + '/' + uid,
			cache: false,
			beforeSend: function(){ buildOverlay(_self); $('.box-entry-overlay').show(); },
			success: function(data) { _self.html(data); }
		});
		
	}
	
	/**
		Function build overlay layer
	*/
	function buildOverlay(element){
		overlay = '\
			<div class="box-entry-overlay"> \
				<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"> \
						<tr> \
							<td align="center"> \
								<h2 style="color: #ffffff;"> \
									Loading ... \
								</h2> \
							</td> \
						</tr> \
				</table> \
			</div>';
			
		element.append(overlay);	
		
	}

	/**
		Function to getting artwork data
		param: int nid
		return boolean true | false
	*/
	function showPictureOpenCall(aid, eid, uid){
		
		var _self = getObject('box-entry-container');
		
		$.ajax({
			url: baseUrl + '/open-call/get/' + aid + '/' + eid + '/' + uid,
			cache: false,
			beforeSend: function(){ buildOverlay(_self); $('.box-entry-overlay').show(); },
			success: function(data) { _self.html(data); }
		});
	}
	
	/**
		Function to getting artwork data
		param: int nid
		return boolean true | false
	*/
	function showPictureOpening(aid, eid, uid){
		
		var _self = getObject('box-entry-container');
		
		$.ajax({
			url: baseUrl + '/opening/get/' + aid + '/' + eid + '/' + uid,
			cache: false,
			beforeSend: function(){ buildOverlay(_self); $('.box-entry-overlay').show(); },
			success: function(data) {
            _self.html(data);
         }
		});
	}
	
	/**
		Function to getting artwork data
		param: int nid
		return boolean true | false
	*/
	function showPictureFestivalOpenCall(aid, eid, uid){
		
		var _self = getObject('box-entry-container');
		
		$.ajax({
			url: baseUrl + '/festival/get/' + eid + '/' + aid + '/' + uid,
			cache: false,
			beforeSend: function(){			   
			   buildOverlay(_self);
			   $('.box-entry-overlay').show();
			},
			success: function(data) {  _self.html(data); }
		});
	}
	
	/**
		Function to getting application artwork data
		param: int nid
		return boolean true | false
	*/
	function showApplicationOpenCall(aid, eid, uid){

		var _self = getObject('box-entry-container');
		
		$.ajax({
			url: baseUrl + '/open-call-manager/application-get/' + aid + '/' + eid + '/' + uid,
			cache: false,
			beforeSend: function(){ buildOverlay(_self); $('.box-entry-overlay').css('height', '230px'); $('.box-entry-overlay').show(); },
			success: function(data) { _self.html(data); }
		});
	}

	/**
		Case insensitive contains - see the Capital C
	*/
	jQuery.extend(  
		jQuery.expr[':'], {  
			Contains : "jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0"  
	});  	

	/**
		Ajax request to add user favourite
	*/
	function addFavourite(obj, url, nid, type){
		
		var _this = $(obj);
		
		activateProgress(_this);
		
		$.post(url, 
			{	nid: nid,
				type: type
			},
			function(data, textStatus){
				deactivateProgress();
				
				if (data['error'])
					jQuery.showDialogError(data['message'], data['width']);
				
				if (data['success'])
					jQuery.showDialogSuccess(data['message'], data['width']);	
			},
			'json'
			);
		
		
	}//addFavourite
	
		var scroll;
		
		function scroll_fwd(s) {
			
			if(s) scroll = true;
			var _self = getObject('scroller');
			_self.scrollLeft( _self.scrollLeft() + 4);
			if(scroll) setTimeout("scroll_fwd()",10)
		}
			
		function scroll_rew(s) {
			if(s) scroll = true;
			var _self = getObject('scroller');
			_self.scrollLeft( _self.scrollLeft() - 4);
			if(scroll) setTimeout("scroll_rew()",10)
		}
	
	
	/**
		Function to clear previous work data on upload form
		return boolean true | false
	*/
	function clearPreviousWorkData(){
      $("#field_title_value").val("Untitled");
      $("#field_material_value").val("");
      $("#field_size_value").val("");
      $("#field_price_value").val("");
	}
	
	/**
		Participate Now Custom Button
	*/
	function participateNowUrl(){
	   var i_value = 1;
	   if ($("#field_participate_now_button-1").attr("checked")) {
	      i_value = 0;
	   }
	   if (i_value == 1) {
	      $("#div_participate_now_url").css("display", "none");
	      $("#div_wooloo_application_management_system").css("display", "");
	   } else if (i_value == 0) {
	      $("#div_participate_now_url").css("display", "");
	      $("#div_wooloo_application_management_system").css("display", "none");
	   }
	}
	
	/**
		Application Required - Set Application Fee
	*/
	function applicationRequired(){
	   var i_value = 0;
	   if ($("#field_apply_required-1").attr("checked")) {
	      i_value = 1;
	   }
	   if (i_value == 1) {
	      $("#div_application_fee").css("display", "");
	   } else if (i_value == 0) {
	      $("#div_application_fee").css("display", "none");
	   }
	}
	
	/**
		Application Fee Options
	*/
	function applicationFee(){
	   var i_value = 0;
	   if ($("#field_application_fee_required-1").attr("checked")) {
	      i_value = 1;
	   }
	   //alert(i_value);
	   if (i_value == 1) {
	      $("#div_application_fee_options").css("display", "");
	   } else if (i_value == 0) {
	      $("#div_application_fee_options").css("display", "none");
	   }
	}
	
	/*
	   Announcemnt Preview Functions
	*/
	showPreview = function(s_preview_div) {
      if ($("#" + s_preview_div).length > 0) {
         // block ui
         $.blockUI({
             message: $('#' + s_preview_div),
             fadeIn: 700, 
             fadeOut: 700,
             showOverlay: true,
             centerY: true,
             centerX: true,
             overlayCSS: {
               backgroundColor: '#000000',
               opacity: 0.8,
            },
             css: {
                 top: '80px',
                 padding: '0px',
                 marginLeft: '-60px',
                 backgroundColor: '#ffffff', 
                 opacity: 1,
                 color: '#333333',
                 fontWeight: '700',
                 fontSize: '14px',
                 width: '666px',
                 border: 'solid 4px #ffffff',
             } 
         });
      }
   }
	
	/**
		Function to set nationality based on country
		param: none
		return nationality
	*/
	function setLocationInfo(){

		var _field_country_value = getObject('field_country_value');
		var i_country_value = _field_country_value.val();
		
		// set id value to i_country_id
		$("#field_country_id_value").val(i_country_value);
		
		var _td_nationality_value = getObject('td_nationality_value');
		var _td_city_value = getObject('td_city_value');
		
		$.ajax({
			url: baseUrl + '/settings/set-nationality/' + i_country_value,
			//dataType: 'json',
			cache: false,
			//beforeSend: function(){ buildOverlay(_self); $('.box-entry-overlay').show(); },
			//success: function(data) { _self.html(data); }
			success: function(data) {
			   //var s_nationality = data.nationality;
			   //alert(data);
			   _td_nationality_value.html(data);
			   $("#field_nationality_value").selectbox();
			   $("#field_nationality_id_value").val(i_country_value);
			   //$("select").selectbox();
			}
		});
		
	}
	
	function setNationalityId(){

		var _field_nationality_value = getObject('field_nationality_value');
		var i_nationality_value = _field_nationality_value.val();
		
		// set id value to i_country_id
		$("#field_nationality_id_value").val(i_nationality_value);
		
		var _td_nationality_value = getObject('td_nationality_value');
		var _td_city_value = getObject('td_city_value');
		
	}
	
	function drawCountrySelect(){

		var _field_country_id_value = getObject('field_country_id_value');
		var i_country_id_value = _field_country_id_value.val();
		//alert(i_country_id_value);
		var _td_country_value = getObject('td_country_value');
		
		$.ajax({
			url: baseUrl + '/settings/draw-country-select/' + i_country_id_value,
			//dataType: 'json',
			cache: false,
			//beforeSend: function(){ buildOverlay(_self); $('.box-entry-overlay').show(); },
			//success: function(data) { _self.html(data); }
			success: function(data) {
			   //var s_nationality = data.nationality;
			   //alert(data);
			   _td_country_value.html(data);
			   $("#field_country_value").selectbox();
			   //$("#field_nationality_id_value").val(i_country_value);
			   //$("select").selectbox();
			}
		});
		
	}
	
	function drawNationalitySelect(){

		var _field_nationality_id_value = getObject('field_nationality_id_value');
		var i_nationality_id_value = _field_nationality_id_value.val();
		//alert(i_country_id_value);
		var _td_nationality_value = getObject('td_nationality_value');
		
		$.ajax({
			url: baseUrl + '/settings/draw-nationality-select/' + i_nationality_id_value,
			//dataType: 'json',
			cache: false,
			//beforeSend: function(){ buildOverlay(_self); $('.box-entry-overlay').show(); },
			//success: function(data) { _self.html(data); }
			success: function(data) {
			   //var s_nationality = data.nationality;
			   //alert(data);
			   _td_nationality_value.html(data);
			   $("#field_nationality_value").selectbox();
			   //$("#field_nationality_id_value").val(i_country_value);
			   //$("select").selectbox();
			}
		});
		
	}
	
	sendTestMessage = function(i_uid, s_email) {
	   //$("#div_send_test_email").html("<div style=\"width: 585px; text-align: center; padding-right: 30px;\"><img src=\"/public/images/image_loading.gif\" style=\"padding-left: 80px; padding-top: 40px;\"></div>");
	   $("#div_send_test_email").css("display", "none");
	   $("#div_confirm_email").css("display", "none");
	   $("#div_send_test_email_confirm").css("display", "none");
	   $("#div_send_test_email_loading").css("display", "");
	   $.post("/subscriptions/send-test-message", { uid: i_uid },
         function(data) {
            //alert("Data Loaded: " + data);
            var s_confirm_email = "<div style=\"float: left; color: #333333; font-size: 14px;\">";
            s_confirm_email += "A test email has been sent from WOOLOO.ORG to <b>" + s_email + "</b>.";
            s_confirm_email += "</div>";
            s_confirm_email += "<div style=\"clear: both; height: 10px;\"></div>";
            s_confirm_email += "<div style=\"float: left; color: #333333; font-size: 14px;\">";
            s_confirm_email += "If you have received this email, please click the \"Subscribe Now\" button below.";
            s_confirm_email += "<div style=\"clear: both; height: 10px;\"></div>";
            s_confirm_email += "<div style=\"float: left; color: #333333; font-size: 14px;\">";
            s_confirm_email += "If you did <b>not</b> receive this message, please check your Spam/Junk folder. ";
            s_confirm_email += "If the email has been sent to your Spam/Junk folder, please adjust your settings to allow emails from the WOOLOO.ORG domain into your normal inbox. ";
            s_confirm_email += "After you have changed your settings, please click the \"Send Test Message\" button to try again. ";
            s_confirm_email += "You should not press the \"Subscribe Now\" button until you have succesfully received a test message from WOOLOO.ORG in your inbox. ";
            s_confirm_email += "</div>";
            $("#div_confirm_email").html(s_confirm_email);
            $("#div_confirm_email").css("display", "");
            $("#div_send_test_email_loading").css("display", "none");
            $("#div_send_test_email_confirm").css("display", "");
      });
   }

	
	setMenu();
	setMouseOverBorder();
	customizeSelectBox();
	showErrorMessages();
	//startUlImage();

