
var currentItem = 0;

function setupData () {
    $('#mobil').load('data/mobil.txt');
    $('#sabit').load('data/sabit.txt');
}

function setupContent () {
    $('#menu a').click(gotoPage);
    $('#content a').click(gotoPage);

    historyCallback('');
}

function gotoPage (e) {
    var contentPart = this.getAttribute('href').replace(/^.*\/#\//, '').replace(/[^a-z0-9_]/g, '');
    var jq = $('#content > div#' + contentPart);
    if (jq.length > 0) {
        $('#content > div').hide();
        jq.fadeIn();
    }
}

function historyCallback (hash) {
    var anchor = hash.replace(/[^a-z0-9_]/g, '');
    if (anchor == '') anchor = 'anasayfa';

    $('#content > div').hide();
    $('#content > div#' + anchor).show();
}

function setupNews () {
    $('#content #haberler div:last-child').addClass('last');
    $('#content #haberler div').clone(true).insertAfter('#news h1');

    $('#news div').hide();
    $('#news div:eq(' + currentItem + ')').show();

    $('#news a.nextArrow').click(showNextItem);
    $('#news a.prevArrow').click(showPrevItem);
}

function showItem () {
    $('#news div:eq(' + currentItem + ')').show(500);
    $('#news div:not(:eq(' + currentItem + '))').hide(500);
}

function showNextItem (e) {
    e.preventDefault();
    if (currentItem >= $('#news div').length - 1) return;
    currentItem++;
    showItem();
}

function showPrevItem (e) {
    e.preventDefault();
    if (currentItem <= 0) return;
    currentItem--;
    showItem();    
}

function setupQueryForm () {
    disableButton($('#queryNumber button'));
    $('#queryNumber input.text').focus(initInput);
    $('#queryNumber input.text').keydown(limitInput);
    $('#queryNumber input.text').keyup(checkInput);
}

function initInput (e) {
    this.value = '';
    $('#queryNumber input.text').unbind('focus');
}

function limitInput (e) {
    if (e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) e.preventDefault();
}

function checkInput (e) {
    this.value = this.value.replace(/[^0-9]/g, '');
    this.value = this.value.substr(0, 10);

    if (this.value.length == 10) {
        enableButton($('#queryNumber button'));
    } else {
        disableButton($('#queryNumber button'));
    }
}

function disableButton (jq) {
    jq.addClass('inactive');
    jq.bind('click', function (e) { e.preventDefault() });
}

function enableButton (jq) {
    jq.removeClass('inactive');
    jq.unbind('click');
}

Cufon.replace('h1', { fontFamily: 'OfficinaSansStd' });

$(document).ready(function(){
    setupContent();
    setupData();
	setupQueryForm();
	setupNews();
	$('#menu').autosprites({
	  overSpeed: 100,
	  outSpeed: 250
	});
	$.historyInit(historyCallback, 'index.html');
});
