var curr_slide=0;
interval_id="";
function init_show(){
	$('div.demo-show2> div').hide();
    $('div.demo-show2> h3').click(function() {		
		open_panel($(this));
		clearInterval(interval_id);
    });
	window.setTimeout(function(){
	 $('div.demo-show2> h3').eq(curr_slide++).click();
	 set_slideshow();
	},500);
}

function open_panel(obj) {
    curr_slide = $('div.demo-show2> h3').index($(obj)) + 1;
    $('div.demo-show2> h3').removeClass("selected");
    $('div.demo-show2> div.selected_pane').removeClass("selected_pane");
    $(obj).addClass("selected");    
    $(obj).next("div").addClass("selected_pane");	
    $("#main_img_holder").fadeOut(300,function(){
		$(this).attr({"src": $(obj).next("div").find(".main_img").attr("src")});
		$(this).fadeIn(200);
	});    
	$(".desc_overlay").html($(obj).next("div").find(".story_desc").text());
    $("#main_img_holder").parent().attr({
        "href": $(obj).next("div").find("a").attr("href")
    });            
    var $nextDiv = $(obj).next();
    var $visibleSiblings = $nextDiv.siblings('div:visible');
    if ($visibleSiblings.length) {
        $visibleSiblings.slideUp(200, function() {
            $nextDiv.slideToggle(200, function() {
                $("#current_slide_mark").css({
                    "left": ($(obj).position().left - 17) + "px",
                    "top": ($(obj).position().top + 10) + "px"
                }).show();
            });
        });
    } else {
        $nextDiv.slideToggle(200, function() {
            $("#current_slide_mark").css({
                "left": ($(obj).position().left - 17) + "px",
                "top": ($(obj).position().top + 10) + "px"
            }).show();
        });
    }
}

function set_slideshow(){
  interval_id=window.setInterval(function(){
   open_panel($('div.demo-show2> h3').eq(curr_slide++));
   if(curr_slide>=$('div.demo-show2> h3').size()){
   	curr_slide=0;
   }
  }, 5000)
}

function submitEdit() {
	$("#userEditProfile").submit();
}

function cancelEdit() {
	location.href = BASE_URL + "/my-account/profile";
}

function saveAbout(save) {
	if (!save) $("#aboutText").val(""); 
	$.post("/my-account/profile/saveAbout", $(".aboutForm").serialize());
}

function showCancelReason() {
	$('#subsCancelReason').slideToggle();
}

function cancelAccount() {
	if (confirm('Are you sure you want to cancel your Account? \n\nBy doing so if you have an active subscription it will be canceled and you will have access only until the end of the paid period.')) {
		location.href = BASE_URL + '/my-account/edit/account_cancel';
	}
}

$(document).ready(function(){

	$("#aboutText").focus(function(){
		if ($("#aboutText").val() == "Write something about yourself...") {
			$("#aboutText").val(""); 
		}
	}).blur(function(){
		if ($("#aboutText").val().replace(/^\s+|\s+$/g,'') == "") {
			$("#aboutText").val("Write something about yourself..."); 
		}
	});
	
	$(".myCoursesTogle").click(function() {
		$(".coursesContainer .tab").removeClass("active");
		$(this).parent().addClass("active");
		$.post("/my-account/profile/getCourses", {type: $(this).attr("id"), user: $(".aboutYou .user").html()}, function(data){
			$(".coursesContainer .html").html(data);
		});
	});
	
	$(".myVideosTogle").click(function() {
		$(".videoContainer .tab").removeClass("active");
		$(this).parent().addClass("active");
		$.post("/my-account/profile/getVideos", {type: $(this).attr("id"), user: $(".aboutYou .user").html()}, function(data){
			$(".videoContainer .html").html(data);
		});
	});
	
	$(".myGroupsTogle").click(function() {
		$(".groupsContainer .tab").removeClass("active");
		$(this).parent().addClass("active");
		$.post("/my-account/profile/getGroups", {type: $(this).attr("id"), user: $(".aboutYou .user").html()}, function(data){
			$(".groupsContainer .html").html(data);
		});
	});
	
	$("#editAction").click(function() {
		$.post("/my-account/profile/getEditForm", function(data) {
			$("#centerProfileContent").html(data);
			$("#birthdate").datepick({defaultDate: new Date(2010, 1 - 1, 1)});
		});
	});
	
	$(".send_but.accountBut").click(function() {
		$("#userEditAccount").submit();
	});
	
	function removeHash(){
		
		window.location = window.location.href.replace( /#.*/, "");
	}

	function showUploadImageError(errorText){
		$.nyroModalManual({
			type : "content",
			blocker : "document",
			bgColor: '#000',
			minHeight: 50,
			showContent: showContentAnimation,
			endShowContent: bindSendButton,
			endRemove: removeHash,
			content: "<div id='errorMsg'><p>"+errorText+"</p></div>"
		});
	}
	

	$(function(){
		jash = window.location.hash;
		if (window.location.hash) {
			errorMsg=jash.replace("#"," ");
			showUploadImageError(errorMsg.replace(/\-/g, " "));
		}
	})
	

	jQuery('#leftProfile .imageProfileContainer').mouseenter(function() {
		$('#leftProfile .imageChangeButton').slideDown();
	}).mouseleave(function() {
		$('#leftProfile .imageChangeButton').slideUp();
	});

    jQuery('.dashboardrightColumn .imageProfileContainer').mouseenter(function() {
		$('.dashboardrightColumn .imageChangeButton').slideDown();
	}).mouseleave(function() {
		$('.dashboardrightColumn .imageChangeButton').slideUp();
	});

	jQuery('.imageChangeButton').click(function(){
		showImageDialog();	
	});

	function showImageDialog(){
		$.nyroModalManual({
			type : "content",
			blocker : "document",
			bgColor: '#000',
			minWidth: 400, 
			minHeight: 170,
			showContent: showContentAnimation,
			ajax: {url: "/my-account/profile/editImageView", data: {url: window.location.href}, type: 'post' }
		});
	}
	
	
	function bindSendButton(elts,settings){
		jQuery('#sendImageButton').click(function(){
			sendImage();
		});
	}
	
	function sendImage(){
		$("#edit_personal_image").submit();
	}
	
	$('#checkall').click(function(){
		$('#uncheckall').removeAttr('checked');
		if (this.checked == true) {
			$('.checks_container input[type=checkbox]').each(function(){
				this.checked = true;
			});
		} else {
			document.getElementById('user-preferences').reset();
		}
	});
	
	$('#uncheckall').click(function(){
		$('#checkall').removeAttr('checked');
		if (this.checked == true) {
			$('.checks_container input[type=checkbox]').each(function(){
				this.checked = false;
			});
		} else {
			document.getElementById('user-preferences').reset();
		}
	});
		
});

