$().ready( function() {
	$('#about-button').click( function() {
		$('#form-wrapper-text').hide();
		$('#about-ectivo').fadeIn('slow');
		$("html,body").animate({scrollTop: $("#header").offset().top}, 500);
		return false;
	});	
	$('#plugin-btn').click(function(){
		if( $('#right-col').is( ":hidden ")){
			$('#thumbs-sample').hide();							 
			$('#cont .cont-col').fadeIn('slow');		
			$("html,body").animate({scrollTop: $("#cont").offset().top}, 500);
		} else {
			$('#cont .cont-col').fadeOut('fast');
			$("html,body").animate({scrollTop: $("#cont").offset().top}, 500);
		}
	});
	$('#menu-sample-btn').click(function() {
		$('#thumbs-sample,#left-col-2').fadeIn('slow');	
		$("html,body").animate({scrollTop: $("#cont").offset().top}, 500);	
		return false;
	});
	$('#sample-btn').click(function(){
		if( $('#thumbs-sample').is( ":hidden ")){
			$('#cont .cont-col').hide();							 
			$('#thumbs-sample,#left-col-2').fadeIn('slow');	
			$("html,body").animate({scrollTop: $("#cont").offset().top}, 500);			
		} else {
			$('#thumbs-sample,#left-col-2').fadeOut('fast');
			$("html,body").animate({scrollTop: $("#cont").offset().top}, 500);		
		}
	});
	//contact us button to reveal contact us section
	$('#contact-us-btn').click(function() {
		$('#form-wrapper-text').hide();
		$('#about-ectivo').hide();
		$('.head-row3').css('height','391px');
		$('.head-row3 #featured img.banner-img').css('marginTop','65px');
		$(this).addClass('active-trail').addClass('active');
		$(this).find('a').addClass('active').find('span').addClass('active');
		/*if( $('#contactus-area').is(":hidden")){
			$('#phone-contact').fadeIn('slow');
		}*/
		$('#contactus-area').fadeIn('slow');
	});
	
	$('#real-person-btn').click(function() {
		$('#form-wrapper-text').hide();
		$('#about-ectivo').hide();
		$('.head-row3').css('height','391px');
		$('.head-row3 #featured img.banner-img').css('marginTop','65px');
		$('#contact-us-btn').addClass('active-trail').addClass('active');
		$('#contact-us-btn').find('a').addClass('active').find('span').addClass('active');
		/*if( $('#contactus-area').is(":hidden")){
			$('#phone-contact').fadeIn('slow');
		}*/
		$('#contactus-area').fadeIn('slow');
	});
	
	/*$('#switch-phone').click(function() {
		if($('#about-ectivo').is(":visible")){
			$('#about-ectivo').hide();
		}
		if($('#form-wrapper-text').is(":visible")){
			$('#form-wrapper-text').hide();
		}
		$('#contactus-area').hide();
		$('#phone-contact').fadeIn('slow');
	});*/
	
	$('#back-to-form').click(function() {
		if($('#form-wrapper-text').is(":visible")){
			$('#form-wrapper-text').hide();
		}
		$('#phone-contact').hide();
		$('#contactus-area').fadeIn('slow');
	});
	
	//submit contact form
	function submit_cnt_form(btnid,formid){
		var bid = '#' + btnid;
		var fid = '#' + formid;
		$(bid).click( function () {
			$('<img src="images/ajax-loader.gif" class="ajax-loader" />').insertAfter($(bid));	
			//get the data
			var name = $(fid + ' #name').val();
			var email = $(fid + '  #email').val();
			var phone = $(fid + ' #phone').val();
			var country = $(fid + ' #country').val();
			var msg = $(fid + ' #msg').val();
			var gbo = $(fid + ' #getback-option').val();
			var formID = $(fid + ' #formID').val();
			var about = $(fid + ' #about').val();
			var fdata = {"name": name, 'email': email, "phone": phone, "country": country, "msg": msg,"gbo": gbo, "about": about, "formID": formID};
			var time = 
			$(this).oneTime(1000,function() {
				$.ajax({  
					type: "post",  
					url : "sendform.php", 
					data:  fdata,
					cache: false,
					success: function(data){
						if(data == 'success'){
						$(fid + ' img').remove('.ajax-loader');  
						$(fid + ' a').remove(bid);
						$(fid + ' span').remove('.cberror');
						$('<span class="color2 cbmsg"><strong>Done!</strong> We’ll be in touch super soon!</span>').appendTo(fid + ' .msg-area');
						} else {
							$(fid + ' img').remove('.ajax-loader');  
							$(fid + ' span').remove('.cberror');
							$('<span class="color3 cberror">' + data + '</span>').appendTo(fid + ' .msg-area');
						}
					} 							
				});
			});
			return false;
		});
	}
	submit_cnt_form('submit-contact-form', 'contactus-form');
	submit_cnt_form('submit-interest-form', 'ryi-form');
	submit_cnt_form('submit-question-form', 'ask-form');
	
	$(document).pngFix();
	var quickList = new  Array();
	//build the country list as local data for jquery autocomplete
	$('.quickList').find("option").each(function(){
		var country = {"cc": this.value, "name": $(this).html()};
		quickList.push(country);
	});
	
	//focus function for the country filter
	$("#quickFilterInput").focus(function(){
		if($(this).val() == 'Type country'){
			$(this).val('');
			$(this).removeClass('color-grey');
		}
	});
	
	//blur function for the country filter
	$("#quickFilterInput").blur(function(){
		if($(this).val() == ''){
			$(this).val('Type country');
			$(this).addClass('color-grey');
		}
	});
	$("#quickFilter-clear").click(function() {
		$("#quickFilterInput").val('Type country').addClass('color-grey');	
		$("#quickFilter-clear").hide();	
		return false;
	});
	 
	 //init country filter based on jquery autocomplete
	$("#quickFilterInput").autocomplete(quickList, {
		minChars: 1,
		width: 310,
		matchContains: true,
		max: 10,
		scroll: false,
		formatItem: function(row, i, max) {
			return "<span class=\" flag " + row.cc + "\"></span>"  + row.name;
		},
		formatMatch: function(row, i, max) {
			return "<span class=\" flag " + row.cc + "\"></span>" +  row.name;
		},
		formatResult: function(row) {
			return row.name;
		}
	}).result(function(event, item) {
		showTabs(item.cc);
	});
	
	function showTabs(cname){
		var domain = 'http://www.fectiv.com/ectivodemo/';
		
		if($('#custom .block').attr('display') != 'none'){
			$('#custom .block').hide();
		}
		$('.custom-loader').show();
		var tab1 = $('#block-block-14 a');
		var tab2 = $('#block-block-15 a');
		var tab3 = $('#block-block-16 a');
		var img1 = $('#block-block-14 a img');
		var img2 = $('#block-block-15 a img');
		var img3 = $('#block-block-16 a img');
		
		img1.attr('src','');
		img2.attr('src','');
		img3.attr('src','');
		
		var tab1val = 'basic.html';
		var tab2val = 'template.html';
		var tab3val = 'eshopping.html';
		var img1val = 'images/banner.png';
		var img2val = 'images/banner2.png';
		var img3val = 'images/banner3.png';
		if(cname == 'AU'){
			tab1val = 'au/basic.html';
			tab2val = 'au/template.html';
			tab3val = 'au/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'au/images/banner3.png';
			
		} else if(cname == 'GB'){
			tab1val = 'uk/basic.html';
			tab2val = 'uk/template.html';
			tab3val = 'uk/eshopping.html';
			img1val = 'uk/images/banner.png';
			img2val = 'uk/images/banner2.png';
			img3val = 'uk/images/banner3.png';
		} else if(cname == 'PK'){
			tab1val = 'pk/basic.html';
			tab2val = 'pk/template.html';
			tab3val = 'pk/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'pk/images/banner3.png';
		} else if(cname == 'PH'){
			tab1val = 'ph/basic.html';
			tab2val = 'ph/template.html';
			tab3val = 'ph/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'ph/images/banner3.png';
		} else if(cname == 'NZ'){
			tab1val = 'nz/basic.html';
			tab2val = 'nz/template.html';
			tab3val = 'nz/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'nz/images/banner3.png';
		} else if(cname == 'IN'){
			tab1val = 'india/basic.html';
			tab2val = 'india/basic.html';
			tab3val = 'india/template.html';
			img1val = 'india/images/india_diy.png';
			img2val = 'india/images/banner.png';
			img3val = 'au/images/banner3.png';
		} else if(cname == 'IE'){
			tab1val = 'ie/basic.html';
			tab2val = 'ie/template.html';
			tab3val = 'ie/eshopping.html';
			img3val = 'ie/images/banner3.png';
		} else if(cname == 'ID'){
			tab1val = 'id/basic.html';
			tab2val = 'id/basic.html';
			tab3val = 'id/eshopping.html';
			img1val = 'id/images/indonesia_diy.png';
			img2val = 'id/images/banner.png';
			img3val = 'id/images/banner3.png';
		} else if(cname == 'EU'){
			tab1val = 'eu/basic.html';
			tab2val = 'eu/template.html';
			tab3val = 'eu/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'eu/images/banner3.png';
		} else if(cname == 'CN'){
			tab1val = 'cn/basic.html';
			tab2val = 'cn/template.html';
			tab3val = 'cn/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'cn/images/banner3.png';
		} else if(cname == 'CA'){
			tab1val = 'ca/basic.html';
			tab2val = 'ca/template.html';
			tab3val = 'ca/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'ca/images/banner3.png';
		} else if(cname == 'BR'){
			tab1val = 'br/basic.html';
			tab2val = 'br/template.html';
			tab3val = 'br/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'br/images/banner3.png';
		} else if(cname == 'US'){
			tab1val = 'us/basic.html';
			tab2val = 'us/template.html';
			tab3val = 'us/eshopping.html';
			img1val = 'images/banner.png';
			img2val = 'images/banner2.png';
			img3val = 'us/images/banner3.png';
		}
		
		tab1.attr('href',domain + tab1val);
		tab2.attr('href',domain + tab2val);
		tab3.attr('href',domain + tab3val);
		img1.attr('src',domain + img1val);
		img2.attr('src',domain + img2val);
		img3.attr('src',domain + img3val);
		$("#quickFilter-clear").show();	
		$('#custom .content img').load(function(){
				$('.custom-loader').hide();
				$('#custom .block').fadeIn('slow');
		});
		
	}
	
	if( $('#thumbs-sample a.preview').length > 0){
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
		/* END CONFIG */
		$("#thumbs-sample a.preview").hover(function(e){
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? "<br/>" + this.t : "";
			$("body").append("<p id='preview'><img src='"+ this.name +"' alt='Image preview' />"+ c +"</p>");								 
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");						
		},
		function(){
			this.title = this.t;	
			$("#preview").remove();
		});	
		$("#thumbs-sample a.preview").mousemove(function(e){
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});				
	}
});
