$(function() {
    $('input.button').each(function(){
    	var old_class = $(this).attr('class');
        $(this).after(unescape('%3Cspan class="'+old_class+'"%3Eaa%3C/span%3E'));
        $(this).hide();
        $(this).next('span.button').text($(this).val()).click(function(){
            $(this).prev('input.button').click();
        });
    });
    Cufon.replace('span.button'); 
});

(function($){
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == default_value) $(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "") $(this).val(default_value);
			});
		});
	};
})(jQuery);


$(window).load(function () {
	$('input[type="text"]').clearDefault();
});

// External Func!
function setExternal() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  if (links.length == 0) return false;
  for (var i = 0; i < links.length; i++) {
    var relation = links[i].getAttribute("rel");
    if (relation == "external") {
      links[i].onclick = function() {
        return !window.open(this.href);
      }
    }     
  }
}


$(document).ready(function(){
	// <a href="#"> return false
	$('a[href=""]').each(function(){
		$(this).click(function(){
			return false;
		});
	});
	
	setExternal();
	
	
	$('#menu ul li a').each(function(){
		if(window.location.href.indexOf($(this).attr('href').replace(new RegExp('[^A-Za-z0-9\-]'),'')) > 0){
			if($(this).parent().parent().parent().attr('id') == 'menu'){
				$(this).addClass('nav-selected');
			}else{
				$(this).parent().parent().prev().addClass('nav-selected');
			}
		}
	});

	if($('#menu .nav-selected').hasClass('nav-selected') == false){
		$('#menu ul li:first a').addClass('nav-selected');
	}
	

	//Kategori Listeleme Grid Fix :)
	var is_category = $("#category-selection").length;
	
	$(window).load(function() {
		if (is_category > 0) {
			$("#category-selection .category-selection:nth-child(3n+2)").addClass("grid");
			
			$(".category-selection-title").each(function(){
				//console.log($(this).width());
				var margin_val = (($(this).width()+16)/2)*(-1);
				$(this).css("margin-left",margin_val+"px");
			});
			
			$(".category-selection").hover(function(){
				$("> .category-selection-title",this).removeClass("hover");
				Cufon.refresh('.font-fc');
			},function(){
				$("> .category-selection-title",this).addClass("hover");
				Cufon.refresh('.font-fc');
			})
		}
	});
	
	
	//Accordeon for FAQ
	var is_faq = $("#faq").length;
	
	if (is_faq > 0) {
		$("#faq .div-title").each(function(){
			$(this).children("a").click(function(){
				if ($(this).hasClass("active"))
				{
					$(this).removeClass("active");
				}
				else
				{
					$("#faq a.active").each(function(){
						$(this).click();
					});
					$(this).addClass("active");
				}
				$(this).parent().next(".faq-body").toggle('slow', function() {
				    // Animation complete.
				});
				return false;
			});
		});
	}
	
	
	//brands menü için link implementasyonu
	$("#brands-menu div:not(:first-child)").click(function(){
		var target_brand = $(this).find("a.hideme").attr("href");
		window.location = target_brand;
		});
	
	$("#brands-menu div").hover(function(){
		$("> span",this).addClass("hoverIt");
		Cufon.refresh('.font-fbc');
	},function(){
		$("> span",this).removeClass("hoverIt");
		Cufon.refresh('.font-fbc');
	})
	
	$(".orjinal_urunler_trigger").hover(function(){
		$(this).addClass("hoverIt");
		//Cufon.refresh('.font-fbc');
	},function(){
		$(this).removeClass("hoverIt");
		//Cufon.refresh('.font-fbc');
	});
	
	
	$("#brands-menu div:first").click(function(){
			var is_collapsed = $(this).hasClass("collapsed");
			if (is_collapsed)
			{
				var nav_item_count_ = $("> a",this).length;
				var nav_item_height_ = (nav_item_count_*24)+38;
				$(this).removeClass("collapsed");
				$(this).css({height: 38});
				$(this).animate({height: nav_item_height_+"px"}, 300, 'jswing');
			
			}
			else
			{
				$(this).addClass("collapsed");
				$(this).animate({height: 38+"px"}, 300, 'jswing');
			}
		});
		
	var is_service_form_footer = $("#send-form-to-me-input").length;
	
	if (is_service_form_footer > 0) {
		
		var is_checked = $("#send-form-to-me:checked").val();
		
		if (is_checked == undefined) {
			//$("#send-form-to-me-input .round-input").parent().css("display","none");
			$("#send-form-to-me-input input").attr('disabled', true);
		}
		else
		{
			//$("#send-form-to-me-input .round-input").parent().css("display","block");
			$("#send-form-to-me-input input").removeAttr('disabled');
		}
		
		$("#send-form-to-me").click(function(){
			
			var is_checked = $("#send-form-to-me:checked").val();
			
			if (is_checked == undefined) {
				//$(".service-form-footer .round-input").parent().css("display","none");
				//$("#send-form-to-me-input .round-input").parent().fadeOut();
				$("#send-form-to-me-input input").attr('disabled', true);
			}
			else
			{
				//$(".service-form-footer .round-input").parent().css("display","block");
				//$("#send-form-to-me-input .round-input").parent().fadeIn();
				$("#send-form-to-me-input input").removeAttr('disabled');
			}
		});
	}
	
	var is_endercheck = $(".endercheck").length;
	
	if (is_endercheck > 0) {
		$(".endercheck a").each(function(){
			$(this).click(function(){
				if ($(this).hasClass("passive"))
				{
					alert ("Diğer seçiminizi temizleyiniz.\n(Diğer seçimi tekrar tıklayınız.)");
				}
				else if ($(this).hasClass("warranty-yes"))
				{
					var is_checked = $("#warranty-yes:checked").val();
					//alert (is_checked);
					if (is_checked == undefined) {
						$("#warranty-yes").click();
						$(this).addClass("active");
						$("span a.warranty-no").addClass("passive");
						Cufon.refresh('.font-fbc');
					}
					else if (is_checked != undefined)
					{
						$("#warranty-yes").click();
						$(this).removeClass("active");
						$(".warranty-no").removeClass("passive");
						Cufon.refresh('.font-fbc');
					}
				}
				else if ($(this).hasClass("warranty-no"))
				{
					var is_checked = $("#warranty-no:checked").val();
					//alert (is_checked);
					if (is_checked == undefined) {
						$("#warranty-no").click();
						$(this).addClass("active");
						$("span a.warranty-yes").addClass("passive");
						Cufon.refresh('.font-fbc');
					}
					else if (is_checked != undefined)
					{
						$("#warranty-no").click();
						$(this).removeClass("active");
						$(".warranty-yes").removeClass("passive");
						Cufon.refresh('.font-fbc');
					}
				}
				return false;
			});
		});
	}
		
	var is_errors = $("#errors").length;
	
	if (is_errors > 0) {
		$(".endercheck a").each(function(){
			$(this).click(function(){
				if ($(this).hasClass("passive"))
				{
					alert ("Diğer seçiminizi temizleyiniz.\n(Diğer seçimi tekrar tıklayınız.)");
				}
				else if ($(this).hasClass("warranty-yes"))
				{
					var is_checked = $("#warranty-yes:checked").val();
					//alert (is_checked);
					if (is_checked == undefined) {
						$("#warranty-yes").click();
						$(this).addClass("active");
						$("span a.warranty-no").addClass("passive");
						Cufon.refresh('.font-fbc');
					}
					else if (is_checked != undefined)
					{
						$("#warranty-yes").click();
						$(this).removeClass("active");
						$(".warranty-no").removeClass("passive");
						Cufon.refresh('.font-fbc');
					}
				}
				else if ($(this).hasClass("warranty-no"))
				{
					var is_checked = $("#warranty-no:checked").val();
					//alert (is_checked);
					if (is_checked == undefined) {
						$("#warranty-no").click();
						$(this).addClass("active");
						$("span a.warranty-yes").addClass("passive");
						Cufon.refresh('.font-fbc');
					}
					else if (is_checked != undefined)
					{
						$("#warranty-no").click();
						$(this).removeClass("active");
						$(".warranty-yes").removeClass("passive");
						Cufon.refresh('.font-fbc');
					}
				}
				return false;
			});
		});
	}
	
	
	//Nav-Up Linki
	$('.nav-up').click(function(){
		$('html, body').animate({scrollTop: 0}, 'slow');	
		return false;
	});
	
	//Language
	$('div.language div ul').hover(function(){
		$(this).parent().css('overflow', 'visible');
		$(this).parent().addClass('hover');
	}, function(){
		$(this).parent().css('overflow', 'hidden');
		$(this).parent().removeClass('hover');
	});	
	
	//Elmalma
	$(".elmalmaband").hover(function(){
		$("span.elmalma-tip").fadeIn(500);
	},
	function(){
		$("span.elmalma-tip").fadeOut(500);
	});
	
	//Menü'de oluşan Cufon sorunu için refresh
	$("#nav-menu .nav-software").hover(function(){
		Cufon.refresh('#nav-menu li a');
	});
	
	$(".nav-about").mouseout(function(){
		setTimeout(function() {
			Cufon.refresh('#nav-menu li a');
		}, 1);
	});
	
	
	$(".nav-about, .nav-service").mouseout(function(){
		setTimeout(function() {
			Cufon.refresh('#nav-menu li a');
		}, 50);
	});
	
	
	// Menu Anim
	$("#nav-menu li").each(function(){
		var nav_item_count = $("> .nav-sub-item > span",this).length;
		var nav_item_height = nav_item_count*23;
		$(this).hover(function(){
			$("> .nav-sub-item",this).css({height: 0});
			$("> .nav-sub-item",this).animate({height: nav_item_height+"px"}, 300, 'easeOutBounce');
		},function(){
			if($("> .nav-sub-item",this).is(':animated'))
			{
				$("> .nav-sub-item",this).clearQueue().stop();
				$("> .nav-sub-item",this).animate({height: 0}, 200, 'easeOutBounce');
			}
			else
			{
				$("> .nav-sub-item",this).css({height: nav_item_height});
				$("> .nav-sub-item",this).animate({height: 0}, 200, 'easeOutBounce');
			}
		
		});
	
	});
	
});

//Soldaki Markalar Açılır Menüsü
/*var myMenu;
window.onload = function() {
	myMenu = new SDMenu("brands-menu");
	myMenu.init();
};*/
