$(document).ready(function(){
	//initAutocomplete();
	login();
	$('body').myPopup();
	initFormValidation()
	initAdvancedSearch();
	initBookmark();
	initInputTolltip();
	initSelectRow();
	initOpenClose();
	initLogo();
	initStars();
	if ($('a.light-box').length > 0) $('a.light-box').lightBox();
	initMore();
	$('body').lightPlayer();
	initPayPal();
	$('body').rate();
});
function initPayPal(){
	$('div.initpaypal').each(function(){		var hold = $(this);		var select = hold.find('select.sel-paypal');
		var line = hold.find('div.line-holder');
		var wrap = hold.find('div.paypal-wrap');
		var fer = line.find('.required');
		var ferNum = line.find('.required-num');		
		wrap.hide();
		select.change(function(){
			if ($(this).val() == 'PayPal'){
				fer.removeClass('required');
				ferNum.removeClass('required-num');
				line.hide();
				wrap.show();
			}
			else{
				fer.removeClass('required').addClass('required');
				ferNum.removeClass('required-num').addClass('required-num');
				line.show();
				wrap.hide();
			}
		});	});
}
function initMore(){
	$('div.hold-opener').each(function(){
		var hold = $(this);
		var link = hold.find('a.btn-opener-block');
		var first = hold.find('div.first-block');
		var block = hold.find('div.opener-block');
		var hf = first.outerHeight(true);
		var hb = block.outerHeight(true);
		
		block.css({height:hf+3});
		
		link.click(function(){
			if (!$(this).hasClass('opened')){
				block.animate({height:hb+3}, 500);
				$(this).addClass('opened');
			}
			else{
				block.animate({height:hf+3}, 500);
				$(this).removeClass('opened');
			}
			return false;
		});
	});
}

function initStars(){
	$('ul.star-rating').each(function(){
		var hold = $(this);
		var stars = hold.find('> li');
		var active = stars.index(stars.filter('.active:eq(0)'));
		
		hold.hover(function(){
			stars.removeClass('active');
		}, function(){
			stars.eq(active).addClass('active');
		});
		stars.click(function(){
			stars.removeClass('active');
			active = stars.index($(this));
			return false;
		});
	});
}
function initLogo(){
	$('form.file-logo').each(function(){
		var hold = $(this);
		var file = hold.find('input.file-input-area');
		var inputPos = hold.find('input.position');
		var logo = hold.find('strong.b-logo');
		var error = hold.find('div.hint-line > ul > li.formats');
		var button = hold.find('a.button');
		var btnCrop = hold.find('a.btn-crop');
		var hl = logo.outerHeight();
		var wl = logo.outerWidth();
		var leftImg = 0;
		var topImg = 0;
		var fer = false;
		
		file.change(function(){
			error.removeClass('error');
			if(($(this).val().lastIndexOf('.jpg') != -1)) loadIMG()
			else {
				if(($(this).val().lastIndexOf('.bmp') != -1)) loadIMG()
				else{
					if(($(this).val().lastIndexOf('.tiff') != -1)) loadIMG()
					else error.addClass('error');
				}
			}
			function loadIMG(){
				$.ajax({
					type: "GET",
					data: file.val(),
					url: button.attr('title'),
					success: function(msg){
						fer = true;
						logo.html(msg);
						var hol = logo.find('img');
						var h = hol.outerHeight();
						var w = hol.outerWidth();
						hol.addClass('draggable').addClass('ui-widget-content');
						hol.draggable({ 
							scroll: false,
							stop: function(event, ui) {
								if ((w + ui.position.left - wl) < 0) {
									hol.animate({left: -w + wl}, {queue:false, duration: 200});
									ui.position.left = -w + wl;
								}
								else{
									if(ui.position.left > 0){
										hol.animate({left: 0}, {queue:false, duration: 200});
										ui.position.left = 0;
									}
								}
								if ((h + ui.position.top - hl) < 0) {
									hol.animate({top: -h + hl}, {queue:false, duration: 200});
									ui.position.top = -h + hl;
								}
								else{
									if(ui.position.top > 0){
										hol.animate({top: 0}, {queue:false, duration: 200});
										ui.position.top = 0;
									}
								}
								leftImg = ui.position.left;
								topImg = ui.position.top;
							}
						});
					},
					error:function(){
						alert('Ajax error1!');
					}
				});
			}
		});
		btnCrop.click(function(){
			if (fer) {
				inputPos.attr('value', 'left=' + leftImg + ' top=' + topImg);
			}	
			return false;
		});
	});
}
function initOpenClose(){
	$('#accordion').each(function(){		var hold = $(this);		var links = hold.find('div.row > a.p-name');
		var box = hold.find('> li > ul');
		var h = [];
		
		box.each(function(_i){
			h[_i] = $(this).outerHeight(true);
			if (!links.eq(_i).hasClass('opened')) $(this).css({height: 0});
		});		
		links.click(function(){
			var active = links.index($(this));
			if ($(this).hasClass('opened')){
				box.eq(active).animate({height: 0}, 300);
				$(this).removeClass('opened');
			}
			else{
				box.eq(active).animate({height: h[active]}, 300);
				$(this).addClass('opened');
			}
			return false;
		});	});
}
function initSelectRow(){
	$('#accordion').each(function(){
		var hold = $(this);
		var span = hold.find('div.row > span, ul > li > span');
		var btn = hold.find('div.action-row > span > a');
		var btnNo = $('form.hold-form .btn-no');
		var box = $('div.your-chose > div.var');
		var count = 0;
		var confirmBox = $('div.confirm-box');
		var h = confirmBox.outerHeight(true);
		
		box.css({opacity:0});
		confirmBox.css({marginTop:-h});
		span.each(function(){
			count++;
			$(this).attr('rel', count);
			if (count == 3) count = 0;
		});
		span.hover(function(){
			var ter = $(this).attr('rel');
			span.each(function(){
				if ($(this).attr('rel') == ter) $(this).addClass('color');
			});
		}, function(){
			span.removeClass('color');
		});
		btn.click(function(){
		//alert($(this));
			span.removeClass('color-active');
			var _active = btn.index($(this))+1;
			span.each(function(){
				if ($(this).attr('rel') == _active) $(this).addClass('color-active');
			});
			box.animate({
				opacity:0
				}, {queue:false, duration: 500}).removeClass('active').eq(_active).animate({
					opacity:1
					}, {queue:false, duration: 500, complete: function(){
						$(this).css('opacity','auto');
					}}).addClass('active');
			confirmBox.animate({marginTop:0}, 500);
			return false;
		});
		
		/* by me */
		
		btn.ready(function(){
		//alert(add_plan);
		if(add_plan>-1 && add_plan!=3)
		{
		//alert(add_plan);
		document.getElementById("package_id").value=parseInt(add_plan)+1;
		//alert(document.getElementById("package_id").value);
		csd=document.getElementById('j'+add_plan);
			span.removeClass('color-active');
			var _active = btn.index(csd)+1;
			span.each(function(){
				if ($(this).attr('rel') == _active) $(this).addClass('color-active');
			});
			box.animate({
				opacity:0
				}, {queue:false, duration: 500}).removeClass('active').eq(_active).animate({
					opacity:1
					}, {queue:false, duration: 500, complete: function(){
						$(this).css('opacity','auto');
					}}).addClass('active');
			confirmBox.animate({marginTop:0}, 500);
			return false;
			}
			if(add_plan==3)
			{
			var _active = 0;
			span.removeClass('color-active');
			box.animate({
				opacity:0
				}, {queue:false, duration: 500}).removeClass('active').eq(_active).animate({
					opacity:1
					}, {queue:false, duration: 500, complete: function(){
						$(this).css('opacity','auto');
					}}).addClass('active');
			confirmBox.animate({marginTop:0}, 500);
			return false;
			
			}
		});
		
		/* by me */
		
		btnNo.click(function(){
			var _active = 0;
			span.removeClass('color-active');
			box.animate({
				opacity:0
				}, {queue:false, duration: 500}).removeClass('active').eq(_active).animate({
					opacity:1
					}, {queue:false, duration: 500, complete: function(){
						$(this).css('opacity','auto');
					}}).addClass('active');
			confirmBox.animate({marginTop:0}, 500);
			return false;
		});
	});
}
function initInputTolltip(){
	$('form.input-tolltip').each(function(){
		var hold = $(this);
		var inputs = hold.find('input:text');
		var _active;
		
		inputs.focus(function(){
			_active = inputs.index($(this));
			inputs.parents('div.row').removeClass('active-input');
			$(this).parents('div.row').addClass('active-input');
		});
		inputs.mouseover(function(){
			inputs.parents('div.row').removeClass('active-input');
			$(this).parents('div.row').addClass('active-input');
		});
		inputs.mouseout(function(){
			inputs.parents('div.row').removeClass('active-input');
			inputs.eq(_active).parents('div.row').addClass('active-input');
		});
	});
}
function initBookmark(){
	var link = $('li.btn-bookmark > a');
	
	function CreateBookmarkLink() {

	title = $('title').text();
	url = window.location;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
	}
	link.click(function(){
		if (CreateBookmarkLink()) alert('Bookmark this page!');
		return false;
	});
}
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
function initAdvancedSearch(){
	$('div.results').each(function(){
		var kok = $.cookie("filter-open");
		var hold = $(this);
		var open = $('a.btn-adv-search');
		var open2 = $('a.btn-adv-search2');
		var close = hold.find('div.headline li.link > a');
		var filter = hold.find('div.filter');
		var hm = filter.outerHeight(true);
		var h = filter.outerHeight();
		var _form = hold.find('form.filter-frm');
		var _chek = _form.find('input:checkbox');
		var _submit = _form.find('input:submit');
		var mar = h - hm;
		
		
		if ((kok == 'close') || (kok == null)) {
			filter.css({
				marginBottom: -(h)
			});
			$.cookie('filter-open', 'close');
		}
		if (kok == 'open') {
			open.hide();
			open2.addClass('opened');
		}
		_chek.each(function(){
		if($(this).attr('name')=="km")
		{
		//alert($(this).attr('id'));
		//if ($.cookie('checkbox-' + $(this).attr('id')) == 'checked' ) $(this).attr('checked', 'checked');
		}else {
			if ($.cookie('checkbox-' + $(this).attr('name')) == 'checked' ) $(this).attr('checked', 'checked');
			}
		});
		open.click(function(){
			open2.addClass('opened');
			filter.animate({marginBottom: -mar}, 300);
			open.hide();
			$.cookie('filter-open', 'open');
			return false;
		});
		open2.click(function(){
			if (!open2.hasClass('opened')){
				open2.addClass('opened');
				filter.animate({marginBottom: -mar}, {queue:false, duration: 300});
				open.hide();
				$.cookie('filter-open', 'open');
			}
			else{
				open2.removeClass('opened');
				filter.animate({marginBottom: -(h)}, {queue:false, duration: 300});
				open.show();
				$.cookie('filter-open', 'close');
			}
			return false;
		});
		close.click(function(){
			open2.removeClass('opened');
			filter.animate({marginBottom: -(h)}, 300);
			open.show();
			$.cookie('filter-open', 'close');
			return false;
		});
		_chek.change(function(){
		//alert($(this).attr('id'));
		if($(this).attr('name')=="km")
		{
		 for(mas=5;mas<=30;mas=mas+5)
		 {
		  if(document.getElementById('km-'+mas).checked==true)
		  {
		   if(document.getElementById($(this).attr('id')).value!=mas)
		   {
		   document.getElementById('km-'+mas).checked=false;
		   }
		  }
		 }
		 }
		$('#panel_1').block({ 
                message: '<h1>Processing...</h1>', 
                css: { border: 'none', 
                   padding: '15px', 
            backgroundColor: '#000', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: .5, 
            color: '#fff'  } 
            }); 
 
        //setTimeout($.unblockUI, 300);
		//$.blockUI();

		
			_submit.trigger('click');
			if ($.cookie('checkbox-' + $(this).attr('name')) == 'checked' ) $.cookie('checkbox-' + $(this).attr('name'), null);
			else $.cookie('checkbox-' + $(this).attr('name'), 'checked');
		});
	});
}
function initFormValidation() {
	var _errorClass = 'error';
	var _regEmail = /^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/;
	var _regPhone = /^[69]{2}[0-9]{8}$/;
	var _regNum = /^[0-9]+$/;
	var _regPass = /^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$/;
	var flag = true;
	
	$('form.validate-hold').each(function(){
		var _form = $(this);
		function checkFields() {
			
			var _flag = false;
			_form.find('.'+_errorClass).removeClass(_errorClass);

			// fields validation
			_form.find('input.required-email').each(function(){
				if(!_regEmail.test($(this).val())) addError($(this));
			});
			_form.find('input.required-email-confirm').each(function(){
				if((!_regEmail.test($(this).val())) || ($(this).val() != _form.find('input.required-email').val())) addError($(this));
			});
			_form.find('input.required-phone').each(function(){
				if(!_regPhone.test($(this).val())) addError($(this));
			});
			_form.find('input.required-num').each(function(){
				if(!_regNum.test($(this).val())) addError($(this));
			});
			_form.find('input.required, input.required-captcha, input.required-password, textarea.required').each(function(){
			   if(!$(this).val().length || $(this).val() == $(this).attr('alt')) addError($(this));
			});
			_form.find('input.required-password-confirm').each(function(){
				if((!$(this).val().length || $(this).val() == $(this).attr('alt')) || ($(this).val() != _form.find('input.required-password').val())) addError($(this));
			});
			_form.find('input.required-checkbox').each(function(){
				if(!$(this).attr('checked')) addError($(this));
				
				/*if(document.getElementById('me').checked==true)
				{
				if(document.getElementById('lat_long').value=="")
				{
				//alert('no');
				//addError($(document.getElementById('me')));
				addError($(this));
				}
				//document.form1.submit("true");
				}
				
				*/
			});
			_form.find('input.required-password,input.required-password-confirm').each(function(){
			    if($(this).val().length<6 || $(this).val().length>8) addError($(this));
				if(!_regPass.test($(this).val())) addError($(this));
			});
			 
			/*_form.find('input.required-captcha').each(function(){ 
						var _this = $(this);
						if(_this.val().toLowerCase()!=document.getElementById('sx0001').value.toLowerCase())
						{  addError($(this));
						   alert("Invalid captcha code.");
						
						}
						
						});*/
          /* if(document.getElementById('add_v').checked==true)
		   {
		   show_in_map();
		   }*/
			// error class adding
			function addError(_obj) {
				_obj.parents('div:first').addClass(_errorClass);
				_flag=true;
			}
			return _flag;
		}

		// catch form submit event
		_form.submit(function(){
			if(checkFields()) {
				return false;
			}
			_form.find('input.required-captcha').each(function(){
				var _this = $(this);
				$.ajax({
					type: "GET",
					url: $(this).attr('title'),
					data:'captcha='+_this.val(),
					success: function(msg){
						if (msg != 'true') {
							_this.parents('div.line').addClass(_errorClass);
							flag = false;
							return false;
						}
					},
					error:function(){
						//alert('Ajax error2!');
					}
				});
			});
			if (!flag) return false;
		});
	});
}
function login(){
	var step1 = $('div.drop div.step1');
	var step2 = $('div.drop div.step2');
	var step3 = $('div.drop div.step3');
	$('ul.sub-nav a.login').click(function(){
		if (!$(this).parent().hasClass('active')) {
			$(this).parent().addClass('active')
		}
		else {
			$(this).parent().removeClass('active');
			step1.show();
			step2.hide();
			step3.hide();
		}
		return false;
	});
	$('ul.sub-nav a.login').parent().hover(function(){
		$(this).addClass('hover-login');
	}, function(){
		$(this).removeClass('hover-login');
	});
	$('body').click(function(){
		if (!$('ul.sub-nav a.login').parent().hasClass('hover-login')) {
			$('ul.sub-nav a.login').parent().removeClass('active');
			step1.show();
			step2.hide();
			step3.hide();
		}
	})
	$('form.login-frm').each(function(){
		var hold = $(this);
		var _form = $('form.login-frm').eq(1);
		var forgot = hold.find('a.forgot');
		var backtologin = hold.find('a.backtologin');
		var send = hold.find('input.btn-send');
		var again = $('a.btn-login-again');
		var _regEmail = /^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/;
		var _errorClass = 'error';
		
		forgot.click(function(){
			step1.fadeOut(300, function(){
				step2.fadeIn(300);
			});
			return false;
		});
		backtologin.click(function(){
			step2.fadeOut(300, function(){
				step1.fadeIn(300);
			});
			return false;
		});
		function checkFields1() {
			var _flag = false;
			_form.find('.'+_errorClass).removeClass(_errorClass);

			// fields validation
			_form.find('input[name=email]').each(function(){
				if(!_regEmail.test($(this).val())) addError($(this));
			});

			// error class adding
			function addError(_obj) {
				_obj.parent().addClass(_errorClass);
				_flag=true;
			}
			return _flag;
		}
		send.click(function(){
			if(checkFields1()) {
				return false;
			}
			else{
			document.getElementById('limx').style.display="inline";
			document.getElementById('emsx').innerHTML="Please enter your email to receive your password:";
			email=encodeURIComponent(document.getElementById('email').value);
				$.ajax({
					type:'POST',
					url:$('form.login-frm').eq(1).attr('action')+"/"+email,
					data:$('form.login-frm').eq(1).serialize(),
					success:function(msg){
					//alert(msg);
					if(msg=="error")
					{
					document.getElementById('emsx').innerHTML="Invalid Information. Please Try Again";
					document.getElementById('limx').style.display="none";
					}
					else
					{
					document.getElementById('limx').style.display="none";
						step2.fadeOut(300, function(){
							step3.fadeIn(300);
						});
					}	
						//alert('ajax send');
					},
					error:function(){
					//	alert('ajax error3');
					}
				});
			}
			return false;
		});
		again.click(function(){
			step3.fadeOut(300, function(){
				step1.fadeIn(300);
			});
			return false;
		});
	});
}
function initAutocomplete(){
	var availableTags = [];
	var input = $("#search-field, form.search-form input:text, #suburb-f");
	var linkInc = input.attr('title');
	input.attr('title', '');
	
	$.ajax({
		type: "GET",
		global: false,
		dataType: "html",
		url: linkInc,
		success: function(msg){
			availableTags = msg.split(/:\s+/);
			input.autocomplete({
			    minLength:3,
				source: availableTags,
				open: function(event, ui) {
					$('ul.ui-menu').width(input.outerWidth(true)-6);
					if ($('ul.ui-menu').outerHeight(true) > 200){
						$('ul.ui-menu').height(200);
						$('ul.ui-menu > li').css({
							width: $('ul.ui-menu').outerWidth(true)-24
						});
					}
					input.keydown(function(event, ui) {
						setTimeout(function(){							$('ul.ui-menu').height('auto');
							if ($('ul.ui-menu').outerHeight(true) > 200) {
								$('ul.ui-menu').height(200);
								$('ul.ui-menu > li').css({
									width: $('ul.ui-menu').outerWidth(true)-24
								});
							}
							else{
								$('ul.ui-menu > li').css({
									width: '100%'
								});
							}						}, 300);
					});
				}
			});
		},
		error:function(){
			//alert('Ajax error4!');
		}
	});
}
function hideFormText() {
	var _inputs = document.getElementsByTagName('input');
	var _txt = document.getElementsByTagName('textarea');
	var _value = [];
	
	if (_inputs) {
		for(var i=0; i<_inputs.length; i++) {
			if (_inputs[i].type == 'text' || _inputs[i].type == 'password') {
				
				_inputs[i].index = i;
				_value[i] = _inputs[i].value;
				
				_inputs[i].onfocus = function(){
					if (this.value == _value[this.index])
						this.value = '';
					this.className = this.className.replace(" focus","")
					this.className += ' focus';
					this.className = this.className.replace(" addfor","")
					this.className += ' addfor';
				}
				_inputs[i].onblur = function(){
					if (this.value == '') {
						this.className = this.className.replace(" focus","")
						this.value = _value[this.index];
					}
					this.className = this.className.replace(" addfor","")
				}
			}
		}
	}
	if (_txt) {
		for(var i=0; i<_txt.length; i++) {
			_txt[i].index = i;
			_value['txt'+i] = _txt[i].value;
			
			_txt[i].onfocus = function(){
				if (this.value == _value['txt'+this.index])
					this.value = '';
				this.className = this.className.replace(" focus","")
				this.className += ' focus';
				this.className = this.className.replace(" addfor","")
				this.className += ' addfor';
			}
			_txt[i].onblur = function(){
				if (this.value == '') {
					this.className = this.className.replace(" focus","")
					this.value = _value['txt' + this.index];
				}
				this.className = this.className.replace(" addfor","")
			}
		}
	}
}
jQuery.fn.myPopup = function(_options){
	// defaults options	
	var buser_id=0;
	var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '.link-popup',
		linkCloseName: 'a.close',
		divFader: 'fader'
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _IE = ((navigator.appName.indexOf('Microsoft Internet Explorer') != -1) && (parseInt(navigator.appVersion) < 9)) ? true : false;
		var links = _hold.find(_options.linkOpenName);
		var _fader = $('<div class="'+_options.divFader+'"></div>');
		var popup;
		$('body').append(_fader);
		_fader.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 999,
			background: 'black',
			opacity: 0.7
		});
		
		
		function init(_obj){
			popup = $(_obj);
			var btnClose = popup.find(_options.linkCloseName);
			var submitBtn = popup.find('.link-submit');
			
			if (_IE){
				$('select').css({visibility: 'hidden'});
				popup.find('select').css({visibility: 'visible'});
				}
			var w = $('body').width();
			var _w = $('#wrapper').width();
			if (_w > w) w =_w;
			var h = $(window).height();
			var _offset = $('html').scrollTop();
			var ret = _offset+(h/2) - popup.outerHeight(true)/2;
			if (ret < 0) ret = 0;
			popup.css({
				top: ret,
				left: w/2 - popup.outerWidth(true)/2
			}).hide();
			_fader.css({
				width: w,
				height: $('#wrapper').height()
			}).fadeIn(300, function(){
				popup.fadeIn(300);
			});
			$(window).resize(function(){
				w = $('body').width();
				_w = $('#wrapper').width();
				if (_w > w) w =_w;
				popup.css({
					left: w/2 - popup.outerWidth(true)/2
				});
				_fader.css({
					width: w
				});
			});
		
			$(document).keydown(function(event) {
				if (event.keyCode == '27'){
					popup.css({left: '-9999px'});
					$('div.optionsDivVisible').removeClass('optionsDivVisible').addClass('optionsDivInvisible');
					_fader.hide();
					if (_IE) $('select').css({visibility: 'visible'});
					submitBtn.unbind('click');
					$('a.print-box').unbind('click');
					$(window).unbind('resize');
				}
			});
			btnClose.click(function(){document.getElementById('vidiv').style.display = 'block';
				popup.css({left: '-9999px'});
				$('div.optionsDivVisible').removeClass('optionsDivVisible').addClass('optionsDivInvisible');
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				submitBtn.unbind('click');
				$('a.print-box').unbind('click');
				$(window).unbind('resize');
				return false;
			});
			submitBtn.click(function(){
				$('div.optionsDivVisible').removeClass('optionsDivVisible').addClass('optionsDivInvisible');
				var thisBtn = $(this);
				var _thisUrl = $(this).attr('title');
				var _thisUrl_value = $(this).attr('id');
				var _errorClass = 'error';
				var _regEmail = /^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/;
				var _regPhone = /^[69]{2}[0-9]{8}$/;
				var _regNum = /^[0-9]+$/;
				var flag = true;
				popup.find('form.validate-form').each(function(){
					var _form = $(this);
					function checkFields() {
						//alert(document.getElementById('sx0001').value);
						var _flag = false;
						_form.find('.'+_errorClass).removeClass(_errorClass);
			
						// fields validation
						_form.find('input.required-email').each(function(){
							if(!_regEmail.test($(this).val())) addError($(this));
						});
						_form.find('input.required-email-confirm').each(function(){
							if((!_regEmail.test($(this).val())) || ($(this).val() != _form.find('input.required-email').val())) addError($(this));
						});
						/*_form.find('input.required-phone').each(function(){
							if(!_regPhone.test($(this).val())) addError($(this));
						});
						_form.find('input.required-num').each(function(){
							if(!_regNum.test($(this).val())) addError($(this));
						});*/
						_form.find('input.required, textarea.required').each(function(){
						var value = $(this).val();
						if(jQuery.trim(value) == "")
						{
							addError($(this));
						}
							if(!$(this).val().length || $(this).val() == $(this).attr('alt')) addError($(this));
						});
						
						_form.find('input.required-captcha').each(function(){
						var _this = $(this);
						if(_this.val().toLowerCase()!=document.getElementById('sx0001').value.toLowerCase())
						{
						_this.parents('div.line').addClass(_errorClass);
						flag = false;
						}
						else
						{
						flag = true;
						}
					
					});
					
						
						
						// error class adding
						function addError(_obj) {
							_obj.parents('div.line').addClass(_errorClass);
							_flag=true;
						}
						return _flag;
					}
					// catch form submit event
					if(checkFields()) {
						return false;
					}
					_form.find('input.required-captcha').each(function(){
						var _this = $(this);
						$.ajax({
							type: "GET",
							url: $(this).attr('title'),
							data:'captcha='+_this.val(),
							success: function(msg){
								if (msg != 'true') {
									_this.parents('div.line').addClass(_errorClass);
									flag = false;
								}
							},
							error:function(){
								//alert('Ajax error5!');
							}
						});
					});
					if (flag && !thisBtn.hasClass('no-ajax')) {
					
					if(_thisUrl_value=="load_next_fst")
					{
					document.getElementById('lox').innerHTML="<div align='center' id='lox' style='font-size:18px'><b>Please Wait...</b></div>";
					document.getElementById('load_next_fst').style.display="none";
					//document.getElementById('load_next_fst').value="Please Wait...";
					document.getElementById('load_next_fst').disabled=true;
					buser_id=document.getElementById('bxu').value;
					fullname=encodeURIComponent(document.getElementById('full-name').value);
					address=encodeURIComponent(document.getElementById('your-adr').value);
					eadr=encodeURIComponent(document.getElementById('e-adr').value);
					mph=encodeURIComponent(document.getElementById('m-num').value);
					//alert('sss');
					var direct="";
					if(document.getElementById('gdirection').value=="gdirection")
					{
					 direct=document.getElementById('gdirection').value;
					}
					$.ajax({
							type:'POST',
							url:situr+"minisites/send_sms/"+buser_id+"/"+mph+"/"+fullname+"/"+eadr+"/"+address+"/"+direct,
							data:_form.serialize(),
							success:function(msg){
							
								//document.getElementById('load_next').disabled=false;
								if(msg==0 || msg==1)
								{
								//document.getElementById('s_m_s').innerHTML="<b>An E-mail and a sms has been sent successfuly to your mail and phone.</b>";
								document.getElementById('s_m_s').innerHTML="<div class='section' align='center' id='contact_sms' style='border:0px solid #e4e4e4; padding:0px; background-color:#91FE98; font-size:22px; font-weight:bold; color:#000000'><p>Thanks. Your message has been sent.<br/>It should arrive shortly.</p></div>";
								document.getElementById('ops1').style.display="none";
								//document.getElementById('load_next_fst').value="SENT";
								document.getElementById('lox').innerHTML="<div align='center' id='lox' style='font-size:18px'><b>SENT SUCCESSFULL</b></div>";
								}
								else
								{
								//document.getElementById('s_m_s').innerHTML="<b>Sorry due to some problem email and sms sending has been failed.Please try again latter.</b>";
								document.getElementById('s_m_s').innerHTML="<div class='section' align='center' id='contact_sms' style='border:0px solid #e4e4e4; padding:0px; background-color:#FF0000; font-size:22px; font-weight:bold; color:#ffffff'><p>Sorry ! Message sending failed.<br/>Please try again.</p></div>";
								document.getElementById('ops1').style.display="none";
								//document.getElementById('load_next_fst').value="SENT";
								//document.getElementById('load_next').disabled=false;
								document.getElementById('lox').innerHTML="<div align='center' id='lox' style='font-size:18px'><b>NOT SENT</b></div>";
								}
								
				               return false;
								
							},
							error:function(){
								//alert('ajax error777');
							}
						});
					
					
					}
					
					if(_thisUrl_value=="load_next")
					{
					
					
					$.ajax({
							type:'POST',
							url:_form.attr('action'),
							data:_form.serialize(),
							success:function(msg){ document.getElementById('vidiv').style.display = 'none';
								document.getElementById('lixu').value="1";
								//alert('ajax send');
								popup.css({left: '-9999px'});
								if (_IE) $('select').css({visibility: 'visible'});
								submitBtn.unbind('click');
								$('a.print-box').unbind('click');
								$(window).unbind('resize');
								//alert(_thisUrl);
								init("#voucher-end");
								//document.getElementById('load_next').style.display="none";
								//showAddress_popup(document.getElementById('bxu1').value,document.getElementById('bxu2').value,'v_in_map');
							},
							error:function(){
								alert('ajax error6');
							}
						});
					
					
					
					
					//alert(document.getElementById('nd'));
					//document.getElementById('nd').innerHTML='<div class="voucher-popup" id="voucher-end"><div class="inner"><div class="headline">hi mithun...</div></div></div>';
					document.getElementById('loxx').innerHTML="<div align='center' id='loxx' style='font-size:18px'><b>Please Wait...</b></div>";
					document.getElementById('load_next').style.display="none";
					//document.getElementById('load_next').value="Please Wait...";
					document.getElementById('load_next').disabled=true;
					buser_id=document.getElementById('bxu').value;
					vfirstname=encodeURIComponent(document.getElementById('first-name').value);
					vlastname=encodeURIComponent(document.getElementById('last-name').value);
					vemail=encodeURIComponent(document.getElementById('email-adr').value);
					vph=encodeURIComponent(document.getElementById('mobile-num').value);
					
					//alert(situr+"minisites/get_voucher/"+buser_id);
					$.ajax({
							type:'POST',
							url:situr+"minisites/get_voucher/"+document.getElementById('bxu3').value+"/"+buser_id+"/0/"+vfirstname+"/"+vlastname+"/"+vemail+"/"+vph,
							data:_form.serialize(),
							success:function(msg){
							//alert(msg);
							//alert(document.getElementById('nd'));
								//document.getElementById('nd').innerHTML=msg;
								//document.getElementById('load_next').disabled=false;
								//alert(msg);
								if(msg==0)
								{
								//document.getElementById('s_m').innerHTML="<b>Sorry ! An email was sent before for this voucher to your email box.</b>";
								//document.getElementById('ops2').style.display="none";
								//document.getElementById('s_m').innerHTML="<div class='section' align='center' id='contact_sms' style='border:0px solid #e4e4e4; padding:0px; background-color:#FF0000; font-size:22px; font-weight:bold; color:#ffffff'><p>Sorry ! An email was sent before.<br/>Please check your inbox.</p></div>";
								//document.getElementById('load_next').value="SENT";
								//document.getElementById('loxx').innerHTML="<div align='center' id='loxx' style='font-size:18px'><b>NOT SENT</b></div>";
								}
								else
								{
								//document.getElementById('s_m').innerHTML="<b>E-mail has been sent successfuly to your mail.Please use the link from your mail to get the voucher.</b>";
								//document.getElementById('ops2').style.display="none";
								//document.getElementById('s_m').innerHTML="<div class='section' align='center' id='contact_sms' style='border:0px solid #e4e4e4; padding:0px; background-color:#91FE98; font-size:22px; font-weight:bold; color:#000000'><p>Your voucher has been sent.<br/>Please check your inbox in 5 minutes</p></div>";
								//document.getElementById('load_next').value="SENT";
								//document.getElementById('loxx').innerHTML="<div align='center' id='loxx' style='font-size:18px'><b>SENT SUCCESSFULL</b></div>";
								}
								//alert(document.getElementById('s_m'));
								//wait(10000);
								//setTimeout("alert('Three seconds has passed.');",3000);
								/*document.getElementById('s_m').style.display="none";
								
								popup.css({left: '-9999px'});
				$('div.optionsDivVisible').removeClass('optionsDivVisible').addClass('optionsDivInvisible');
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				submitBtn.unbind('click');
				$('a.print-box').unbind('click');
				$(window).unbind('resize');*/
				//document.getElementById('load_next').value="Get Voucher";
				
				return false;
								
							},
							error:function(){
								//alert('ajax error777');
							}
						});
					
					}
						/*$.ajax({
							type:'POST',
							url:_form.attr('action'),
							data:_form.serialize(),
							success:function(msg){
								//alert('ajax send');
								popup.css({left: '-9999px'});
								if (_IE) $('select').css({visibility: 'visible'});
								submitBtn.unbind('click');
								$('a.print-box').unbind('click');
								$(window).unbind('resize');
								init(_thisUrl);
								document.getElementById('load_next').style.display="none";
								showAddress_popup(document.getElementById('bxu1').value,document.getElementById('bxu2').value,'v_in_map');
							},
							error:function(){
								alert('ajax error6');
							}
						});*/
					}
					if (thisBtn.hasClass('no-ajax') && !checkFields()){
					javascript:document.images.captcha.src=situr+'chiro_masters/captcha_image/?'+Math.round(Math.random(0)*1000)+1;
						$('#c-email-adr').attr('value', $('#e-adr').val());
						$('#email-adr').attr('value', $('#e-adr').val());
						$('#mobile-num').attr('value', $('#m-num').val());
						popup.css({left: '-9999px'});
						if (_IE) $('select').css({visibility: 'visible'});
						submitBtn.unbind('click');
						$('a.print-box').unbind('click');
						$(window).unbind('resize');
						init(_thisUrl);
					}
				});
				return false;
			})
			
			
		/*	_fader.click(function(){
				$('div.optionsDivVisible').removeClass('optionsDivVisible').addClass('optionsDivInvisible');
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				submitBtn.unbind('click');
				$('a.print-box').unbind('click');
				$(window).unbind('resize');
				return false;
			});*/

			$('a.print-box').click(function(){ document.getElementById('vidiv').style.display = 'none';
						//alert(document.getElementById('lixu').value);
			    if(document.getElementById('lixu').value!="1")
				{
				document.getElementById('voucher-endx').style.display = 'none';
				}
				$('div.optionsDivVisible').removeClass('optionsDivVisible').addClass('optionsDivInvisible');
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				submitBtn.unbind('click');
				$('a.print-box').unbind('click');
				$(window).unbind('resize');
				$('html').addClass('print');
				window.print();
				$('html').removeClass('print');
				return false;
			});
		}
		
		
		_hold.ready(function(){
			if(view_voucher!=0)	
			{
			var pops_id='#voucher-end';
			prm=$('#ucase').attr('name');
			//alert(prm);
			srm=prm.split("*");
			//if(srm[3]==0)
			//{
			document.getElementById('bxu').value=srm[0];
			document.getElementById('bxu1').value=srm[1];
			document.getElementById('bxu2').value=srm[2];
			document.getElementById('bxu3').value=srm[3];
			//document.getElementById('bxu4').value=srm[4];
			document.getElementById('hit_bn').innerHTML="Voucher For "+srm[4];
			//document.getElementById('hit_vp').innerHTML="$"+srm[5]+" Voucher + Free Pillow";
			document.getElementById('hit_vp').innerHTML=srm[5];
			//alert(srm[0]);
			//}
			if(document.getElementById('bxu3').value==1)
		   {
		   //document.getElementById('v_details001'+srm[0]).innerHTML="Please wait...";
		   document.getElementById('ready_load').style.display="block";
		   $.ajax({
							type:'POST',
							url:situr+"minisites/get_voucher/0/"+document.getElementById('bxu').value+"/"+srm[6]+"/0/0/0/0/"+document.getElementById('unique_num').value,
							data:$(this).serialize(),
							success:function(msg){
							//alert(msg);
							if(msg==1)
							{
							document.getElementById('nd').innerHTML='<div class="voucher-popup" id="voucher-end" style="width:400px; border:0px;"><div class="inner" ><div class="headline"><strong>Sorry ! You have used this link before.</strong></div><div></div><a class="close" href="#">close</a></div>';
							document.getElementById('ready_load').style.display="none";
							//document.getElementById('v_details001'+srm[0]).innerHTML="Voucher Details";
							//showAddress_popup(document.getElementById('bxu1').value,document.getElementById('bxu2').value,'v_in_map');
							init(pops_id);
							}else
							{
							//alert(document.getElementById('nd'));
								document.getElementById('nd').innerHTML=msg;
								//init($(this).attr('href'));
								//popup = $(_obj);
								//popup.css({left: '-9999px'});
								//document.getElementById('v_details001'+srm[0]).innerHTML="Voucher Details";
								document.getElementById('vidiv').style.display = 'none';
								document.getElementById('voucher-end').style.display = 'none';
								document.getElementById('ready_load').style.display="none";
								showAddress_popup(document.getElementById('bxu1').value,document.getElementById('bxu2').value,'v_in_map');
								init(pops_id);
							}
								
							},
							error:function(){
								//alert('ajax error777');
							}
						});
		   }
		   else
		   {
		   init(pops_id);
		   }
			
			
			return false;
			}
			});
		
		links.click(function(){
			if ($(this).attr('href')){
			var pops_id=$(this).attr('href');
			prm=$(this).attr('id');
			srm=prm.split("*");
			//alert(prm);
			//if(srm[3]==0)
			//{
			document.getElementById('bxu').value=srm[0];
			document.getElementById('bxu1').value=srm[1];
			document.getElementById('bxu2').value=srm[2];
			document.getElementById('bxu3').value=srm[3];
			//document.getElementById('bxu4').value=srm[4];
			document.getElementById('hit_bn').innerHTML="Voucher For "+srm[4];
			//document.getElementById('hit_vp').innerHTML="$"+srm[5]+" Voucher + Free Pillow";
			document.getElementById('hit_vp').innerHTML=srm[5];
			document.getElementById('gdirection').value=srm[9];
			document.getElementById('lno').innerHTML=srm[7];
			//alert(srm[0]);
			//}
			if(document.getElementById('bxu3').value==1)
		   {
		   document.getElementById('v_details001'+srm[0]).innerHTML="Please wait...";
		   $.ajax({
							type:'POST',
							url:situr+"minisites/get_voucher/"+document.getElementById('bxu3').value+"/"+document.getElementById('bxu').value+"/1",
							data:$(this).serialize(),
							success:function(msg){
							//alert(msg);
							//alert(document.getElementById('nd'));
								document.getElementById('nd').innerHTML=msg;
								//init($(this).attr('href'));
								//popup = $(_obj);
								//popup.css({left: '-9999px'});
								document.getElementById('v_details001'+srm[0]).innerHTML="Voucher Details";
								showAddress_popup(document.getElementById('bxu1').value,document.getElementById('bxu2').value,'v_in_map');
								init(pops_id);
								
							},
							error:function(){
								//alert('ajax error777');
							}
						});
		   }
		   else
		   {
		  
			document.getElementById('cnt_m').innerHTML=srm[4];
			document.getElementById('s_m_s').innerHTML="";
			document.getElementById('s_m').innerHTML="";
			document.getElementById('load_next_fst').style.display="block";
			document.getElementById('load_next').style.display="block";
			document.getElementById('lox').innerHTML="<div align='center' id='lox' style='font-size:18px'></div>";
			document.getElementById('loxx').innerHTML="<div align='center' id='loxx' style='font-size:18px'></div>";
			//document.getElementById('load_next_fst').value="Send";
			//document.getElementById('load_next').value="Get Voucher";
			document.getElementById('load_next').disabled=false;
			document.getElementById('load_next_fst').disabled=false;
			document.getElementById('ops2').style.display="block";
			document.getElementById('ops1').style.display="block";
			//document.getElementById('hit_vp').innerHTML="$"+srm[5]+" Voucher + Free Pillow";
			
			if(srm[8]=="op1")
			{ 
			document.getElementById("sx0001").value="ptk";
			
			//javascript:document.images.captcha.src=situr+'chiro_masters/captcha_image/?'+Math.round(Math.random(0)*1000)+1;
			}
			
			if(srm[8]=="op2")
			{
			document.getElementById("sx0001").value="ybv";
			//javascript:document.images.captcha1.src=situr+'chiro_masters/captcha_image/?'+Math.round(Math.random(0)*1000)+1;
			}
			
			if(srm[8]=="op6")
			{
			
			javascript:document.images.captcha6.src=situr+'chiro_masters/captcha_image/?'+Math.round(Math.random(0)*1000)+1;
			}
			
			
			          /*  $.ajax({
							type:'POST',
							url:situr+"chiro_masters/cap_return/",
							data:$(this).serialize(),
							success:function(msg){
							//alert(msg);
							document.getElementById("sx0001").value=msg;
						    //init(pops_id);
								
							},
							error:function(){
								//alert('ajax error777');
							}
						});*/
			
		   init(pops_id);
		   }
			
			}
			else{
				init($(this).attr('title'));
			}
			return false;
		});
	});
	
}
jQuery.fn.lightPlayer = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '.link-player',
		linkCloseName: 'a.btn-close',
		divFader: 'fader'
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _IE = ((navigator.appName.indexOf('Microsoft Internet Explorer') != -1) && (parseInt(navigator.appVersion) < 9)) ? true : false;
		var links = _hold.find(_options.linkOpenName);
		var _fader = $('<div class="'+_options.divFader+'"></div>');
		
		var popup;
		$('body').append(_fader);
		_fader.css({
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 999,
			background: 'black',
			opacity: 0.7
		});
		function init(_obj, _rel){
			var width = _rel.indexOf('width=');
			var dv = _rel.indexOf(';');
			var height = _rel.indexOf('height=');
			var wp = _rel.substring(6, dv);
			var hp = _rel.substring(height+7);
			var _player = $('<div class="light-player">\n' +
							'	<div id="player1" style="display:block;width:'+wp+'px;height:'+hp+'px"></div>\n' +
							'	<script type="text/javascript">\n' +
							'		flowplayer("player1", "swf/flowplayer-3.1.5.swf", {\n' +
							'			clip : {\n' +
							'			autoPlay: true,\n' +
							'			autoBuffering: true\n' +
							'			},\n' +
							'			playlist: [\n' +
							'				"'+_obj+'"\n' +
							'			]\n' +
							'		});\n' +
							'	</script>\n' +
							'</div>');
			$('body').append(_player);
			_player.css({
				position: 'absolute',
				background: 'black',
				top: '0px',
				left: '-9999px',
				zIndex: 1000
			});
			popup = _player;
			var btnClose = popup.find(_options.linkCloseName);
			var submitBtn = popup.find('.link-submit');
			
			if (_IE){
				$('select').css({visibility: 'hidden'});
				popup.find('select').css({visibility: 'visible'});
			}
			var w = $('body').width();
			var _w = $('#wrapper').width();
			if (_w > w) w =_w;
			var h = $(window).height();
			var _offset = $('html').scrollTop();
			
			var ret = _offset+(h/2) - popup.outerHeight(true)/2;
			if (ret < 0) ret = 0;
			popup.css({
				top: ret,
				left: w/2 - popup.outerWidth(true)/2
			}).hide();
			_fader.css({
				width: w,
				height: $('#wrapper').height()
			}).fadeIn(300, function(){
				popup.fadeIn(300);
			});
			$(window).resize(function(){
				w = $('body').width();
				_w = $('#wrapper').width();
				if (_w > w) w =_w;
				popup.css({
					left: w/2 - popup.outerWidth(true)/2
				});
				_fader.css({
					width: w
				});
			});
			
			btnClose.click(function(){
				popup.css({left: '-9999px'});
				_fader.hide();
				if (_IE) $('select').css({visibility: 'visible'});
				$(window).unbind('resize');
				return false;
			});
			_fader.click(function(){
				popup.css({left: '-9999px'});
				if (_IE) $('select').css({visibility: 'visible'});
				$('#player1_api').remove();
				_player.remove();				_fader.hide();
				$(window).unbind('resize');
				return false;
			});
		}
		links.click(function(){
			init($(this).attr('href'), $(this).attr('rel'));
			return false;
		});
	});
}


jQuery.fn.rate = function(_options){
var _options = jQuery.extend({
		duration: 700,
		linkOpenName: '#rate'
		},_options);
var _hold = $(this);
var _speed = _options.duration;
var _IE = ((navigator.appName.indexOf('Microsoft Internet Explorer') != -1) && (parseInt(navigator.appVersion) < 9)) ? true : false;
var links = _hold.find(_options.linkOpenName);
links.click(function(){
            //alert('sss');
			val_lnk=$(this).attr('href');
			val_lnk=val_lnk.split('#');
			//alert(val_lnk[1]);
			//alert(document.getElementById('bxu').value);
			document.getElementById("rate_load").style.display="block";
			$.ajax({
							type:'GET',
							url:situr+"minisites/rate/"+val_lnk[1]+"/"+document.getElementById('bxu').value,
							data:$(this).serialize(),
							success:function(msg){
							//alert(msg);
							document.getElementById("rate_load").style.display="none";
							document.getElementById("rate_group").innerHTML=msg;
							
						    //init(pops_id);
								
							},
							error:function(){
								alert('ajax error777');
							}
						});
			
		});
}	
