jQuery(document).ready(function(){ 

   // set home tab
    var rel = jQuery("#today").attr('rel');

     if(location.href == rel) {
       jQuery(".home").attr('class', 'home active'); 
     } 
   /*<<  3 steps landing  >>*/
    var step;
    var form = jQuery('form#landingregform[class="three"]');
    jQuery(form).find('select,input[type=text]').hide();
    step = 0;

    function next(form){
	var select = jQuery(form).find('select');
	var inp = jQuery(form).find('input[type=button]');
	if( step == 1 ) {
	    jQuery(select).slideDown('fast');
	    jQuery(inp).removeClass().addClass('button_blue_long2 bold');
	    jQuery(inp).val('Продолжить');
	} else if( step == 2 ) {
	    jQuery(select).slideUp('fast', function(){
		jQuery(form).find('input#input_email_catcher2').slideDown('fast')
	    });
	    jQuery(inp).val('Зарегистрироваться');
	} else if( step == 3 ) { 
	    jQuery(form).submit();
	}
    }

    jQuery(form).find('input[type=button]').click(function(event){
	
	event.preventDefault();
	var headText = jQuery(form).parent().find('h2 font');
	var submit = jQuery(form).find('input[type=button]');
	if( step == 0 ){
	    jQuery(headText).text('Выберите город');
	    jQuery(submit).val('');
	} else if(step == 1){
	    if(jQuery(this).parent().find('select').val() == 0){
		alert('Выберите город');
		return;
	    }
	    jQuery(headText).text('Введите email');
	} else if( step == 2){
	    var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ ;
	    var str = jQuery(this).parent().find('input[name=dealticker_email]').val();
	    if(jQuery(this).parent().find('input[name=dealticker_email]').val() == '' || jQuery(this).parent().find('input[name=dealticker_email]').val() == 'Мой E-mail'){
		alert('Введите Email');
		return;
	    } else if(!regex.test(str)){
		alert('Неверно указаный E-mail');
		return;
	    }
	    jQuery(submit).val('');
	}
	step++;
	next(jQuery(this).parent());
    });
    /*<<  3 steps landing end  >>*/
    
    jQuery('#olredyByQ').click(function(event){
	var attr = jQuery('.informationBolckSmall').css('display');
	if (attr == 'none') {
	    jQuery('.informationBolckSmall').css('display', 'block');
	} else {
	    jQuery('.informationBolckSmall').css('display', 'none');
	}
    });
    
    jQuery('.faq_content_block').hide();
    jQuery('.blue_panel').click(function(){
	var h3 = jQuery(this);
	jQuery(this).parent().find('p').slideToggle("fast",function(){
	    if(jQuery(h3).hasClass('blue_panel')){
		jQuery(h3).removeClass().addClass('green_panel');
	    } else {
		jQuery(h3).removeClass().addClass('blue_panel');
	    }
	});
    });    
    
    // zoom image
    jQuery('a.zoom').zoomimage({centered: true,hideSource: true, border:10});
   
    
    // conditions in reviews
    jQuery('#termsfor').click(function() {
	jQuery('#accepttermsblock').css('display', 'block');
    });
    
    jQuery('#send-review').click(function() {
	var checked = jQuery('#accepttermscomment').attr('checked');
	if(checked != 'checked') {
	    alert('Вам необходимо согласиться с правилами использования комментариев!');
	    return false;
	}
    })
    
    // custom auth form
    jQuery("#mymail").click(function(){
        var inputValue = jQuery("#mymail").val();
        if(inputValue == 'Ваш e-mail') {
            jQuery("#mymail").val('');    
        }
    });        

    jQuery("#mymailsmall").click(function(){
	if(jQuery("#mymailsmall").val() == 'e-mail') {
	    jQuery("#mymailsmall").val('');    
	    jQuery("#mymailsmall").removeClass('atentioninput').addClass('inputtextrows');

	} else {
	    jQuery("#mymailsmall").removeClass('atentioninput').addClass('inputtextrows');
	}
    });
    
    jQuery("#VoitiButton").click(function(){
	if (jQuery("#mymailsmall").val() == '') {
	    jQuery("#mymailsmall").removeClass('inputtextrows').addClass('atentioninput');   
	    return false;
	}
	else {
	    if (jQuery("#pwdsmallinputspec").val() == '') { 
		jQuery("#pwdsmallinputspec").removeClass('inputtextrows').addClass('atentioninput');     
		return false;
	    } else {
		jQuery("#smallloginformauto").submit();    
	    }
	}
    }); 
    
   // custom register form
    jQuery("#submitregisterbutton").click(function() {
	if(jQuery('#email-register-field').val() == '') {
	    jQuery("#email-register-field").removeClass('registerlinginput').addClass('atentioninputregister');
	    return false;  
	} else {
	    var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ ;
	    var str = jQuery("#email-register-field").val();
	    if(!regex.test(str)){
		alert('Не валидный e-mail адрес');
		return;
	    } else {
		jQuery("#registerformsmall").submit();
	    }
	}
    });
    
    // buy product
    jQuery("#buy-product").click(function() {
	jQuery(".popup").css('display', 'block');
	return false;
    });
    
    jQuery(".popup_back_close").click(function() {
	jQuery(".popup").css('display', 'none');
	return false;
    });
    
    // personal info change
    jQuery("#changeMyInfo").click(function() {
	var mode = jQuery("#personalinfocheindge").css("display");
	if(mode == "none") {
	    jQuery("#personalinfocheindge").css("display", "block");
	    return false;
	} else {
	    jQuery("#personalinfocheindge").css("display", "none");
	    return false;
	}
    });
    
    // password change
    jQuery("#changeMyPassword").click(function() {
	var mode = jQuery("#personalinfocheindgePwd").css("display"); 
	if(mode == "none") {
	    jQuery("#personalinfocheindgePwd").css("display", "block");
	    return false;
	} else {
	    jQuery("#personalinfocheindgePwd").css("display", "none");
	    return false; 
	}
    });
    
    // ajax change password action
    jQuery("#change_pass").live('click', function() { 
	 var current_password = jQuery("#current_password").val();
	 var new_password = jQuery("#new_password").val();
	 var confirm_password = jQuery("#confirm_password").val();
	 var url = jQuery("#url_change_pass").val();
	 var url_redirect = jQuery("#url_redirect").val();
	 
	 jQuery.post(url, {
			   current_password: current_password, 
	                   new_password: new_password, 
			   confirm_password: confirm_password
		          }, 
	 function(data) {
	   jQuery('#status').html(eval(data));
	   if(eval(data) == 'Пароль успешно сохранен') {
	       location.href = url_redirect;
	   }
	   return false;
	 });
    });

});

// auth and register form
function switchfunc(objID) {
    if(jQuery('#' + objID).css('display') == 'none') {
	jQuery('#' + objID).show(400);
    } else {
	jQuery('#' + objID).hide();
    }
}

function selTab() {
	var about = jQuery('#AboutDealTab').attr('class');
	var comments = jQuery('#CommentsTab').attr('class');
       
        if(about == 'noselectedAccTabs') { 
	  jQuery('#AboutDealTab').attr('class', 'selectedAccTabs');
	  jQuery('#CommentsTab').attr('class', 'noselectedAccTabs');
	} else {
	  jQuery('#AboutDealTab').attr('class', 'noselectedAccTabs');
	  jQuery('#CommentsTab').attr('class', 'selectedAccTabs');
	}
}

function selProf1Tab() {
	var amount = jQuery('#AmountTab').attr('class');
	var coupones = jQuery('#CuponesTab').attr('class');
	var profile = jQuery('#ProfileTab').attr('class');
	
       
        if(amount == 'noselectedAccTabs') { 
	  jQuery('#AmountTab').attr('class', 'selectedAccTabs');
	  jQuery('#CuponesTab').attr('class', 'noselectedAccTabs');
	  jQuery('#ProfileTab').attr('class', 'noselectedAccTabs');
	}
}

function selProf2Tab() {
	var amount = jQuery('#AmountTab').attr('class');
	var coupones = jQuery('#CuponesTab').attr('class');
	var profile = jQuery('#ProfileTab').attr('class');

	if(coupones == 'noselectedAccTabs') { 
	  jQuery('#AmountTab').attr('class', 'noselectedAccTabs');
	  jQuery('#CuponesTab').attr('class', 'selectedAccTabs');
	  jQuery('#ProfileTab').attr('class', 'noselectedAccTabs');
	}
}

function selProf3Tab() {
	var amount = jQuery('#AmountTab').attr('class');
	var coupones = jQuery('#CuponesTab').attr('class');
	var profile = jQuery('#ProfileTab').attr('class');

       if(profile == 'noselectedAccTabs') { 
	  jQuery('#AmountTab').attr('class', 'noselectedAccTabs');
	  jQuery('#CuponesTab').attr('class', 'noselectedAccTabs');
	  jQuery('#ProfileTab').attr('class', 'selectedAccTabs');
	}
}

function clearInput(preset,field) {
    if(escape(field.value) == preset) {
	field.style.color = "#000066";
	field.value="";
    }
}

function resetInput(preset,field) {
    if(field.value == "") {
	field.style.color = "#acacac";
	field.value = unescape(preset);
    }	
}

// review counter length comments
function autoclear() {
    var conte = jQuery("#review_field").val();
    var leftchars = 1000 - conte.length;
    var othtml = 'осталось ' + leftchars + ' символов';
    jQuery("#leftchars").html( othtml );
    if( leftchars < 0 ) {
	jQuery("#review_field").val( jQuery("#review_field").val().substring(0,1000) );   
    }
}

var timer_interval;
var input_placeholder;

function start() { 
	timer_interval = window.setInterval("countDown()", 1000);
}

function countDown() { 
	
	var current_time = new Date();
	
	var now = parseInt(current_time.getTime()/1000); 

	var deal_ends = parseInt(jQuery('#deal_endtime').val());     
	
	var seconds_to_go = deal_ends-now
	
	if(seconds_to_go>0) {
		var hours_to_go =  Math.floor(seconds_to_go/3600);
		var minutes_to_go =  Math.floor((seconds_to_go-(hours_to_go*3600))/60);
		var seconds_to_go =  Math.floor(seconds_to_go-(minutes_to_go*60)-(hours_to_go*3600));
	} else {
		var hours_to_go =  0;
		var minutes_to_go =  0;
		var seconds_to_go =  0;
	}
	
	if(document.getElementById("deal_hours_left")) {
		document.getElementById("deal_hours_left").firstChild.nodeValue = hours_to_go;
		document.getElementById("deal_minutes_left").firstChild.nodeValue = minutes_to_go;
		document.getElementById("deal_seconds_left").firstChild.nodeValue = seconds_to_go;
	}

}

/**
 * We use the initCallback callback
 * to assign functionality to the controls
 */
function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

// Ride the horizontal carousel
jQuery(document).ready(function() {
    jQuery("#mycarousel").jcarousel({
        scroll: 3,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
});

// Ride the vertical carousel
jQuery(document).ready(function() {
    jQuery("#mycarousel-vert").jcarousel({
	vertical: true,
        scroll: 3,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
});

// Validation fields
jQuery(document).ready(function() {
    
   var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; 
   // landing register
   jQuery('#senderButtonLand').live('click', function() {
       
        // city validation
	var cityVal = jQuery("#city_dropdown_catcher2").val();
	if(cityVal == 0) {
            alert('Не выбран город');
	    return false;
        }
       
        // email validation
        var emailaddressVal = jQuery("#input_email_catcher2").val();
	
	if(emailaddressVal == 'Введите ваш email адрес') {
	    alert('Не введен e-mail адрес');
	    return false;
	}
	else if(!emailReg.test(emailaddressVal)) {
            alert('Не верный формат e-mail адреса');
	    return false;
        }
   }) 
      // custom register
      jQuery('#senderButtonReg').live('click', function() {
       
        // city validation
	var cityVal = jQuery("#city_dropdown_catcher").val();
	if(cityVal == 0) {
            alert('Не выбран город');
	    return false;
        }
       
        // email validation
        var emailaddressVal = jQuery("#input_email_catcher").val();
	
	if(emailaddressVal == 'Введите ваш email адрес') {
	    alert('Не введен e-mail адрес');
	    return false;
	}
	else if(!emailReg.test(emailaddressVal)) {
            alert('Не верный формат e-mail адреса');
	    return false;
        }
   }) 
});

function checkVirtual(bundle)
{
    jQuery('#' + bundle).attr('checked', 'checked');
}






