// Notification system
Notificate = {
    error: function(message, timeout) {
        if (typeof message == 'undefined') {
            message = "System error!";
        }
        
        if (typeof timeout == 'undefined') {
            timeout = 0;
        }
    
        $.achtung({
            timeout: timeout,
            message: message,
            className: 'achtungFail',
            icon: 'ui-icon-alert'
        });
    },
    success: function(message, timeout) {
        if (typeof message == 'undefined') {
            message = "Operation completed!";
        }
        
        if (typeof timeout == 'undefined') {
            timeout = 5;
        }
    
        $.achtung({
            timeout: timeout,
            message: message,
            className: 'achtungSuccess',
            icon: 'ui-icon-check'
        });
    },
    info: function(message, timeout) {
        if (typeof message == 'undefined') {
            return;
        }
        
        if (typeof timeout == 'undefined') {
            timeout = 10;
        }
    
        $.achtung({
            timeout: timeout,
            message: message,
            className: 'achtungInfo',
            icon: 'ui-icon-info'
        });
    },
    custom: function(message, timeout, settings) {
        if (typeof message == 'undefined') {
            message = "Notification";
        }
        
        if (typeof timeout == 'undefined') {
            timeout = 5;
        }
    
        settings = $.extend({
            timeout: timeout,
            message: message            
        }, settings);
        
        $.achtung(settings);
    }
};

// Setup ajax environment
jQuery.ajaxSetup({
    cache: true,
    error: function() {this.reportRequestError();},
    success: function(data) {
        if (this.dataType != 'json') {
            return;
        }
        
        if (data && data.result) {
            this.onSuccess();
        } else if (data && data.result != undefined && !data.result) {
            this.reportRequestError(data.message);
        }

        if (data.blocks) {
            for (var id in data.blocks) {
                $('#' + id + '-block').html(data.blocks[id]);
            }
        }
    },
    onSuccess: function() {
        this.reportRequestSuccess();
    },
    reportRequestError: function(message) {
        if (typeof message == 'undefined') {
            message = "Request failed. Please, try again later!";
        }

        Notificate.error(message);
    },
    reportRequestSuccess: function(message) {
        if (typeof message == 'undefined') {
            message = "Succeed!";
        }

        Notificate.success(message);
    }
});

function bookmarksite(title,url){
    if (window.sidebar){ // firefox
    	window.sidebar.addPanel(title, url, "");
    }
    else if(window.opera && window.print){ // opera
    	var elem = document.createElement('a');
    	elem.setAttribute('href',url);
    	elem.setAttribute('title',title);
    	elem.setAttribute('rel','sidebar');
    	elem.click();
    }
    else if(document.all){// ie
    	window.external.AddFavorite(url, title);
    }
    return false;
}

// star rating voting
(function($) {
    $('.star-rating a').live('click', function() {
    	var me = this;
    	var curRating = $(me).parents('.star-rating').find('.current-rating');
    	$.ajax({
    		url: $(this).attr('href'),
    		cache: false,
    		dataType: 'json',
    		error: function() {
    			alert('Network problems!!!');
    		},
    		success: function(data) {
    			if (data.result) {
    				curRating.width(data.width + '%');
    			} else{
    				alert('You have already rated this');
    				$(me).parents('.star-rating').css('z-index', '1');
    				$(me).parents('.inline-rating').attr('id', 'idid');
    			}
    		},
    		complete: function() {
    		    $(me).parents('.star-rating').css('z-index', '1');
    			$(me).parents('.star-rating').find('a').remove();
                $('#presentation-rating').attr('id', 'idid');
    		}
    	});
    	
    	return false;
    });
})(jQuery);

// admin menu
jQuery(function($) {
	$('#mainMenu .admin-menu-off a').click(function() {
		$('#admin-menu').hide();
		$('#main-menu').show();
		
		return false;
	});

	$('#mainMenu .admin-menu-on a').click(function() {
		$('#admin-menu').show();
		$('#main-menu').hide();
		
		return false;
	});
});

// FLOATING BAR
jQuery(function($) {
	if ($('#floatingBar').length) {
		$('#floatingBar .tabs a').live('click', function() {
			var $content = $($(this).attr('href'));
			
			if ($('#floatingBar .tab-content').is(':hidden')) {
				$content.show();
				$('#floatingBar').css('width','auto');
				//$('#floatingBar .tabs a').not($(this)).css('opacity', 0.5);
				$('#floatingBar .tabs a').not($(this)).toggleClass('transparent');
				$('#floatingBar .tab-content').animate({"width": "show"}, 'fast');
			} else if($content.is(':hidden')) {
				//$('#floatingBar .tabs a').not($(this)).css('opacity', 0.5);
				$('#floatingBar .tabs a').not($(this)).toggleClass('transparent');
				//$(this).css('opacity', 1);
				$(this).removeClass('transparent');
				$('#floatingBar .fbcontent:visible').fadeOut('fast', function() {
                    $('.fb-success-submit', this).hide();
                    $('.fbcontent-main', this).show();
				});
				$content.fadeIn('fast');
			} else {
				$('#floatingBar .tab-content').animate({"width": "hide"}, 'fast', function() {
					$('#floatingBar').css('width','22px');
					//$('#floatingBar .tabs a').css('opacity', 1);
					$('#floatingBar .tabs a').removeClass('transparent');
					$content.hide();
                    $('.fb-success-submit', $content).hide();
				    $('.fbcontent-main', $content).show();
				});
			}

			return false;
		});

		$('#floatingBar .fbclose a').live('click', function() {
			$('#floatingBar').css('width','22px');
			var $content = $('#floatingBar .fbcontent:visible');

			$('#floatingBar .tab-content').animate({"width": "hide"}, 'fast', function() {
				//$('#floatingBar .tabs a').css('opacity', 1);
				$('#floatingBar .tabs a').removeClass('transparent');
				$content.hide();
                $('.fb-success-submit', $content).hide();
                $('.fbcontent-main', $content).show();
			});

			return false;
		});

		$('#floatingBar .container a.submit').live('click', function() {
			var form = $(this).parents('form');
			form.ajaxSubmit({
				success: function() {
					var fbcontent = form.parents('.fbcontent');
					$('.fbcontent-main', fbcontent).fadeOut('fast');
					$('.fb-success-submit', fbcontent).fadeIn('fast');
				}
			});

			return false;
		});
	}
});

// USER BAR
jQuery(function($) {
	$('#mainLinks_tabs li.first a').live("click", function() {
		if ($('#userBar').is(':hidden')) {
			$('#userBar').slideDown('fast');
		} else {
			$('#userBar').slideUp('fast');
		}

		return false;
	});
	$('#userBar #close').live("click", function() {
		if ($('#userBar').is(':hidden')) {
			$('#userBar').slideDown('fast');
		} else {
			$('#userBar').slideUp('fast');
		}

		return false;
	});

    $('#userBar .loginbar a.submit').click(function() {
        $(this).parents('form').submit();

        return false;
    });
    
    $('#remind-me-pass').click(function() {
        $('#userBar .loginbar').hide();
        $('#userBar .remindbar').show();

        return false;
    });
    
    $('#i-remember-pass').click(function() {
        $('#userBar .remindbar').hide();
        $('#userBar .loginbar').show();

        return false;
    });

    $('#userBar .remindbar a.submit, #userBar .remindbar a.submit').click(function() {
        var form = $(this).parents('form');
        
        $(this).parents('form').ajaxSubmit({
            dataType: 'json',
            success: function(data) {
                $('#userBar .remindbar').text(data);
            }
        });

        return false;
    });
    $('#login_in_page .remindbar a.submit').click(function() {
        var form = $(this).parents('form');
        
        $(this).parents('form').ajaxSubmit({
            dataType: 'json',
            success: function(data) {
                $('#login_in_page .forgot_box').text(data);
            }
        });

        return false;
    });
    $('#login_in_page a.forgot_password_link').click(function() {
    	$(this).hide();
    	$('#login_in_page .forgot_box').show();
    	return false;
    });
    $('#login_in_page #i-remember-pass').click(function() {
    	$('#login_in_page .forgot_box').hide();
    	$('#login_in_page a.forgot_password_link').show();
    	return false;
    });
    
});

// AJAX-FORM
jQuery(function($) {
    function initForm(hash) {
        var width = $(hash.w).width();
        $(hash.w).css('margin-left', -Math.round(width/2) + "px");
        $(hash.w).css('width', width + "px");
        
        $('form', hash.w).ajaxForm({
            method: 'POST',
            dataType: 'json',
            onSuccess: function (data, form) {
                $(hash.w).jqmHide();
                if ($('form', hash.w).attr('class') == 'enableChannels') {
                    location.reload(true);
                }
            }
        });
    }

    function onHide(hash) {
        $(hash.w).hide();
        $(hash.o).remove();
    }

    function hideForm() {
        $('#ajax-form').jqmHide();
        return false;
    }

    if ($('#ajax-form').length == 0) {
        $('<div></div>').attr('id', 'ajax-form').appendTo('body');
    }

    $('#ajax-form').jqm({
        modal: true,
        ajax: "@href",
        ajaxText: 'Loading...',
        toTop: true,
        onLoad: initForm,
        onHide: onHide
    });

    $('#ajax-form .close-button').live('click', hideForm);
    $('#ajax-form button.cancel').live('click', hideForm);
    $('#ajax-form .cancel').live('click', hideForm);
    $(".jqmOverlay").live('click',hideForm);
    
    $('a.action-ajax-form').live('click', function() {
        $('#ajax-form').jqmShow(this);
        return false;
    });
   
    $('#ajax-form .content a.submit').live('click', function() {
		var form = $(this).parents('form');
		
		form.ajaxSubmit({
			success: function() {
				var fbcontent = form.parents('.fbcontent');
				$('.fbcontent-main', fbcontent).fadeOut('fast');
				$('.fb-success-submit', fbcontent).fadeIn('fast');
				setTimeout('$("#ajax-form").jqmHide();', 2000);
				if ($(form).attr('class')=='business_card_edit_form') {
					location.reload(true);
				}
				
			}
		});

		return false;
	});
    $('#ajax-form a.submit').live('click', function() {
		var form = $(this).parents('form');
		form.ajaxSubmit({
			success: function() {
				var fbcontent = form.parents('.fbcontent');
				
				$('.fbcontent-main', fbcontent).fadeOut('fast');
				$('.fb-success-submit', fbcontent).fadeIn('fast');
				setTimeout('$("#ajax-form").jqmHide();', 2000);
				
			}
		});

		return false;
	});
    
});

//NOT_IMPLEMENTED LINKS
(function($) {
    
	function alertThem() {
        alert(not_implemented);
        return false;
    }
        
    $('a.coming-soon-link').live('click', alertThem);
    $('.coming-soon-links a').live('click', alertThem);
    
})(jQuery);

$(document).ready(function() {
    $("#mainLinks #languageSelector").msDropDown();
    $("#mainLinks #currencySelector").msDropDown();
});

// HOVER EVECT FOR PRESENTATION THUMB
(function($) {	
	$('img.presentation_play_button').live('mouseover', function() {
        $(this).attr("src","/images/presentation_avatar_hover2.png");
    });
	$('img.presentation_play_button').live('mouseout', function() {
        $(this).attr("src","/images/presentation_avatar2.png");
    });
	$('img.spotlight_play_button').live('mouseover', function() {
		$(this).attr("src","/images/spotlights_presentation_avatar_hover.png");
    });
	$('img.spotlight_play_button').live('mouseout', function() {
        $(this).attr("src","/images/spotlights_presentation_avatar.png");
    });	        
})(jQuery);

// AJAX AUTOMPLETE FOR GLOBAL SEARCH
$(function() {
    $("#mainMenu .search input, #mainFooter .search input").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "/search/ajax-search",
                dataType: "json",
                data: { query: request.term },
                success: function( data ) {
                    response( data );
                },
                type : "post"
            });
        },
        minLength: 3,
        close: function(event, ui) {
            if ($('#mainMenu .search input').val().length > 0) {
                $("#mainMenu form.search").submit();
            } else {
                $("#mainFooter form.search").submit();
            }
        }
    });
});

var LoginPopup = {
    callback: function(o) {},

    init: function() {
        var self = this;
        
        $('#login-popup-form-container').jqm({
            ajax: '/authentication/loginpopup',
            toTop: true,
            onLoad: function(h) {
                $('#login-popup-form').ajaxForm(function(o) {
                    if (o.result) {
                        self.callback(o);
                    } else {
                        $('#login-popup-form .login_field_element.login-popup-form-message').html(o.message);
                    }
                });
            }
        });
    },
    
    setCallback: function(callback) {
        if (typeof callback == 'function') {
            this.callback = callback;
        }
        
        return this;
    },
    
    show: function() {
        $('#login-popup-form-container').jqmShow();
    },
    
    hide: function() {
        $('#login-popup-form-container').jqmHide();
    }
};

// Login pop-up window
$(document).ready(function() {
    LoginPopup.init();
    Oauth.setCallback(function(o) {
        if (o.result && o.redirect) {
            document.location.href = o.redirect;
        }
    });
});
// restore pass form on login page
$(document).ready(function() {
    $('#pass-restore-form').submit(function() {
    var dataSend = 'email=' + $('#pass-restore-form input').val();
       $.ajax({
        type: "post",
        dataType: 'json',
        url: $('#pass-restore-form').attr('action'),
        data: dataSend,
        success: function(data) {
             $('div.loginform.remind-form').text(data);
        }
       });
       return false;
    });

});

function myFancybox(params) {
    var content = (params.content != null) ? params.content : ''; 
    var linkAdobe = (params.linkAdobe != null) ? params.linkAdobe : '';
    var swfObj = (params.swfObj != null) ? params.swfObj : '';
    var presentationId = (params.presentationId != null) ? params.presentationId : '';

    $.fancybox(
            '<div id="sp-fancy-player" style="background: url(/img/no_flash.png) no-repeat 0 0; width: 932px; height: 367px;"><p class="noflash">' + linkAdobe + '</p></div><div class="sp-fancy">' + content + '<a class="spf-close" onclick="$.fancybox.close()"/></a></div>',
            {
                'autoDimensions'     : false,
                'autoScale'          : false,
                'width'              : 932,
                'height'             : 400,
                'transitionIn'       : 'none',
                'transitionOut'      : 'none',
                'showCloseButton'    : false,
                'enableEscapeButton' : false,
                'hideOnContentClick' : false,
                'overlayColor'       : '#000000',
                'overlayOpacity'     : '0.8',
                'centerOnScroll'     : 'true',
                'onComplete'  : function() {
                    if (swfObj != '') { 
                        eval(swfObj.replace(/__ID__/, presentationId));
                    }
                }
    });
}
