function init_more_links(parent_elm){
  $(parent_elm).find('a.more_link').click(function(){
    var more_link = $(this);
    var more_dots = $(parent_elm).find('#'+more_link.attr('rel')+'_dots');
    var more_content = $(parent_elm).find('#'+more_link.attr('rel'));
    if(more_content.length > 0){
      var shown = more_content.css('display') != 'none';
      if(shown){
        more_dots.show();
        more_content.hide();
        more_link.removeClass('less').addClass('more').html('více');
      }else{
          more_dots.hide();
          more_content.show();
          more_link.removeClass('more').addClass('less').html('méně');
      }
      return false;
    }
  });
}
$(function() {
    $(".poiItem").hover(function(){
		leftPos =  ($(this).prevAll().length - 1) * ($(this).width()+5);
		$(this).find("span").fadeIn(250);
		$(this).find("p").fadeIn(250);
		$(this).find("p").css({left: -leftPos})
	}, function () {
		$(this).find("span").css("display","none");
		$(this).find("p").css("display","none");
	});
});

$(function() {
    $("#tabs.photos").tabs({
        fx: {opacity: 'toggle'}
    });

    $(".offerDetail tbody tr:visible:even").addClass("even");
   	$(".offerDetail tr:visible:odd").addClass("odd");
    
	init_more_links(".offerDetail .cont");
	
	$('#printOfferButton').click(function() {
		window.print();
		$.ajax({
            url: '/ajax/offers/?ajaxAction=afterPrint&id=' + $(this).attr('rel'),
            async: false
        });
		return false;
	});

    try {
        var photoGalleryApi = $("#photoGallery").scrollable({
            hoverClass: 'hover',
            size: 3,
            speed: 600,
            keyboard: false,
            api: true
        });
        if (typeof(photoGalleryApi) != 'undefined') {
            photoGalleryApi.click(0);
            $("#photoGallery").mousewheel();
			if ($("#photoGallery a.smallImg").length == 3){
				$('.imageScroller a.nextPage').addClass('disabled');
			}
        }

        $("#photoGallery a.smallImg").click(function(){
            $("#photoGallery a.smallImg").removeClass('active');
            $(this).addClass('active');
            var imageHref = $(this).attr('href');
            var imageTitle = $(this).find('img').attr('alt');
            $("a.photosShowBig img").attr('src', imageHref).attr('alt', imageTitle);
            $("a.photosShowBig").attr('href', imageHref.replace('/large/', '/xlarge/')).attr('alt', imageTitle);
            return false;
        });

        $("a.photosShowBig").click(function(){
            var bigImagesArr = new Array();
            $("#photoGallery a.smallImg").each(function(i, elm) {
                obj = {
                    'href': $(elm).attr('href').replace('/large/', '/xlarge/'),
                    'title': $(elm).find('img').attr('alt'),
                    'selected': $(elm).hasClass('active'),
                    titlePosition: 'inside',
                    beforeNext: function(selectedArray, selectedIndex, selectedOpts){
                        if (typeof(photoGalleryApi) != 'undefined') {
                            photoGalleryApi.click(selectedIndex+1);
                            $("#photoGallery a.smallImg.active").trigger('click');
                        }
                    },
                    beforePrev: function(selectedArray, selectedIndex, selectedOpts){
                        if (typeof(photoGalleryApi) != 'undefined') {
                            photoGalleryApi.click(selectedIndex-1);
                            $("#photoGallery a.smallImg.active").trigger('click');
                        }
                    }
                };
                bigImagesArr.push(obj);
            });
            $.fancybox(bigImagesArr);
            return false;
        });
    } catch (e) {}



    var offerDetailDialogs = {
        settings: {
            btnReportProblem: $('#reportProblem'),
            btnSendDetail: $('#sendDetail'),
            btnMonitorPrice: $('#monitorPrice'),
            
            ajaxTimeout: 30,//time out for ajax request
            xmlContentElement: 'data',//element in the response which holds the HTML
            xmlResultElement: 'result'
        },

        init: function() {
            try {
                /*if (typeof(dialog) == 'undefined') {
                    return false;
                }*/

                offerDetailDialogs.settings.btnReportProblem.click(function() {
                    offerDetailDialogs.loadDialog('/cs/ajax/offers/','ajaxAction=getFormProblem&request_uri=' + window.location.href);
                    return false;
                });
                offerDetailDialogs.settings.btnSendDetail.click(function(){
                    offerDetailDialogs.loadDialog('/cs/ajax/offers/','ajaxAction=getFormSend&id=' + $(this).attr('rel') + '&request_uri=' + window.location.href);
                    return false;
                });
                offerDetailDialogs.settings.btnMonitorPrice.click(function(){
                    offerDetailDialogs.loadDialog('/cs/ajax/offers/','ajaxAction=getFormPrice&id=' + $(this).attr('rel') + '&request_uri=' + window.location.href);
                    return false;
                });
            } catch(e) {}
        },
        loadDialog: function(url, params) {
            try {
                $.fancybox.showActivity();
				
                $.ajax({
                    type: 'GET',
                    timeout: offerDetailDialogs.settings.ajaxTimeout,
                    url: url,
                    data: params,
                    async: false,
                    dataType: 'xml',
                    success: function(response) {
                        if($(response).find(offerDetailDialogs.settings.xmlResultElement).length < 0) {
                            offerDetailDialogs.closeDialog();
                            return false;
                        } else if($(response).find(offerDetailDialogs.settings.xmlResultElement).text() != '1') {
                            offerDetailDialogs.closeDialog();
                            return false;
                        }
                        if($(response).find(offerDetailDialogs.settings.xmlContentElement).length < 0) {
                            return false;
                        } else {
                            resultData = $(response).find(offerDetailDialogs.settings.xmlContentElement).text();
                            offerDetailDialogs.openDialog(resultData);
                        }
                    },
                    error: function(err){
                        if ((err.status != 200) && (typeof(err.statusText) != 'undefined') && (err.statusText != '')) {
                           alert(err.statusText);
                           offerDetailDialogs.closeDialog();
                        }
					}
                });
            } catch(e) {
                //alert(e.message);
                offerDetailDialogs.closeDialog();
                //return false;
            }
        },
        openDialog: function(data) {
			try {
				$('#fancybox-inner form').unbind('submit');
				fancyboxOptions = {
					padding: 0,
					onComplete: function() {
						$('#fancybox-inner form').submit(offerDetailDialogs.submitDialogForm);
					}
				}
				$.fancybox(data, fancyboxOptions);
			} catch(e){}
        },
        closeDialog: function(dialogElm) {
            $.fancybox.close();
        },
        submitDialogForm: function() {
            try {
                url = $(this).attr('action');
                if (url == '') {
                    return false;
                }
                var formVars = {};
                $(this).find("input:hidden, input:text, input:checked, input:password, option:selected, textarea, input:submit, button").each(function() {
                    if($(this).attr('name')) {//for all common fields
                        formVars[$(this).attr('name')] = $(this).val();
					} else if($(this).parent().attr('name')) {//for <option>
                        formVars[$(this).parent().attr('name')] = $(this).val();
					}
                });

				$.fancybox.showActivity();

				$.ajax({
                    type: 'GET',
                    timeout: offerDetailDialogs.settings.ajaxTimeout,
                    url: url,
                    data: formVars,
                    async: false,
                    dataType: 'xml',
                    success: function(response) {
                        if($(response).find(offerDetailDialogs.settings.xmlResultElement).length < 0) {
                            offerDetailDialogs.closeDialog();
                            return false;
                        } else if($(response).find(offerDetailDialogs.settings.xmlResultElement).text() == '1') {
                            offerDetailDialogs.closeDialog();
                            return true;
                        }
                        if($(response).find(offerDetailDialogs.settings.xmlContentElement).length < 0) {
                            offerDetailDialogs.closeDialog();
                            return false;
                        } else {
                            resultData = $(response).find(offerDetailDialogs.settings.xmlContentElement).text();
                            offerDetailDialogs.openDialog(resultData);
                        }
                    },
                    error: function(err){
                        if ((err.status != 200) && (typeof(err.statusText) != 'undefined') && (err.statusText != '')) {
                           alert(err.statusText);
                           offerDetailDialogs.closeDialog();
                        }
                    }
                });
            } catch(e) {
                //alert(e.message);
                offerDetailDialogs.closeDialog();
            }
            return false;//dont't allow the real submit
        }
    }
    offerDetailDialogs.init();

});


/**
 * Contact form "flashing"
 */
$(function(){
	$('#sendContactForm').click(function(){							 
		$('#question').css({backgroundColor: '#E9F1F4'});
		$('#question input:first').focus();
		$('#question').animate({backgroundColor: '#FFF5DA'}, 500, function(){
			$('#question input:first').focus();
			$(this).animate({backgroundColor: '#E9F1F4'}, 500, function(){
				$(this).animate({backgroundColor: '#FFF5DA'}, 500, function(){})														
			})
		})					
	})
});

/**
 * Refresh captcha image on registration page.
 */
$(function(){
   $('a#refresh_captcha').click(function(){
    	formVars = {};
        formVars['random'] = new Date().getTime();
        ajaxUrl = '/ajax/offerformcaptcha/';
        if (typeof(languageCode) != 'undefined')
            ajaxUrl = '/' + languageCode + ajaxUrl;
    	$.ajax({
            type: 'POST',
            timeout: 30,
            url: ajaxUrl,
            data: formVars,
            async: false,
            dataType: 'xml',
            success: function(xmlResponse){
                if ($('response', xmlResponse).length <= 0) {
                    return false;
                } else {
                    if ($('imageUrl', xmlResponse).length > 0) {
                       $('#captcha_image').attr('src', '/data/captcha/' + $('imageUrl', xmlResponse).text() + '.png');
                    }
                }
            },
            error: function(err){
                if ((err.status != 200) && (typeof(err.statusText) != 'undefined') && (err.statusText != '')) {
                   alert(err.statusText);
                }
                return false;
            }
        });
    	return false;
    });
});
