// JavaScript Document

$(document).ready(function(e) {
/*
	$('.siteTopBanner .close').click(function(e) {
        $('.siteTopBanner').slideUp();
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + 100);
		var c_value=escape('1') + "; expires="+exdate.toUTCString();
		document.cookie="dbk" + "=" + c_value;
    });
*/
	
	$('#addAddressCheckBox').change(function(e) {
        var isChecked = $(this).find('input').is(':checked');
		if(isChecked)
		{
			$('#addressTable').slideDown('fast');
		}
		else{
			$('#addressTable').slideUp('fast');
		}
    });
	
	$('#bankPaymentTable').find('.paymentCol').click(function(e) {
        $(this).find('input').attr('checked','checked');
		$('#bankPaymentTable').find('.paymentCol').removeClass('selected');
		$(this).addClass('selected');
    }); //satınalma sayfası
	
	
	
	var pTitle = new productTitle('.productTitle'); //anasayfa
	
	
	//var lgnDrop = new loginDrop();
	var pGallery = new productGallery('#productGallery');
	
	$('.options').each(function (index, value) {
		value.optionsObj = new inputOptions(value,true);
	});
	
	var siparisNotuCharStatus = new updateCharStatus('#siparisNotu');
	
	
if($.browser.msie){
    var majorVersion = parseInt($.browser.version.substring(0, 1));
    if(majorVersion < 8){
       	//$('select').selectmenu();
    }else{
       	$('select').selectmenu();
    }
}else{
    $('select').selectmenu();
}



	
	frequentlyAsked = new accordion('#frequentlyAskedQuestions');
	
	$('.radioTabs').each(function (index, value) {
		value.radioTabsObj = new radioTabs(value,true);
	})
	
	
	$('.selectOptions').each(function(index, value) {
        value.selectOptionsObj = new showProductOptions(value,true);
    });
	
	
	
	
	
	$('#BuyButton').click(function(e) {
		if($('#Contract').attr('checked') == false){
			addNotification('.contractCheckBox','Sözleşmeyi kabul etmediniz','err');
		} else {
			$('#siparisForm').submit();
		}
    });
	
});



function productTitle(ID){
	thisObj = this;
	this.percent = 100;
	this.minimize = function(){
		this.percent = this.percent-2;
		$(ID).find('h2').css('font-size',this.percent+'%');
		this.test();
	}
	this.test = function(){
		if($(ID).width()>480){
			this.minimize();
		}
	}
	this.test();
}


function showProductOptions(ID){
	var thisObj = this;
	this.sm = $(ID).find('.quantity');
	this.oc = $(ID).find('.productOptions');
	this.sm.change(function(e) {
		var q = e.target.value
		$(thisObj.oc).find('.grayWin').removeClass('opened');
		for(var i=0; i<q; i++)
		{
			$(thisObj.oc).find('.grayWin:eq('+i+')').addClass('opened');
		}
    });
}



function addNotification(target,text,type){
	if(type == 'err'){className = 'negative-ntf';}
	if(type == 'ok'){className = 'positive-ntf';}
	if(type == 'normal'){className = '';}
	
	$('.ntf').remove();
	
	$(target).after('<div class="ntf '+className+'">'+text+'</div>');
	$('.ntf').slideDown();
		
	$('.ntf').click(function(e) {
		$(this).slideUp("slow",function(){
			$(this).remove();
		});
	});
}






function radioTabs(ID)
{
	var thisObj = this
	this.obj = ID;
	
	$(this.obj).find('.radioNav:eq(0) input').change(function(e) {
		$(thisObj.obj).find('.radioContent:eq(0),.radioContent:eq(1)').removeClass('selected');
		$(thisObj.obj).find('.radioContent:eq('+$(this).parent().index()+')').addClass('selected');
    });
}

function accordion(ID){
	$(ID).find('.item').click(function(e) {
		$(ID).find('.item').removeClass('opened');
        $(this).addClass('opened');
		$(ID).find('.item').find('.content').slideUp('fast');
		$(this).find('.content').slideDown('fast');
    });
}

function updateWinPos(ID){
	this.obj = ID;
	$(document).scroll(function(e) {
		var newPos = $(document).scrollTop()-$(ID).parent().position().top+20;
		if(newPos<0){ newPos = 0; }
		$(ID).stop();
		$(ID).animate({marginTop: newPos+'px'}, 300);
    });
}

function updateCharStatus(ID){
	var thisObj = this
	this.obj = ID;
	$(this.obj).find('.textInput').keyup(function () {
		thisObj.updateStatus();
    });
	this.updateStatus = function () {
		var remainChar = 500 - $(this.obj).find('.textInput').val().length;
		if (remainChar <= 0) {
			$(this.obj).find('.val').addClass('red');
		} else {
			$(this.obj).find('.val').removeClass('red');
		}
		$(this.obj).find('.val').html(remainChar);
	}
}

function inputOptions(ID)
{
	var thisObj = this;
	this.obj = ID;
	$(this.obj).find('.option').click(function(e) {
        $(thisObj.obj).find('.option').removeClass('selected');
		$(this).addClass('selected');
    });
}

/*
function loginDrop()
{
	var thisObj = this;
	this.isOpened = false;
	$('#masterLoginButton').click(function(e) {
        thisObj.toggle();
    });
	
	this.toggle = function(){
		if(this.isOpened){
			$('#loginWindow').slideUp('fast');
			$('#masterLoginButton').removeClass('opened');
			this.isOpened = false;
		} else {
			$('#loginWindow').slideDown('fast');
			$('#masterLoginButton').addClass('opened');
			this.isOpened = true;	
		}
	}
	$('#loginWindow, #masterLoginButton').mouseup(function(e) {
        return false;
    });
	$(document).mouseup(function(e) {
		if(thisObj.isOpened){
			thisObj.toggle(thisObj.droppedObj);
		}
	});
}
*/






function productGallery(obj)
{
	var thisObj = this;
	this.obj = obj
	this.currentIndex = 0;
	this.length = $(this.obj).find('.navigation li').length;
	this.interval = setInterval( function(){ thisObj.next(); }, 3500);
	
	$(this.obj).find('.navigation li').click(function(e) {
        thisObj.toggle($(this).index());
		clearInterval(thisObj.interval);
    });
	
	this.toggle = function(ID){
		if(this.currentIndex!=ID){
			$('.navigation li:eq('+this.currentIndex+')').removeClass('selected');
			$('.navigation li:eq('+ID+')').addClass('selected');
			$('.galleryContainer li:eq('+this.currentIndex+')').fadeOut();
			$('.galleryContainer li:eq('+ID+')').fadeIn();
			this.currentIndex = ID;
		}
	}
	this.next = function(){
		if(this.currentIndex<this.length-1){
			this.toggle(this.currentIndex+1);
		} else {
			this.toggle(0);
		}
	}
}


