/**
 * @fileoverview File with document.ready function
 *
 * @version 1.0
 * @author DHIMAHI
*/

$(document).ready(function() {

	// 'Allways load' functions

	//=> Search script
	globalQuickSearchBind();

	//=> Order Summary
	handleOrderSummary();

	//=> Check banners (if anyone is empty - hide him)
	checkBanners('#banners');
	checkBanners('#banner_grid_right_42');
	checkBanners('#banner_sidebar2');

	//=> Facebook
	if ($('body').data('loadfb')) {loadFacebookJS();}

	//=> External links in new window functionality
	//external_links();

	// Other functions

	if ($("#frm_choose").length > 0) {manageReserveButton();}

	if ($("#registerLoginForm").length > 0) {checkUserOldNew();}

	if($("#faddress-metro_station").length > 0) {

		var metros = "";
		$("#faddress-metro_station optgroup").each(function() {
			metros = metros+$(this).html();
		});

		$("#faddress-metro_station").html(metros);

		$("#faddress-city").blur(function() {
			manageMetroList($(this).val(), metros);
		});
	}

	if ($(".edit-address.collapse").length > 0) {
		if ($("#toggle-fields").attr('checked') == false) {
			toggleFullAddress('hide');
		}

		$("#toggle-fields").change(function() {
			toggleFullAddress('toggle');
		});
	}

	//=> Mega dropdown
	if ($("ul#main_nav li").length > 0) {
		$("ul#main_nav li").hoverIntent(mainmenu.over,mainmenu.out);
	}

	//=> Slideshow
	if ($("#overlay_slideshow").length > 0) {
		slideshow.init();
	}

	//=> Carousel
	if ($(".carousel").length > 0) {
		bindCarousel('.carousel ul#carousel', '#carousel_prev', '#carousel_next', 4, 'circular');
	}

	//=> Venue picture
	if ($(".venue_picture ul ul.venue_picture_carousel").length > 0) {
		bindVenueCarousel();
	}

	//=> Event gallery
	if ($(".event_gallery_carousel").length > 0) {
		bindEventGalleryCarousel();
	}

	//=> Category carousel
	if ($("#category_carousel").length > 0) {
		bindCategoryCarousel();
	}

	//=> Subnavigation menu
	if ($(".subnavigation-item").length > 0) {
		generateSubnavigation();
	}

	//=> Detail section
	if ($(".detail_sections").length > 0) {
		detailSection('.detail_sections','.detail_menu');
	}

	//=> Lightbox
	if ($(".event_gallery_carousel").length > 0) {
		$(".event_gallery_carousel .carousel_main ul li a").lightBox();
	}

	//=> Voucher toggle
	if ($("form#voucher-form fieldset").length > 0) {
		showhideVoucher();
	}

	//=> News tab
	if ($(".news-cancelations-wrapper").length > 0) {
		$(".news-cancelations-wrapper:has(.tabbed)").find('.news').click(function(){
			$(this).addClass('active').siblings('.cancelations').removeClass('active');
		});
	}

	//=> Cancelations tab
	if ($(".news-cancelations-wrapper").length > 0) {
		$('.news-cancelations-wrapper:has(.tabbed)').find('.cancelations').click( function(){
			$(this).addClass('active').siblings('.news').removeClass('active');
		});
	}

	//=> Add options
	if ($(".add-options").length > 0) {
		$('.add-options').click(function(){
			copy('possible','ticket_alarm');
		});
	}

	//=> Remove options
	if ($(".remove-options").length > 0) {
		$('.remove-options').click(function(){
			copy('ticket_alarm','possible');
		});
	}

	//=> Back to edit newsleter subscription page
	if ($(".edit-options").length > 0) {
		$('.edit-options').click(function(){
			window.location.href = 'default.html';
		});
	}

	// => New MOD/MOP/...
	if ($(".drawer-content").length > 0) {
		manageDrawers.init();
	}

	// Auto fill MOD address
	if ($(".drawer #existing-addresses").length > 0) {
		fillAddress();
	}

	//=> YES/NO ship address
	if ($(".edit-options").length > 0) {
		$("#diff_delivery_address input:radio").change(buyAddressFlowDeliveryTypeRefresh);
		$("#diff_delivery_address input:radio:checked").change();
	}

	//=> MOD - Methods of delivery
	if ($('#choose_mod').length > 0) {
		$("ul#choose input:radio").not('.subradio').change(buyAddressMODSelector);
		MODSlidingRadios();
	}

	//=> MOP
	if ($('#choose_mop').length > 0) {
		$("ul#choose input:radio").not('.subradio').change(buyAddressMOPSelector);
		MOPSlidingRadios();
	}

	// => Terms of use
	if ($('#termsOfUse').length > 0) {
		if (!$('#fpayment-terms_of_use').attr('checked')) {$('#termsOfUse').reveal();}
		manageTOU();}

	// Credit card gueser
	if ($('#creditcards-icons').length > 0) {
		var cards = new Array();
		var i;

        // In case of prefilled cardnumber find which card is entered if any
        if ($('#fpayment-cc_number').data('prefilled') > 0) {
            var cards = creditCardTypeFromNumber($('#fpayment-cc_number').val());
            $('#creditcards-icons li.opacity-10').removeClass('opacity-10').addClass('opacity-03');
            $('#creditcards-img').removeClass();
            prefillValue = $('#fpayment-cc_number').val();

        }

        for (i = 0; i < cards.length; i++) {
            $('#creditcards-icons .card-'+cards[i]).addClass('opacity-10');
            $('#creditcards-img').addClass('card-'+cards[i]);
        }

        // on change check which card alygorithm is matching
		$('#fpayment-cc_number').keyup(function() {
			var cards = creditCardTypeFromNumber($(this).val());
			$('#creditcards-icons li.opacity-10').removeClass('opacity-10').addClass('opacity-03');
			$('#creditcards-img').removeClass();

			for (i = 0; i < cards.length; i++) {
				$('#creditcards-icons .card-'+cards[i]).addClass('opacity-10');
				$('#creditcards-img').addClass('card-'+cards[i]);
			}
             preventCreditCardPrefillClearing(prefillValue);

		});
	}

	// Disable "Place order" after click
	if ($('.order-overview').length > 0) {
		$('.order-overview form').submit(function(){
		    // On submit disable its submit button
		    $('.order-overview form button[type=submit]').attr('disabled', true);
		});
	}


	//=> Errors in tell a friend
	if ($('#tell-a-friend .error').length > 0) {
		$('#tell-a-friend_menu').click();
	}

	//=> Buy Delivery Address
	if ($("#delivery_address input").length > 0) {
		$('#delivery-toggle li').click(function(event) {
			event.preventDefault();
			if ($('a', this).attr('href')) {
				href = $('a', this).attr('href');
				$('#delivery-toggle li').removeClass('active');
				$(this).addClass('active');
				$('.container_8').children(':gt(0)').hide();
				$('.container_8 '+href).show();

				if (href == '#enter_address') {
					$('#delivery_address input:first').change();
				} else {
					$('#select_delivery_address input:checked').click();
				}
			}
		});
		$('#delivery-toggle li.active a').click();
	}

	//=> Buy Delivery Address - new custom delivery/invoice address
	if ($("#delivery_address input").length > 0) {
		$('#delivery_address input, #delivery_address select').change(buyAddressFlowDeliveryNewRefresh);
		$('#delivery_address input:first').change();
	}
	//=> Buy Delivery Address - existing custom address
	if ($("#select_delivery_address input").length > 0) {
		$('#select_delivery_address input').click(buyAddressFlowDeliveryExistingRefresh);
		$('#select_delivery_address input:checked').click();
	}

	//=> Loader of module-based ajax
	$('a.simpleAjax').bind('click', {
		type: 'link'
	}, simpleAjaxLoadContent);

	if ($('#quicksearch').length > 0) {
		bindQuickSearch();
	}

	//=> Additional information on personalized ticketc
	if ($('.ticket-fields .more').length > 0) {
		personalizedAdditionalInfo();
	}

	//=> Personalized data on order overview
	if ($('.personification-data').length > 0) {
		showHidePersonalizedData();
	}


	/**
	 * Check this functions
	*/
	handleSeatmapPreviews();
    handleHelpSection();

	/**
	 * From search js
	*/
	var reg = new RegExp('^(http[s]?\://.*/).*', 'im');
	//var scriptLink = $('link[href$=global.css]').attr('href').match(reg)[1];
	var staticUrl = $('body').data('static');

	/*
	$("head").append(
		'<link rel="stylesheet" href="'+scriptLink+'jquery-ui.css" type="text/css" media="screen" />'
	);
	*/

	$('#search-form').parent().parent().append('<iframe id="hidden-iframe" class="jsHidden" src="'+staticUrl+'/static/html/blank.html"></iframe>');

	Hash.hash = window.location.hash;
	Hash.init(searchAjaxPerformCall, document.getElementById('hidden-iframe'));

	searchBindEvents(true);

	/* sets anchor default for language switcher */
	var anchor = window.location.search.substr(1);
	if ($('#lang_switcher').data('original_link') == null) {
		var curLink = $('#lang_switcher').attr('href');
		$('#lang_switcher').data('original_link',curLink);
	}
	$('#lang_switcher').attr('href', $('#lang_switcher').data('original_link') + '&' + anchor);

    /**
     * Loyalty program Service
     */
    if ($('#loyalty_service').length > 0) {
        handleLoyaltyProgramService();
    }

    // This is specail event on order overview
    if ($('#triggerPopUpWindow').length > 0) {
        eval($('#triggerPopUpWindow').attr('rel'));
    }

    //=> initialize tell a friend
    tellFriend();

	/**
	 * Allways LAST !!!
	*/

	/* introducing the all new and shiny - jsUnHide events! */
	/* required for browser history objects */
	$(document).trigger('onBeforeJsUnhide');

	//=> Modernizr
	if (typeof Modernizr == 'object') {
		$('head style').eq(1).remove();
	} else {
		$('head style').eq(0).remove();
	}

	$(document).trigger('onAfterJsUnhide');
});
