﻿
jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

function initHorizontal() {
	$(window).mousewheel(function (event, delta) {
		event.preventDefault();
		var scroll = $(window).scrollLeft();
		$(window).scrollLeft(scroll - (delta * 30));
	});
}

function initScrollWidth() {
	var width = 210;
	$('.section').each(function () {
		width += $(this).outerWidth( true );
	});
	$('body').css('width', width);
}

function openPopup() {
	$('#message-atolye').fadeIn();
	$('#overlay2').fadeIn();
}


$(document).ready(function () {
	
	openPopup();
	
	$('#popupClose').click(function(){
		$('#message-atolye').fadeOut();
		$('#overlay2').fadeOut();
	});
	
	var Localization = $('body').attr('class');
	
	$(window).load(function () {
		initScrollWidth();
		// ScrollPane
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		$('.section-team .content, .section-map .content, .section-contact .content, #date-picker-content .entry').jScrollPane({showArrows: false});
		}else{
			$('.section-team .content, .section-map .content, .section-contact .content').jScrollPane({showArrows: true});
		}
	});
	
	// [ REMOVED ] vertical scrolling
	//initHorizontal();
	// vertical scrolling for IE
	//if (!$.browser.msie) {
	//	$('body').addClass("msie");
	//	scrollTips.init();
	//}
	
	// scroll the whole window
	$('nav ul li a').click(function(){
		$.scrollTo( this.hash, 500 );
		return false;
	});
	
	// External Links
	$("a[href^='http']").each(function(){
		if(this.href.indexOf(location.hostname) == -1) { 
			$(this).attr('target', '_blank').addClass('external');
		}
	})
	
	// jQuery Lightbox
	var apopupheight;
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		apopupheight: "1024";
	}else{
		apopupheight: "850";
	}
	
	$("a.popup").colorbox({
		width:"750", 
		height: apopupheight,
		//onComplete:function(){ 
			//if(!$.browser.msie && !$.browser.version <= 7) {
			//	Cufon.replace('#cboxTitle');
			//}
		//})
	});
	
	$('#warning-badge-action').addClass("active-" + ($.randomBetween(1, 5)));
	
	// Subscribe Form
	$('.subscribe-form form').submit(function(e) {
		var fForm = $('.subscribe-form form');
		if(fForm.find('input[name=dateOfBirth_dd]').val() == 'Gün') {
			fForm.find('input[name=dateOfBirth_dd]').val('');
		}
		if(fForm.find('input[name=dateOfBirth_mm]').val() == 'Ay') {
			fForm.find('input[name=dateOfBirth_mm]').val('');
		}
		if(fForm.find('input[name=dateOfBirth_yyyy]').val() == 'Yıl') {
			fForm.find('input[name=dateOfBirth_yyyy]').val('');
		}
		$.post(fForm.attr('action'), fForm.serialize(), function(returnData) {
			var oReturn = $.parseJSON(returnData);
			if(oReturn.status == true) { 
				$("#form-messages .success-meassage span").html(oReturn.message);
				$("#form-messages").removeClass().addClass("submit-success").show();
			}
			else {
				$("#form-messages .error-meassage span").html(oReturn.message);
				$("#form-messages").removeClass().addClass("submit-error").show();
			}
		});
		e.preventDefault();
	});
	
	$('.close-form-error').click(function () {
        $("#form-messages").removeClass().hide();
        return false;
    });
	
    // select .flowplanes and make it scrollable. use circular and navigator plugins
    $(".section-place .flowpanes").scrollable({ circular: true, mousewheel: false }).navigator({
        naviItem: 'a',
        activeClass: 'current',
        history: false
    });
	
	$(".section-gallery .flowpanes").scrollable({ circular: false, mousewheel: false }).navigator({
        naviItem: 'a',
        activeClass: 'current',
        history: false
    });
	
	// Calendar
    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();
	// Set default names
	var monthNamesTr = ['OCAK', 'ŞUBAT', 'MART', 'NİSAN', 'MAYIS', 'HAZİRAN', 'TEMMUZ', 'AĞUSTOS', 'EYLÜL', 'EKİM', 'KASIM', 'ARALIK'];
	var dayNamesShortTr = ['pz', 'pzt', 'sal', 'çrş', 'prş', 'cum', 'cts'];
	
	if( Localization === "en_US" ) {
		monthNamesTr = ['January','February','March','April','May','June','July','August','September','October','November','December'];
		dayNamesShortTr = ['sun','mon','tue','wed','thu','fri','sat'];
	}
    
    $('#calendar').fullCalendar({
        titleFormat: {
			month: "MMMM yyyy"
		},
		firstDay: 1,
		monthNames: monthNamesTr,
		dayNamesShort: dayNamesShortTr,
		events: "index/eventlist?locale=" + Localization,
		eventRender: function (event, element, view) {
            if (event.start.getMonth() != view.start.getMonth())
                return false;
        },
        eventClick: function (calEvent, jsEvent, view) {
			$.get('index/event?locale=' + Localization + '&ID=' + calEvent.id, function(data) {
				$('#date-picker-content').html(data);
			});
			
            return false;
        }
    });

    $('#calendarSec').fullCalendar({
        titleFormat: {
			month: "MMMM yyyy"
		},
		firstDay: 1,
		month: date.getMonth() + 1,
		monthNames: monthNamesTr,
		dayNamesShort: dayNamesShortTr,
		events: "index/eventlist?locale=" + Localization,
		eventRender: function (event, element, view) {
            if (event.start.getMonth() != view.start.getMonth())
                return false;
        },
        eventClick: function (calEvent, jsEvent, view) {
			$.get('index/event?locale=' + Localization + '&ID=' + calEvent.id, function(data) {
				$('#date-picker-content').html(data);
			});
			
            return false;
        }
    });
	
	// Warning Badge Func
    $('#warning-badge-action').click(function () {
		//if(!$.browser.msie && !$.browser.version <= 7) {
			//Cufon.replace('#message-box .message-entry');
	//	}
		$("#overlay").fadeIn("fast");
		$("#message-box").fadeIn("fast");
        return false;
    });
	
	// Message Box Func
    $('#message-box').click(function () {
        $("#overlay").fadeOut("fast");
		$(this).toggle();
        return false;
    });
    
    $('#message-box-tr').click(function () {
        $("#overlay").fadeOut("fast");
		$(this).toggle();
        return false;
    });
	
    $('#overlay').click(function () {
        $("#message-box").toggle();
		$(this).fadeOut("fast");
    });
	
	// Popup Box
    $('.post-info-details').click(function () {
		//if(!$.browser.msie && !$.browser.version <= 7) {
			//Cufon.replace('.entry-content div h4, .entry-content div p');
		//}
		$$ = $(this).closest(".content").toggleClass("extented-content");
        $$$ = $$.find(".entry");
        $$$.hide().removeClass("visuallyhidden").fadeIn("fast");
		
		
		
		if((navigator.userAgent.match(/iPhone/i))) {
		$$$.find(".entry-content").jScrollPane({showArrows: false});
		}else{
		$$$.find(".entry-content").jScrollPane({showArrows: true});
		}
        return false;
    });

    $('.close-entry').live('click', function () {
        $(this).closest(".content").toggleClass("extented-content");
        $(this).parent().addClass("visuallyhidden");
        return false;
    });

    $('#subscribe-form-link').click(function () {
		//if(!$.browser.msie && !$.browser.version <= 7) {
		//	Cufon.replace('#subscribe-form p, #subscribe-form label, #subscribe-form button, #subscribe-form .error-meassage, #subscribe-form .success-meassage');
		//}
		$("#subscribe-form").slideToggle();
        return false;
    });
	
	$('.close-form').click(function () {
        $("#subscribe-form").slideToggle();
        return false;
    });
	
	$("ul.thumbs li:nth-child(3n)").addClass("last-item");
	
	//if(!$.browser.msie && !$.browser.version <= 7) {
		//Cufon.replace('.fc-header-title, .fc-state-default, .fc-day-number, .header .description, ul.thumbs .figcaption, #place p, #team ul li, #map p, #map li, #contact dd, #contact h4, #contact li');
	//}
	
	$(document).ajaxSuccess(function() {  
		//if(!$.browser.msie && !$.browser.version <= 7) {
			//Cufon.replace('#calendar .fc-event-title, #calendarSec .fc-event-title, #date-picker-content h3, #date-picker-content p, #date-picker-content dt, #date-picker-content dd, #cboxTitle');
	//	}
		
		setTimeout( // jScrollPane needs a time to relax :) özgür
			function() {
			if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
		$('.section-team .content, .section-map .content, .section-contact .content, #date-picker-content .entry, .entry-content').jScrollPane({showArrows: false});
		}else{
			$('.section-team .content, .section-map .content, .section-contact .content, #date-picker-content .entry, .entry-content').jScrollPane({showArrows: true});
		}	
			},
			200
		);
	});

	
});
