(function($) {
    
    $.fn.TipFriend = function(options) {
        $.extend({}, options);
        return this.bind('click', function() {
            //display overlay
            $('.modalBackground').remove();
            $('<div class="modalBackground" />').css({
                position: 'fixed',
                Zindex: '10000'
            }).appendTo('#aspnetForm');
            $('.modalBackground').css('display','block');
            
            //remove old popup if it exists
            $('.jpopup').remove();
            
            //Create modal window and attach form to it
            $('#aspnetForm').append(
                $('<div class="jpopup" />').css({
                    position: 'fixed',
                    left: '50%',
                    marginLeft: -5 - 400 / 2,
                    zIndex: '100001'
                }).append(
                    $('<div class="jpopup-content"/>').append(
                        $('<a class="closebtn" title="' + closeTxt + '" href="#"><span>' + closeTxt + '</span></a>')
                    ).append(
                        $('<div id="tipfriend-wrapper"/>').append(
                            $('<h4/>').html(tipfriendTitle)
                        ).append(
                            $('<h1/>').html($('#contentarea h1').html())
                        ).append(
                            $('<label />').html(yourname + '<span class="mandatory">*</span>')
                        ).append(
                            $('<input type="text" />').attr({
                                name : 'yourname',
                                id : 'your-name'
                            })
                        ).append(
                            $('<label />').html(myMail + '<span class="mandatory">*</span>')
                        ).append(
                            $('<input type="text" />').attr({
                                name : 'youremail',
                                id : 'your-email'
                            })
                        ).append(
                            $('<label />').html(friendMail + '<span class="mandatory">*</span>')
                        )
                        .append(
                            $('<input type="text" />').attr({ 
                                name : 'friendemail',
                                id : 'friend-email'
                            })
                        ).append(
                            $('<label />').html(message)
                        ).append(
                            $('<textarea name="message" rows="3" />')
                        ).append(
                            $('<button type="submit" id="tip-a-friend">' + sendTxt + '</button>')
                        ).append(
                            $('<p class="infotext"/>').html(infotext + ' (<span class="mandatory">*</span>=' + mandatory + ')</span>')
                        )
                    )
                )
            );
            //position vertically
            var topPos;
            
            var bHeight = (document.documentElement.clientHeight || document.body.clientHeight);
            topPos = ((bHeight/2) - ($('.jpopup').height()/2));
            topPos = topPos + 'px';
        
            $('.jpopup').css('top',topPos);
            
            
           $('#tip-a-friend').SubmitTipFriend();
           $('.closebtn').CloseTipFriend();
            return false;
            
            
        });
    }
    
    $.fn.SubmitTipFriend = function(options) {
        $.extend({}, options);
        return this.bind('click', function() { 
            //test email validation 
			var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
            var formValid = true;
            if(pattern.test($('#friend-email').val())){         
	           $('#friend-email').removeClass("error");
	           $('#friend-email').prev().removeClass("error");
	           $('#friend-email').prev().html(friendMail + '<span class="mandatory">*</span>');
            }
            else {
                $('#friend-email').addClass("error");               
                $('#friend-email').prev().addClass("error");
                $('#friend-email').prev().html(friendMail + '<span class="mandatory">*</span>' + ' (' + mailFailed + ')');
                formValid = false;
            }
            if(pattern.test($('#your-email').val())){         
	           $('#your-email').removeClass("error");
	           $('#your-email').prev().removeClass("error");
	           $('#your-email').prev().html(myMail + '<span class="mandatory">*</span>');
            }
            else {
                $('#your-email').addClass("error");               
                $('#your-email').prev().addClass("error");
                $('#your-email').prev().html(myMail + '<span class="mandatory">*</span>' + ' (' + mailFailed + ')');
                formValid = false;
            }
            if($('#your-name').val() != "") {
                $('#your-name').removeClass("error");
                $('#your-name').prev().removeClass("error");
                $('#your-name').prev().html(yourname + '<span class="mandatory">*</span>');
            }
            else {
                $('#your-name').addClass("error");
                $('#your-name').prev().addClass("error");
                $('#your-name').prev().html(yourname + '<span class="mandatory">*</span>' + ' (' + nameFailed + ')');
                formValid = false;
            }
      
            if(formValid) {
                //hide form and display loading
                $('#tipfriend-wrapper').css('visibility','hidden');
                $('.jpopup-content').append($('<img src="/images/ajax-loader.gif" class="ajax-loader" alt="' + loadingTxt + '" title="' + loadingTxt + '" />'));
                
                //get current page id
                var page = $('.tip-friend:first').attr("href");
                var pageid = page.substring(page.indexOf("id=")+3,page.length);
                var pageLang = page.substring(page.indexOf("?lang=")+6,page.indexOf("&"));
                //send mail
                $.post('/templates/Tipfriend.aspx', {
                        id : pageid,
                        friendemail : $('[name="friendemail"]').val(),
                        youremail : $('[name="youremail"]').val(),
                        message : $('[name="message"]').val(),
                        yourname : $('[name="yourname"]').val(),
                        json : 'true',
                        lang : pageLang
                    }, function(data) {
                        if(data.success == "true") {
                            $('.ajax-loader').css('display','none');
                            $('.jpopup-content').append($('<h1 class="successmsg"><span>' + msgSuccess + '</span></h1>'));
                        }
                        else {
                            $('.ajax-loader').css('display','none');
                            $('#tipfriend-wrapper').css('visibility','visible');
                            if(data.friendMail == "false") {
                                $('#friend-email').addClass("error");               
                                $('#friend-email').prev().addClass("error");
                                $('#friend-email').prev().html(friendMail +' (' + mailFailed + ')');
                            }
                            if(data.yourMail == "false") {
                                $('#your-email').addClass("error");               
                                $('#your-email').prev().addClass("error");
                                $('#your-email').prev().html(myMail + ' (' + mailFailed + ')');
                            }
                        }
                        
                    },"json");
            }    
            return false;   
        });
    }
    
    $.fn.CloseTipFriend = function(options) {
        $.extend({}, options);
        return this.bind('click', function() {
            $('.jpopup, .modalBackground').css({
                display:'none'
            });
            return false;
        });
    }
    $.fn.Zoom = function(options) {
        $.extend({}, options);
        return this.bind('click', function() {
            //display overlay
            $('.modalBackground').remove();
            $('<div class="modalBackground" />').css({
                position: 'fixed',
                Zindex: '10000'
            }).appendTo('#aspnetForm');
            $('.modalBackground').css('display','block');
            
             // Skapa grejerna
             var    source    = $(this).attr('href'),
                    image     = $('<img/>'),
                    // Grejen som bilden är i, göms utanför skärmen så man kan kolla hur stor bilden är
                    popup     = $('<div class="jpopup imgpop" />'),
                    popupWrapper = $('<div class="jpopup-content"/>');
             
            //appenda popup
            popupWrapper.appendTo(popup);
            popup.appendTo($('#aspnetForm'));

            image.appendTo(popupWrapper).bind('load', function() {
                popup.css({
                    display: 'block',
                    height: image.height() + 10,
                    marginLeft: -5 - image.width() / 2,
                    // Här kommer den in i synfältet igen :)
                    top: ((document.documentElement.clientHeight || document.body.clientHeight)/2) - (image.height()/2),
                    width: image.width() + 5,
                    position: 'fixed',
                    left: '50%',
                    marginLeft: -5 - image.width() / 2,
                    zIndex: '100001'
                });
            }).attr('src', source);
            // måste sättas efter bind, annars funkar inte onload i IE när bilden dras ur cache
            
            popup.bind('click', function() {
                $('.jpopup, .modalBackground').css({
                    display: 'none'
                });
            });
            return false;           
        });
    }
    var interval;
    $.fn.Ticker = function(options) {
    	$.extend({}, options);
    	 return this.each(function() {
			//first create delimiter
			$(this).css('width', '194px');
			$(this).append($('<li class="news-restart">' + latestNews + '</li>'));
			var list = $(this);
		    var currentPos = 0;
		    var scrollHeight = $(this).height();
			
			var dllist = $(this).children('li');
			dllist.clone().appendTo($(this));
			dllist.clone().appendTo($(this));
			dllist.clone().appendTo($(this));
			
			interval = setInterval(function(){
				move();
			},80);
			
			$(this).hover(
                function() {
                    clearInterval(interval);
                },
                function() {
                    interval = setInterval(function(){
				        move();
			        },80);
                }
            );
			
	
			var move = function() {
				currentPos = currentPos + 2;
				if (currentPos <= scrollHeight) {
					list.css({
						top: '-' + currentPos + 'px'
					});
				}
				else {
					currentPos = 0;
					list.css({
						top: '-' + currentPos + 'px'
					});
				}
			}

    	});
	}
})(jQuery);
jQuery(function($) {
    $('.newsticker-container').css({overflow: 'hidden'});
    $('.tip-friend').TipFriend();
    $('.zoom').Zoom();
    //$('.newsticker').Ticker();
    
    
    if($.browser.msie) {
	    if($.browser.version < 7) {
	        $(window).scroll(function () {
	            var bHeight = (document.documentElement.clientHeight || document.body.clientHeight);
                topPos = ((bHeight/2) - ($('.jpopup').height()/2)) + (document.documentElement.scrollTop || document.body.scrollTop );
                topPos = topPos + 'px';
                $('.jpopup').css('top',topPos);
	        });
	    }
	}
});
jQuery.noConflict();
jQuery(window).bind('unload', function() { jQuery('*').unbind(); });



function togglePlayDiv(id)
{
    if(document.getElementById(id).style.visibility == "hidden")
    {
        document.getElementById(id).style.visibility = "visible";
        
        // kod för att stoppa
    }else{
        document.getElementById(id).style.visibility = "hidden";
        
        // kod för att starta play
    }
    
    
}
