//wquery 1.7.1

$(function()
{
	var ok = true;
	
	jQuery.each(jQuery.browser, function(i, val)
	{
		if ( i == 'version' )
		{
			v = val;
		}
		
		if ((i == 'msie' && val == true && v < 6) || (i == 'safari' && val == true && v < 2))
		{
			ok = false;
		}
	});
	
	if (ok)
	{
		$('.js').each(function()
		{
			var a = $(this).get_events( $(this).attr('class') );
			
			for (var i = 0; i < a.length; i++)
			{
				if ( a[i] == 'load' )
				{
					$(this).js_loader( a[i] );
				}
				else
				{
					$(this).bind( a[i], { q:a[i] }, $(this).js_all )
				}
			}
		});
	}
	
	$(this).preload_images();
});

current_menu = false;

jQuery.fn.extend(
{
	get_events: function(n)
	{
		var z = new Array();
		
		var a = 'js{';
		
		var b = n.split(a);
		
		var c = b.length;
		
		for (var i = 0; i < c; i++)
		{
			var d = b[i].indexOf('.');
			
			if (d == -1)
			{
				
			}
			else
			{
				var e = b[i].substr(0, d);
				
				var f = e.split(',');
				
				for (var j = 0; j < f.length; j++)
				{
					z.push(f[j]);
				}
			}
		}
		
		return z;
	},
	
	get_content: function(a)
	{
		if ( a.val() != undefined )
		{
			return a.val();
		}
		else if ( a.text() != undefined )
		{
			return a.text();
		}
		else if ( a.html() != undefined )
		{
			return a.html();
		}
	},
	
	update_content: function(a, b)
	{
		if ( a.val() != undefined )
		{
			a.val(b);
		}
		else if ( a.text() != undefined )
		{
			a.text(b);
		}
		else if ( a.html() != undefined )
		{
			a.html(b);
		}
	},
	
	append_content: function(a, b)
	{
		if ( a.val() != undefined )
		{
			a.val( a.val() + b );
		}
		else if ( a.text() != undefined )
		{
			a.text( a.text() + b );
		}
		else if ( a.html() != undefined )
		{
			a.html( a.html() + b );
		}
	},
	
	remove_content: function(a, b)
	{
		if ( a.val() != undefined )
		{
			a.val( a.val().substr(0, a.val().length - b) );
		}
		else if ( a.html() != undefined )
		{
			a.html( a.html().substr(0, a.html().length - b) );
		}
		else if ( a.text() != undefined )
		{
			a.text( a.text().substr(0, a.text().length - b) );
		}
	},
	
	get_all_func: function(n)
	{
		var a = $(this).attr('class').indexOf(n);
		
		if (a != - 1)
		{
			var b = $(this).attr('class').substr(a, $(this).attr('class').length - a);
			
			var c = b.indexOf('.');
			
			if (b != - 1)
			{
				var d = b.substr(c, b.length - c);
				
				var e = d.indexOf('}');
				
				if (e != -1)
				{
					var f = d.substr(0, e);
					
					var g = f.substr(1, f.length - 1);
					
					var h = g.split('*');
					
					return h;
				}
			}
		}
	},
	
	get_vars_val: function(n)
	{
		if (n != undefined)
		{
			var y = 'js_rule(';
			
			return $(this).get_all_vars(n, y);
		}
	},
	
	get_vars: function(n)
	{
		if (n != undefined)
		{
			var y = '(';
			
			return $(this).get_all_vars(n, y);
		}
	},
	
	get_all_vars: function(n, y)
	{
		var a = n.indexOf(y);
		
		if (a != -1)
		{
			var b = n.substr(a + y.length, n.length - a);
			
			var c = b.indexOf(')');
			
			if (c != -1)
			{
				var d = b.substr(0, c);
				
				var e = d.split(',');
			
				var z = new Array();
				
				for (var i = 0; i < e.length; i++)
				{
					var f = e[i].split(':');
					
					if (f.length == 2)
					{
						var x = f[0];
						
						var y = f[1];
						
						z[x] = y;
					}
				}
				
				return z;
			}
		}
	},
	
	get_width_and_height: function()
	{
		var a = new Array();
		
		if (self.innerWidth)
		{
			a[0] = self.innerWidth;
			a[1] = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			a[0]= document.documentElement.clientWidth;
			a[1] = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			a[0] = document.body.clientWidth;
			a[1] = document.body.clientHeight;
		}
		
		return a;
	},
	// loader functions bind events and onload events //
	
	js_all: function(event)
	{
		var z = true;
		
		var b = $(this).get_all_func( event.data.q );
		
		for ( var i = 0; i < b.length; i++)
		{
			if ( b[i].length > 0 && z != false )
			{
				var d = b[i].indexOf('(');
				
				var e = b[i].substr(0, d);
				
				var vars = $(this).get_vars( b[i] );
				
				if (e == 'js_char_lim') { z = $(this).js_char_lim(vars); }
				if (e == 'js_show') { z = $(this).js_show(vars); }
				if (e == 'js_hide') { z = $(this).js_hide(vars); }
				if (e == 'js_toggle') { z = $(this).js_toggle(vars); }
				if (e == 'js_copy_text') { z = $(this).js_copy_text(vars); }
				if (e == 'js_append_text') { z = $(this).js_append_text(vars); }
				if (e == 'js_validate') { z = $(this).js_validate(vars); }
				if (e == 'js_confirm') { z = $(this).js_confirm(vars); }
				if (e == 'js_ajax') { z = $(this).js_ajax(vars); }
				if (e == 'js_menu') { z = $(this).js_menu(vars); }
				if (e == 'js_maths') { z = $(this).js_maths(vars); }
				if (e == 'js_popup') { z = $(this).js_popup(vars); }
				if (e == 'js_false') { z = false }
			}
		}
		return z;
	},
	
	js_loader: function(x)
	{
		var z = true;
		
		var b = $(this).get_all_func(x);
		
		for ( var i = 0; i < b.length; i++)
		{
			if ( b[i].length > 0 && z != false )
			{
				var d = b[i].indexOf('(');
				
				var e = b[i].substr(0, d);
				
				var vars = $(this).get_vars( b[i] );
				
				if (e == 'js_hide') { z = $(this).js_hide(vars); }
				if (e == 'js_menu') { z = $(this).js_menu_init(vars); }
				if (e == 'js_flash') { z = $(this).js_flash(vars); }
			}
		}
	},
	
	
	js_menu_init: function(vars, b)
	{
		var b = (b) ? b : $(this).attr('class').substr(4, $(this).attr('class').length);
		
		vars['speed_off'] = ( vars['speed_off'] != undefined ) ? vars['speed_off'] : 500;
		
		vars['speed_over'] = ( vars['speed_over'] != undefined ) ? vars['speed_over'] : 10;
		
		vars['speed_out'] = ( vars['speed_out'] != undefined ) ? vars['speed_out'] : 200;
		
		vars['effect'] = ( vars['effect'] != undefined ) ? vars['effect'] : 'fade';
		
		vars['target'] = ( vars['target'] != undefined ) ? vars['target'] : 'a';
		
		$(this).children().each(function()
		{
			if ( $(this).attr('id') != undefined && $(this).attr('id').search('[0-9]+') != -1 )
			{
				if ( ( $(this).is('a') && vars['target'].search('a') != -1 ) || ( $(this).is('li') && vars['target'].search('li') != -1 ) || ( $(this).is('ul') && vars['target'].search('ul') != -1 ) )
				{
					$(this).addClass('js js{mouseout.js_menu(type:out,effect:' + vars['effect'] + ',speed_off:' + vars['speed_off'] + ',speed:' + vars['speed_out'] + ')} js{mouseover.js_menu(type:over,effect:' + vars['effect'] + ',speed:' + vars['speed_over'] + ')}');
					
					var a = 'mouseover';
					
					$(this).bind( a, { q:a }, $(this).js_all );
					
					var a = 'mouseout';
					
					$(this).bind( a, { q:a }, $(this).js_all );
				}
			}
		});
		
		$(this).children().each(function()
		{
			$(this).js_menu_init(vars, b);
		});
	},
	
	/* load functions */
	preload_images: function()
	{
		var wd_preload_images = new Array();
		
		var i = 0;
		
		$('a').each(function()
		{
			var a = false;

			if ( $(this).css('filter') != undefined && $(this).css('filter').search('_off') != -1 )
			{
				var b = $(this).css('filter').indexOf('src=');
				
				if (b != -1)
				{
					a = $(this).css('filter').substr(b + 5, $(this).css('filter').length - 5 - 2 - b);
				}
			}

			if ( $(this).css('background-image') != undefined && $(this).css('background-image') != 'none' && $(this).css('background-image').search('_off') != -1 )
			{
				a = $(this).css('background-image').substr(4, $(this).css('background-image').length - 5);
				
				a = a.replace(/'/, '');
				
				a = a.replace(/"/, '');
				
				a = a.replace(/"/, '');
			}
			
			if (a)
			{
				a = a.replace(/_off/, '_on');
				
				wd_preload_images[i] = new Image();
				
				wd_preload_images[i].src = a;
				
				i++;
			}
		});
	},
	
	/* normal functions */
	js_char_lim: function(vars)
	{
		$('.' + vars['target']).each(function()
		{
			var a = $(this).get_content( $(this) );
			
			if (a.length >= vars['limit'])
			{
				$(this).update_content( $('.' + vars['target']), a.substring(0, vars['limit']) );
			}
		});
	},
	
	js_show: function(vars)
	{
		var a = ( vars['speed'] ) ? vars['speed'] : '';
		
		$('.' + vars['target']).each(function()
		{
			if ( vars['opacity'] )
			{
				$(this).fadeTo( a, vars['opacity'] );
			}
			else if ( vars['effect'] == 'fade' )
			{
				$(this).fadeIn(a);
			}
			else
			{
				$(this).show(a);
			}
		});
	},
	
	js_hide: function(vars)
	{
		var a = ( vars['speed'] ) ? vars['speed'] : '';
		
		$('.' + vars['target']).each(function()
		{
			if ( vars['effect'] == 'fade' )
			{
				$(this).fadeOut(a);
			}
			else
			{
				$(this).hide(a);
			}
		});
	},
	
	js_popup: function(vars)
	{
		var z = $(this).get_width_and_height();
		
		var a = ( vars['toolbar'] ) ? vars['toolbar'] : 0;
		
		var b = ( vars['scrollbars'] ) ? vars['scrollbars'] : 1;
		
		var c = ( vars['location'] ) ? vars['location'] : 1;
		
		var d = ( vars['statusbar'] ) ? vars['statusbar'] : 1;
		
		var e = ( vars['menubar'] ) ? vars['menubar'] : 1;
		
		var f = ( vars['resizable'] ) ? vars['resizable'] : 1;
		
		var x = parseInt(z[0] * .8);
		
		var g = ( vars['height'] ) ? vars['height'] : x;
		
		x = parseInt(z[1] * .8);
		
		var h = ( vars['width'] ) ? vars['width'] : x;
		
		x = parseInt(z[0] * .1);
		
		var i = ( vars['left'] ) ? vars['left'] : x;
		
		x = parseInt(z[1] * .15);
		
		var j = ( vars['top'] ) ? vars['top'] : x;
		
		var k = ( vars['name'] ) ? vars['name'] : 'window';
		
		window.open($(this).attr('href'),k,'"toolbar=' + a + ',scrollbars=' + b + ',location=' + c + ',statusbar=' + d + ',menubar=' + e + ',resizable=' + f + ',width=' + g + ',height=' + h + ',left=' + i + ',top=' + j + '"');
		
		return false;
	},
	
	js_toggle: function(vars)
	{
		var a = ( vars['speed'] ) ? vars['speed'] : '';
		
		$('.' + vars['target']).each(function()
		{
			$(this).toggle(a);
		});
	},
	
	js_copy_text: function(vars)
	{
		if ( vars['text'] != undefined )
		{
			var content = vars['text'];
		}
		else
		{
			var content = $(this).get_content( $(this) );
		}
		
		$('.' + vars['target']).each(function()
		{
			$(this).update_content( $('.' + vars['target']), content );
		});
	},
	
	js_append_text: function(vars)
	{
		if ( vars['remove'] != undefined )
		{
			$('.' + vars['target']).each(function()
			{
				$(this).remove_content( $('.' + vars['target']), vars['remove'] );
			});
		}
		else
		{
			if ( vars['text'] != undefined )
			{
				var content = vars['text'];
			}
			else
			{
				var content = $(this).get_content( $(this) );
			}
			
			$('.' + vars['target']).each(function()
			{
				$(this).append_content( $('.' + vars['target']), content )
			});
		}
	},
	
	js_confirm: function(vars)
	{
		if ( vars['text'] )
		{
			var a = vars['text'];
		}
		else
		{
			var a = 'Are you sure you want to perform this action'
		}
		
		if ( confirm(a) )
		{
			return true;
		}
		else
		{
			return false;
		}
	},
	
	js_maths: function(vars)
	{
		var money = false;
		
		if ( vars['type'] == 'sum' )
		{
			var a = 0;
			
			$('.' + vars['this']).each(function()
			{
				var b = $(this).get_content( $(this) );
				
				if ( b.charAt(0) == '$' )
				{
					money = true;
				}
				
				b = b.replace('$', '');
				
				b = parseFloat(b);
								
				if ( !isNaN(b) )
				{
					a = a + b;
				}
			});
			
			if (money)
			{
				a = a.toFixed(2);
				
				a = '$' + a;
			}
			
			$(this).update_content( $('.' + vars['target']), a );
		}
		else if ( vars['type'] == 'mul' )
		{
			var a = 1;
			
			$('.' + vars['this']).each(function()
			{
				var b = $(this).get_content( $(this) );
				
				if ( b.charAt(0) == '$' )
				{
					money = true;
				}
				
				b = b.replace('$', '');
				
				b = parseFloat(b);
				
				if ( isNaN(b) )
				{
					b = 0;	
				}
				
				a = a * b;
			});
			
			if (money)
			{
				a = a.toFixed(2);
				
				a = '$' + a;
			}
			
			$(this).update_content( $('.' + vars['target']), a );
		}
	},
	
	js_flash: function(vars)
	{
		//vars['title']
		//vars['file']
		swfobject.embedSWF(vars['location'] + '?thetitle=' + vars['title'] + '&file=' + vars['file'],$(this).attr('id'),vars['width'],vars['height'],'8.0.0');
		//swfobject.embedSWF(vars['location'] + '?file=Toxic.flv',$(this).attr('id'),vars['width'],vars['height'],'8.0.0');
	},
	
	js_ajax: function(vars)
	{
		if ( vars['url'] )
		{
			var a = vars['url'];
		}
		else
		{
			var a = $(this).attr('href');
		}
		
		if ( a.indexOf('?') != -1 )
		{
			a = a + '&ajax=1';
		}
		else
		{
			a = a + '?ajax=1';
		}
		
		$.ajax(
		{
			type: "GET",
			url: a,
			async: false,
			
			success: function(result)
			{
				if (result == 'false' || result == '0')
				{
					result = false;
				}
				
				b = result;
			},
			failure: function(result)
			{
				location.href = '/error.htm';
			}
		});
		
		if ( vars['target'] )
		{
			$(this).update_content( $('.' + vars['target']), b );
		}
		
		return b;
	},
	
	js_validate: function(vars)
	{
		pass = true;
		
		if ( vars['target'] )
		{
			$('.' + vars['target']).removeClass('error_on');
		}
		
		$(this).parents('form').children().each(function()
		{
			$(this).wd_validate();
		});
		
		if (pass)
		{
			if ( vars['text'] )
			{
				$(this).update_content( $(this), vars['text'] );
			}
			
			if ( vars['disable'] == 'yes' )
			{
				$(this).parents('form').children().each(function()
				{
					$(this).wd_disable();
					
				});
				
				$(this).attr('disabled', 'disabled');
				
				$(this).parents('form').submit();
			}
			
			return true;
		}
		else
		{
			if ( vars['target'] )
			{
				$('.' + vars['target']).addClass('error_on');
			}
			
			return false;
		}
	},
	
	wd_disable: function()
	{
		$(this).attr('readonly', 'readonly');
		
		$(this).children().each(function()
		{
			$(this).wd_disable();
		});
	},
	
	wd_validate: function()
	{
		var vars_sub = $(this).get_vars_val( $(this).attr('class') );
		
		if ( vars_sub != undefined )
		{
			var a = $(this).get_content( $(this) );
			
			var d = $.trim(a);
			
			var fail = 0;
			
			if ( vars_sub['required'] == 'yes' && d.length == 0)
			{
				fail = 1;
			}
			else
			{
				if ( vars_sub['limit'] && a.length > 0 )
				{
					if ( a.length > vars_sub['limit'] )
					{
						fail = 1;
					}
				}
				
				if ( vars_sub['checked'] )
				{
					var check_name = $(this).attr('name');
					
					var passed_check = false;
					
					$('input:checked').each(function()
					{
						if ( check_name == $(this).attr('name') )
						{
							passed_check = true;
						}
					});
					
					if (!passed_check)
					{
						fail = 1;
					}
				}
				
				if ( vars_sub['type'] == 'email' && a.length > 0 )
				{
					if ( a.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1 )
					{
						fail = 1;
					}
				}
				
				if ( vars_sub['type'] == 'phone' && a.length > 0 )
				{
					if ( a.search(/^[\(\)\+\-\_\ 0-9]*$/) == -1 )
					{
						fail = 1;
					}
				}
				
				if ( vars_sub['type'] == 'number' && a.length > 0 )
				{
					if ( isNaN(a) )
					{
						fail = 1;
					}
				}
				
				if ( vars_sub['file_type'] )
				{
					if (a.length != 0)
					{
						var b = a.split('.');
						
						var c = b.length;
						
						var d = b[c - 1].toLowerCase();
						
						var q = vars_sub['file_type'].split('|');
						
						var r = false;
						
						for ( var i = 0; i < q.length; i++ )
						{
							if (q[i].toLowerCase() == d)
							{
								r = true;
							}
						}
						
						if (r == false)
						{
							fail = 1;
						}
					}
				}
			}
			
			if (fail == 1)
			{
				$('.' + vars_sub['target']).addClass('error_on');
				
				pass = false;
			}
			else
			{
				$('.' + vars_sub['target']).removeClass('error_on');
			}
		}
		
		$(this).children().each(function()
		{
			$(this).wd_validate();
		});
	},
	
	/* menu */
	js_menu: function(vars)
	{
		var a = $(this);
		
		if ( vars['type'] == 'over' )
		{
			var y = $(this).attr('id').split('_');
			
			$(this).js_menu_over_initial(a, y);
			
			setTimeout(function()
			{
				$(this).js_menu_over(a, y, vars);				
			},vars['speed']);
		}
		else if ( vars['type'] == 'out' )
		{
			var y = $(this).attr('id').split('_');
			
			$(this).js_menu_out_initial(a, y);
			
			setTimeout(function()
			{
				if ( current_menu == '' )
				{
					setTimeout(function()
					{
						$(this).js_menu_out(a, y, vars);
					},vars['speed_off']);
				}
				else
				{
					$(this).js_menu_out(a, y, vars);
				}
			},vars['speed']);
		}
	},
		
	js_menu_over_initial: function(a, y)
	{
		current_menu = $(this).attr('id');
		
		if ( y.length == 3 )
		{
		
		}
		else if ( y.length == 4 )
		{
			//$(this).parents('li').parents('li').find('>a').addClass('menu_over');
		}
		else if ( y.length == 5 )
		{
			//$(this).parents('li').parents('li').find('>a').addClass('menu_over');
		}
		
		if ( $(this).is('a') )
		{
			//$(this).addClass('menu_over');
		}
	},
	
	js_menu_out_initial: function(a, y)
	{
		current_menu = '';
		
		var x = current_menu.split('_');
		
		if ( y.length == 3 )
		{
			if ( x[1] != y[1] )
			{
				//$(this).parents('ul').find('>li').find('>a').removeClass("menu_over");
			}
		}
		else if ( y.length == 4 )
		{
			if ( x[2] != y[2] )
			{
				//$(this).parents('ul').find('>li').find('>a').removeClass("menu_over");
			}
		}
		else if ( y.length == 5 )
		{
			if ( x[3] != y[3] )
			{
				//$(this).parents('ul').find('>li').find('>a').removeClass("menu_over");
			}
		}	
	},
	
	js_menu_over: function(a, y, vars)
	{
		var b = a.attr('id');
		
		if ( current_menu == b )
		{
			if ( a.is('a') )
			{
				if ( vars['effect'] == 'fade' )
				{
					a.parent('li').find('>ul').fadeIn();
				}
				else if ( vars['effect'] == 'slide' )
				{
					a.parent('li').find('>ul').slideDown();
				}
			}
			else if ( a.is('li') || a.is('ul') )
			{
				if ( vars['effect'] == 'fade' )
				{
					a.find('>ul').fadeIn();
				}
				else if ( vars['effect'] == 'slide' )
				{
					a.find('>ul').slideDown();
				}
			}
		}
	},
		
	js_menu_out: function(a, y, vars)
	{
		var x = current_menu.split('_');
		
		if ( y[1] != x[1] )
		{
			if ( y.length == 3 )
			{
				if ( vars['effect'] == 'fade' )
				{
					a.parent('li').find('>ul').fadeOut();
				}
				else if ( vars['effect'] == 'slide' )
				{
					a.parent('li').find('>ul').slideUp();
				}
			}
			else if ( y.length == 4 )
			{
				if ( vars['effect'] == 'fade' )
				{
					a.parent('li').find('>ul').fadeOut();
					
					a.parents('li').parents('li').find('>ul').fadeOut();
				}
				else if ( vars['effect'] == 'slide' )
				{
					a.parent('li').find('>ul').slideUp();
					
					a.parents('li').parents('li').find('>ul').slideUp();
				}
			}
			else if ( y.length == 5 )
			{
				if ( vars['effect'] == 'fade' )
				{
					a.parents('li').parents('li').find('>ul').fadeOut();
				}
				else if ( vars['effect'] == 'slide' )
				{
					a.parents('li').parents('li').find('>ul').slideUp();
				}
			}
		}
		else if ( y[2] != x[2] )
		{
			if (  y.length == 4 )
			{
				if ( vars['effect'] == 'fade' )
				{
					a.parent('li').find('>ul').fadeOut();
				}
				else if ( vars['effect'] == 'slide' )
				{
					a.parent('li').find('>ul').slideUp();
				}
			}
			else if ( y.length == 5 )
			{
				if ( vars['effect'] == 'fade' )
				{
					a.parent('li').parent('ul').fadeOut();
				}
				else if ( vars['effect'] == 'slide' )
				{
					a.parent('li').parent('ul').slideUp();
				}
			}
		}
		else if ( y[3] != x[3] )
		{
			if ( y.length == 5 )
			{
				if ( vars['effect'] == 'fade' )
				{
					a.parent('li').find('>ul').fadeOut();
				}
				else if ( vars['effect'] == 'slide' )
				{
					a.parent('li').find('>ul').slideUp();
				}
			}
		}	
	}
});